plum
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
- 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
|
-
|
|
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
|
-
|
|
137
|
+
let Item::Fn(f) = &src.items[c.item] else { return Vec::new() };
|
|
127
|
-
let fname = f.name.clone();
|
|
128
|
-
|
|
138
|
+
let FnBody::Block(fb) = &f.body else { return Vec::new() };
|
|
129
|
-
|
|
139
|
+
let Some(Stmt { kind: StmtKind::Match(m), .. }) = fb.stmts.last() else { return Vec::new() };
|
|
130
|
-
|
|
140
|
+
let mut inner: Vec<String> = Vec::new();
|
|
131
|
-
|
|
141
|
+
for case in &m.cases {
|
|
132
|
-
|
|
142
|
+
for p in &case.patterns {
|
|
133
|
-
|
|
143
|
+
crate::visit::patternBinders(p, &mut inner);
|
|
144
|
+
}
|
|
134
145
|
}
|
|
135
|
-
}
|
|
136
|
-
|
|
146
|
+
let binds = bindCounts(f);
|
|
137
|
-
|
|
147
|
+
inner.retain(|t| binds.get(t) == Some(&1));
|
|
148
|
+
inner
|
|
149
|
+
})
|
|
150
|
+
.collect();
|
|
138
|
-
|
|
151
|
+
for (idx, it) in src.items.iter().enumerate() {
|
|
139
|
-
|
|
152
|
+
let mut body = match it {
|
|
140
|
-
|
|
153
|
+
Item::Fn(g) => g.body.clone(),
|
|
141
|
-
|
|
154
|
+
Item::Test(t) => FnBody::Block(t.body.clone()),
|
|
142
|
-
|
|
155
|
+
Item::Const(k) => FnBody::Expr(k.value.clone()),
|
|
143
|
-
|
|
156
|
+
_ => continue,
|
|
144
|
-
|
|
157
|
+
};
|
|
145
|
-
let inSelf = idx == c.item;
|
|
146
|
-
let selfBody = f.body.clone();
|
|
147
|
-
|
|
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
|
-
|
|
162
|
+
match argFor(call, c.param, &c.v) {
|
|
149
163
|
Some(a) if deepFresh(a) => {}
|
|
164
|
+
Some(Expr { kind: ExprKind::Var(t), .. })
|
|
150
|
-
|
|
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
|
-
|
|
169
|
+
ExprKind::Var(n) => {
|
|
170
|
+
if let Some(&ci) = byName.get(n.as_str()) {
|
|
171
|
+
ok[ci] = false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
154
|
-
|
|
174
|
+
ExprKind::Attribute(a) => {
|
|
155
|
-
|
|
175
|
+
if let AttrKind::Method(mc) = &a.attr {
|
|
156
|
-
|
|
176
|
+
// A method call on a receiver of known type is never a call of the free fn
|
|
157
|
-
|
|
177
|
+
// (a `Type.f(..)` call has a type name as its object and is not told apart).
|
|
158
|
-
|
|
178
|
+
let typed = mc.recv.is_some() && !matches!(a.object.kind, ExprKind::TypeName(_));
|
|
159
|
-
|
|
179
|
+
if let (Some(&ci), false) = (byName.get(&mc.name), typed) {
|
|
160
|
-
|
|
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();
|