plum

#treesitter#compiler#wasm

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

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


a85700c — Peter John 2026-11-09T21:28:10+05:30
Reuse: check every candidate's callers in one walk of the program
Files changed (1) hide show
  1. compiler/core/src/reuse.rs +57 -38
compiler/core/src/reuse.rs CHANGED
@@ -120,50 +120,69 @@ pub fn markReuse(src: &mut Source) -> Vec<String> {
120
120
  }
121
121
  cands.push(Cand { item: idx, param: pi, v: v.clone() });
122
122
  }
123
- // Callers.
123
+ // Callers: one walk over the program, each call site checked against the candidate it names.
124
124
  let mut ok: Vec<bool> = vec![true; cands.len()];
125
+ let byName: HashMap<String, usize> = cands
126
+ .iter()
127
+ .enumerate()
125
- for (ci, c) in cands.iter().enumerate() {
128
+ .filter_map(|(ci, c)| match &src.items[c.item] {
129
+ Item::Fn(f) => Some((f.name.clone(), ci)),
130
+ _ => None,
131
+ })
132
+ .collect();
133
+ // Per candidate: the match's pattern binders bound once in it.
134
+ let inners: Vec<Vec<String>> = cands
135
+ .iter()
136
+ .map(|c| {
126
- let Item::Fn(f) = &src.items[c.item] else { continue };
137
+ let Item::Fn(f) = &src.items[c.item] else { return Vec::new() };
127
- let fname = f.name.clone();
128
- let FnBody::Block(fb) = &f.body else { continue };
138
+ let FnBody::Block(fb) = &f.body else { return Vec::new() };
129
- let Some(Stmt { kind: StmtKind::Match(m), .. }) = fb.stmts.last() else { continue };
139
+ let Some(Stmt { kind: StmtKind::Match(m), .. }) = fb.stmts.last() else { return Vec::new() };
130
- let mut inner: Vec<String> = Vec::new();
140
+ let mut inner: Vec<String> = Vec::new();
131
- for case in &m.cases {
141
+ for case in &m.cases {
132
- for p in &case.patterns {
142
+ for p in &case.patterns {
133
- crate::visit::patternBinders(p, &mut inner);
143
+ crate::visit::patternBinders(p, &mut inner);
144
+ }
134
145
  }
135
- }
136
- let binds = bindCounts(f);
146
+ let binds = bindCounts(f);
137
- inner.retain(|t| binds.get(t) == Some(&1));
147
+ inner.retain(|t| binds.get(t) == Some(&1));
148
+ inner
149
+ })
150
+ .collect();
138
- for (idx, it) in src.items.iter().enumerate() {
151
+ for (idx, it) in src.items.iter().enumerate() {
139
- let mut body = match it {
152
+ let mut body = match it {
140
- Item::Fn(g) => g.body.clone(),
153
+ Item::Fn(g) => g.body.clone(),
141
- Item::Test(t) => FnBody::Block(t.body.clone()),
154
+ Item::Test(t) => FnBody::Block(t.body.clone()),
142
- Item::Const(k) => FnBody::Expr(k.value.clone()),
155
+ Item::Const(k) => FnBody::Expr(k.value.clone()),
143
- _ => continue,
156
+ _ => continue,
144
- };
157
+ };
145
- let inSelf = idx == c.item;
146
- let selfBody = f.body.clone();
147
- mapExprs(&mut body, &mut |e| match &mut e.kind {
158
+ mapExprs(&mut body, &mut |e| match &mut e.kind {
159
+ ExprKind::FnCall(call) => {
160
+ let Some(&ci) = byName.get(&call.name) else { return };
161
+ let c = &cands[ci];
148
- ExprKind::FnCall(call) if call.name == fname => match argFor(call, c.param, &c.v) {
162
+ match argFor(call, c.param, &c.v) {
149
163
  Some(a) if deepFresh(a) => {}
164
+ Some(Expr { kind: ExprKind::Var(t), .. })
150
- Some(Expr { kind: ExprKind::Var(t), .. }) if inSelf && inner.contains(t) && crate::visit::countVar(&selfBody, t) == 1 => {}
165
+ if idx == c.item && inners[ci].contains(t) && matches!(it, Item::Fn(f) if crate::visit::countVar(&f.body, t) == 1) => {}
151
166
  _ => ok[ci] = false,
152
- },
167
+ }
168
+ }
153
- ExprKind::Var(n) if *n == fname => ok[ci] = false,
169
+ ExprKind::Var(n) => {
170
+ if let Some(&ci) = byName.get(n.as_str()) {
171
+ ok[ci] = false;
172
+ }
173
+ }
154
- ExprKind::Attribute(a) => {
174
+ ExprKind::Attribute(a) => {
155
- if let AttrKind::Method(mc) = &a.attr {
175
+ if let AttrKind::Method(mc) = &a.attr {
156
- // A method call on a receiver of known type is never a call of the free fn
176
+ // A method call on a receiver of known type is never a call of the free fn
157
- // (a `Type.f(..)` call has a type name as its object and is not told apart).
177
+ // (a `Type.f(..)` call has a type name as its object and is not told apart).
158
- let typed = mc.recv.is_some() && !matches!(a.object.kind, ExprKind::TypeName(_));
178
+ let typed = mc.recv.is_some() && !matches!(a.object.kind, ExprKind::TypeName(_));
159
- if mc.name == fname && !typed {
179
+ if let (Some(&ci), false) = (byName.get(&mc.name), typed) {
160
- ok[ci] = false;
180
+ ok[ci] = false;
161
- }
162
181
  }
163
182
  }
183
+ }
164
- _ => {}
184
+ _ => {}
165
- });
185
+ });
166
- }
167
186
  }
168
187
  // Mark.
169
188
  let mut report = Vec::new();