plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


5ec63bc — Peter John 2026-11-11T00:59:48+05:30
checker: check the arity of types written in bodies without copying them
compiler/checker/src/declchecks.rs CHANGED
@@ -101,26 +101,47 @@ pub(crate) fn checkBareGenericTypes(source: &ast::Source) -> Vec<CheckError> {
101
101
  // The types written in the body (annotated locals, type arguments of
102
102
  // constructors and calls, closure parameters): only their argument count is
103
103
  // checked, each at its top-level statement.
104
- let stmts = match &f.body {
105
- ast::FnBody::Block(b) => b.stmts.clone(),
104
+ // Read in place: a copy of every body per check was most of this check's cost.
106
- ast::FnBody::Expr(e) => vec![ast::Stmt::Expr(e.clone(), f.line, f.column)],
105
+ // An expression body is the one statement it stands for, at the fn's position.
107
- ast::FnBody::Extern => Vec::new(),
106
+ let one = |s: Option<&ast::Stmt>, e: Option<&ast::Expr>, line: usize, column: usize, errors: &mut Vec<CheckError>| {
108
- };
109
- for mut s in stmts {
110
- let (line, column) = if s.span.startLine != 0 { (s.span.startLine, s.span.startColumn) } else { (f.line, f.column) };
107
+ let mut ty = |t: &ast::Type| arity(t, &params, line, column, &generic_enums, errors);
108
+ match (s, e) {
111
- plum_core::rename::typesInStmts(std::slice::from_mut(&mut s), &mut |t| {
109
+ (Some(s), _) => plum_core::rename::typesInStmtsRef(std::slice::from_ref(s), &mut ty),
112
- arity(t, &params, line, column, &generic_enums, &mut errors)
110
+ (None, Some(e)) => plum_core::rename::typesInExprRef(e, &mut ty),
111
+ (None, None) => {}
113
- });
112
+ }
114
113
  // a constructor's own type arguments: `Box<Int, Int>(value: 2)`
115
- plum_core::visit::walkBlockRef(&ast::Block { stmts: vec![s] }, &mut |e| {
114
+ let mut ctor = |e: &ast::Expr| {
116
115
  if let ast::ExprKind::ClassCall(c) = &e.kind {
117
116
  if !c.generics.is_empty() {
118
117
  let t = ast::Type { name: c.type_name.clone(), generics: Vec::new() };
119
118
  let t = ast::Type { generics: c.generics.iter().map(|_| t.clone()).collect(), ..t };
120
- arity(&t, &params, line, column, &generic_enums, &mut errors);
119
+ arity(&t, &params, line, column, &generic_enums, errors);
120
+ }
121
+ }
122
+ };
123
+ match (s, e) {
124
+ (Some(s), _) => {
125
+ for e in plum_core::visit::stmtExprRefs(s) {
126
+ plum_core::visit::walkRef(e, &mut ctor);
127
+ }
128
+ for b in plum_core::visit::stmtBlockRefs(s) {
129
+ plum_core::visit::walkBlockRef(b, &mut ctor);
121
130
  }
122
131
  }
132
+ (None, Some(e)) => plum_core::visit::walkRef(e, &mut ctor),
133
+ (None, None) => {}
134
+ }
123
- });
135
+ };
136
+ match &f.body {
137
+ ast::FnBody::Block(b) => {
138
+ for s in &b.stmts {
139
+ let (line, column) = if s.span.startLine != 0 { (s.span.startLine, s.span.startColumn) } else { (f.line, f.column) };
140
+ one(Some(s), None, line, column, &mut errors);
141
+ }
142
+ }
143
+ ast::FnBody::Expr(e) => one(None, Some(e), f.line, f.column, &mut errors),
144
+ ast::FnBody::Extern => {}
124
145
  }
125
146
  }
126
147
  ast::Item::Enum(e) => {
compiler/core/src/rename.rs CHANGED
@@ -146,6 +146,64 @@ fn typesInPattern(p: &mut CasePattern, f: &mut dyn FnMut(&mut Type)) {
146
146
  }
147
147
  }
148
148
 
149
+ /// `typesInStmts` read in place: the same types, in the same order, with no copy of `stmts`.
150
+ pub fn typesInStmtsRef(stmts: &[Stmt], f: &mut dyn FnMut(&Type)) {
151
+ for s in stmts {
152
+ match &s.kind {
153
+ StmtKind::Assign(a) => {
154
+ if let Some(t) = &a.ty {
155
+ f(t);
156
+ }
157
+ }
158
+ StmtKind::Match(m) => {
159
+ for c in &m.cases {
160
+ for p in &c.patterns {
161
+ typesInPatternRef(p, f);
162
+ }
163
+ }
164
+ }
165
+ _ => {}
166
+ }
167
+ for e in crate::visit::stmtExprRefs(s) {
168
+ typesInExprRef(e, f);
169
+ }
170
+ for b in crate::visit::stmtBlockRefs(s) {
171
+ typesInStmtsRef(&b.stmts, f);
172
+ }
173
+ }
174
+ }
175
+
176
+ fn typesInPatternRef(p: &CasePattern, f: &mut dyn FnMut(&Type)) {
177
+ match p {
178
+ CasePattern::As { ty, args, .. } => f(&Type { name: ty.clone(), generics: args.clone() }),
179
+ CasePattern::Class { fields, .. } => fields.iter().for_each(|x| typesInPatternRef(x, f)),
180
+ _ => {}
181
+ }
182
+ }
183
+
184
+ /// `typesInStmtsRef` for one expression.
185
+ pub fn typesInExprRef(e: &Expr, f: &mut dyn FnMut(&Type)) {
186
+ match &e.kind {
187
+ ExprKind::Closure(c) => {
188
+ c.param_types.iter().flatten().for_each(|t| f(t));
189
+ typesInStmtsRef(&c.body.stmts, f);
190
+ }
191
+ ExprKind::Block(b) => typesInStmtsRef(&b.stmts, f),
192
+ ExprKind::ClassCall(c) => c.generics.iter().for_each(|t| f(t)),
193
+ ExprKind::FnCall(c) => c.type_args.iter().for_each(|t| f(t)),
194
+ ExprKind::Attribute(a) => {
195
+ if let AttrKind::Method(c) = &a.attr {
196
+ c.type_args.iter().for_each(|t| f(t));
197
+ }
198
+ }
199
+ ExprKind::Bind(b) => b.case.patterns.iter().for_each(|p| typesInPatternRef(p, f)),
200
+ _ => {}
201
+ }
202
+ for ch in crate::visit::childRefs(e) {
203
+ typesInExprRef(ch, f);
204
+ }
205
+ }
206
+
149
207
  /// `typesInStmts` for one expression.
150
208
  pub fn typesInExpr(e: &mut Expr, f: &mut dyn FnMut(&mut Type)) {
151
209
  match &mut e.kind {