plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
482ec9d
— Peter John
2026-11-10T15:35:06+05:30
Compiler: sibling scopes may declare one name with two types
compiler/checker/tests/checker/diagnostic_tests.rs
CHANGED
|
@@ -244,3 +244,30 @@ fn genericMainIsAnError() {
|
|
|
244
244
|
assert_eq!((es[0].line, es[0].column), (1, 1), "{es:?}");
|
|
245
245
|
assert!(es[0].message.contains("main can't be generic"), "{es:?}");
|
|
246
246
|
}
|
|
247
|
+
|
|
248
|
+
/// A declaration in a sibling scope gets a fresh name (`binders::freshenSiblingDeclarations`);
|
|
249
|
+
/// one that shadows a local still in scope is still the "already declared" error.
|
|
250
|
+
#[test]
|
|
251
|
+
fn shadowingALocalInScopeIsStillAnErrorBesideSiblingDeclarations() {
|
|
252
|
+
let es = errors(
|
|
253
|
+
"fun f(k: Int) -> Int =\n if k > 0\n d := 2\n d\n else\n d := 3\n d := 4\n d\n",
|
|
254
|
+
);
|
|
255
|
+
assert_eq!(es.len(), 1, "{es:?}");
|
|
256
|
+
assert!(es[0].message.contains("assign 'd': already declared"), "{es:?}");
|
|
257
|
+
let es = errors("fun f(k: Int) -> Int =\n d := 1\n if k > 0\n d := 2\n d\n else\n d\n");
|
|
258
|
+
assert!(es.iter().any(|e| e.message.contains("assign 'd': already declared")), "{es:?}");
|
|
259
|
+
let es = errors("fun f(d: Int) -> Int =\n if d > 0\n d := 2\n d\n else\n d := 3\n d\n");
|
|
260
|
+
assert_eq!(es.iter().filter(|e| e.message.contains("already declared")).count(), 2, "{es:?}");
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/// An error about a renamed sibling declaration names it as the source does.
|
|
264
|
+
#[test]
|
|
265
|
+
fn aRenamedSiblingDeclarationKeepsItsSourceNameInMessages() {
|
|
266
|
+
let es = errors(
|
|
267
|
+
"fun f(k: Int) -> Int =\n if k > 0\n d := 2\n d\n else\n d: Int := true\n d\n",
|
|
268
|
+
);
|
|
269
|
+
assert_eq!(es.len(), 1, "{es:?}");
|
|
270
|
+
let shown = plum_core::diag::sourceName(&es[0].message);
|
|
271
|
+
assert!(shown.contains("assign 'd': type mismatch"), "{shown}");
|
|
272
|
+
assert_eq!(es[0].line, 6, "{es:?}");
|
|
273
|
+
}
|
compiler/core/src/binders.rs
CHANGED
|
@@ -202,3 +202,119 @@ fn renameCallsIn(stmts: &mut [Stmt], old: &str, new: &str) {
|
|
|
202
202
|
}
|
|
203
203
|
}
|
|
204
204
|
}
|
|
205
|
+
|
|
206
|
+
/// Renames a `:=` declaration whose name another, sibling scope of the same function already
|
|
207
|
+
/// declared (`d := A(..)` in one `if` branch and `d := B(..)` in the other, in two closures'
|
|
208
|
+
/// bodies, in two arms). Codegen keeps one wasm local per name in a function, typed once, so
|
|
209
|
+
/// the second declaration would store a `B` in the local of an `A`. The second one and every
|
|
210
|
+
/// use of it in the rest of its block get a fresh name.
|
|
211
|
+
///
|
|
212
|
+
/// A declaration of a name that is still in scope (an enclosing block's local, a parameter)
|
|
213
|
+
/// is left alone: it is the checker's "already declared" error.
|
|
214
|
+
pub fn freshenSiblingDeclarations(source: &mut Source) {
|
|
215
|
+
for item in source.items.iter_mut() {
|
|
216
|
+
match item {
|
|
217
|
+
Item::Fn(f) => {
|
|
218
|
+
let mut visible: HashSet<String> = f.params.iter().map(|p| p.name.clone()).collect();
|
|
219
|
+
visible.insert("self".to_string());
|
|
220
|
+
let mut seen = visible.clone();
|
|
221
|
+
match &mut f.body {
|
|
222
|
+
FnBody::Block(b) => siblingStmts(&mut b.stmts, &mut seen, &visible),
|
|
223
|
+
FnBody::Expr(e) => siblingExpr(e, &mut seen, &visible),
|
|
224
|
+
FnBody::Extern => {}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
Item::Test(t) => siblingStmts(&mut t.body.stmts, &mut HashSet::new(), &HashSet::new()),
|
|
228
|
+
Item::Const(c) => siblingExpr(&mut c.value, &mut HashSet::new(), &HashSet::new()),
|
|
229
|
+
_ => {}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/// `seen`: every name bound so far anywhere in the function. `outer`: the names in scope where
|
|
235
|
+
/// this block starts.
|
|
236
|
+
fn siblingStmts(stmts: &mut [Stmt], seen: &mut HashSet<String>, outer: &HashSet<String>) {
|
|
237
|
+
let mut visible = outer.clone();
|
|
238
|
+
for i in 0..stmts.len() {
|
|
239
|
+
let (head, rest) = stmts.split_at_mut(i + 1);
|
|
240
|
+
let s = &mut head[i];
|
|
241
|
+
// The values are evaluated, and the nested blocks left, before the name is bound.
|
|
242
|
+
if let StmtKind::Match(m) = &mut s.kind {
|
|
243
|
+
for e in m.subjects.iter_mut() {
|
|
244
|
+
siblingExpr(e, seen, &visible);
|
|
245
|
+
}
|
|
246
|
+
for c in &mut m.cases {
|
|
247
|
+
let mut inner = visible.clone();
|
|
248
|
+
for p in &c.patterns {
|
|
249
|
+
let mut now = Vec::new();
|
|
250
|
+
patternBinders(p, &mut now);
|
|
251
|
+
seen.extend(now.iter().cloned());
|
|
252
|
+
inner.extend(now);
|
|
253
|
+
}
|
|
254
|
+
if let Some(g) = &mut c.guard {
|
|
255
|
+
siblingExpr(g, seen, &inner);
|
|
256
|
+
}
|
|
257
|
+
siblingStmts(&mut c.body.stmts, seen, &inner);
|
|
258
|
+
}
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
for e in stmtExprs(s) {
|
|
262
|
+
siblingExpr(e, seen, &visible);
|
|
263
|
+
}
|
|
264
|
+
for b in stmtBlocks(s) {
|
|
265
|
+
siblingStmts(&mut b.stmts, seen, &visible);
|
|
266
|
+
}
|
|
267
|
+
let StmtKind::Assign(a) = &mut s.kind else { continue };
|
|
268
|
+
if !a.declare {
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
for t in &mut a.targets {
|
|
272
|
+
let AssignTarget::Var(n) = t else { continue };
|
|
273
|
+
if n == "_" || visible.contains(n.as_str()) {
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if seen.insert(n.clone()) {
|
|
277
|
+
visible.insert(n.clone());
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
let old = n.clone();
|
|
281
|
+
let new = fresh(&old);
|
|
282
|
+
renameStmts(rest, &mut |r: &mut String, _: Name| {
|
|
283
|
+
if *r == old {
|
|
284
|
+
*r = new.clone();
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
renameCallsIn(rest, &old, &new);
|
|
288
|
+
seen.insert(new.clone());
|
|
289
|
+
visible.insert(new.clone());
|
|
290
|
+
*n = new;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
fn siblingExpr(e: &mut Expr, seen: &mut HashSet<String>, visible: &HashSet<String>) {
|
|
296
|
+
match &mut e.kind {
|
|
297
|
+
ExprKind::Closure(c) => {
|
|
298
|
+
let mut inner = visible.clone();
|
|
299
|
+
inner.extend(c.params.iter().cloned());
|
|
300
|
+
seen.extend(c.params.iter().cloned());
|
|
301
|
+
siblingStmts(&mut c.body.stmts, seen, &inner);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
ExprKind::Block(b) => {
|
|
305
|
+
siblingStmts(&mut b.stmts, seen, visible);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
ExprKind::Bind(b) => {
|
|
309
|
+
let mut names = Vec::new();
|
|
310
|
+
for p in &b.case.patterns {
|
|
311
|
+
patternBinders(p, &mut names);
|
|
312
|
+
}
|
|
313
|
+
seen.extend(names);
|
|
314
|
+
}
|
|
315
|
+
_ => {}
|
|
316
|
+
}
|
|
317
|
+
for ch in childExprs(e) {
|
|
318
|
+
siblingExpr(ch, seen, visible);
|
|
319
|
+
}
|
|
320
|
+
}
|
compiler/core/src/lower.rs
CHANGED
|
@@ -56,6 +56,7 @@ pub fn frontEndDiag(source: &mut Source) -> Result<(), Vec<Diagnostic>> {
|
|
|
56
56
|
}
|
|
57
57
|
// Before forms lifts loop bodies: a lifted body's binders are renamed with it.
|
|
58
58
|
crate::binders::freshenShadowingBinders(source);
|
|
59
|
+
crate::binders::freshenSiblingDeclarations(source);
|
|
59
60
|
crate::forms::prepareForms(source);
|
|
60
61
|
// The stages above build nodes (constructors) with no id.
|
|
61
62
|
crate::ast::numberIds(source);
|
tests/core/sibling_scopes_two_types.plum
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
# One local name declared with a different type in each of two sibling scopes: `if` branches,
|
|
2
|
+
# loop bodies, loop binders, closure bodies, `match` arms, `if Pattern :=` binders. Locals are
|
|
3
|
+
# function-wide in wasm, so each declaration needs a local of its own type.
|
|
4
|
+
import std/Int
|
|
5
|
+
import std/Float
|
|
6
|
+
import std/Str
|
|
7
|
+
import std/Bool
|
|
8
|
+
import std/Option
|
|
9
|
+
import std/Result
|
|
10
|
+
import std/Slice
|
|
11
|
+
import std/collections/List
|
|
12
|
+
|
|
13
|
+
struct A(x: Int)
|
|
14
|
+
struct B(y: Str, z: Int)
|
|
15
|
+
struct V(x: Float, y: Float)
|
|
16
|
+
struct Tally(mut n: Int)
|
|
17
|
+
|
|
18
|
+
enum E =
|
|
19
|
+
| One(a: A)
|
|
20
|
+
| Two(b: B)
|
|
21
|
+
| Num(n: Float)
|
|
22
|
+
| Word(s: Str)
|
|
23
|
+
|
|
24
|
+
fun loops() -> Int =
|
|
25
|
+
t := Tally(n: 0)
|
|
26
|
+
for i := range 3
|
|
27
|
+
d := A(x: i)
|
|
28
|
+
t.n = t.n + d.x
|
|
29
|
+
for i := range 2
|
|
30
|
+
d := B(y: "b", z: i + 10)
|
|
31
|
+
t.n = t.n + d.z
|
|
32
|
+
for i := range 2
|
|
33
|
+
d := 1.5
|
|
34
|
+
t.n = t.n + Int(d * 2.0)
|
|
35
|
+
for i := range 2
|
|
36
|
+
d := "four"
|
|
37
|
+
t.n = t.n + d.length()
|
|
38
|
+
t.n
|
|
39
|
+
|
|
40
|
+
fun branches(flag: Bool) -> Int =
|
|
41
|
+
if flag
|
|
42
|
+
d := A(x: 1)
|
|
43
|
+
d.x
|
|
44
|
+
else
|
|
45
|
+
d := B(y: "b", z: 2)
|
|
46
|
+
d.z
|
|
47
|
+
|
|
48
|
+
fun scalarBranches(k: Int) -> Int =
|
|
49
|
+
if k == 0
|
|
50
|
+
d := 7
|
|
51
|
+
d
|
|
52
|
+
else if k == 1
|
|
53
|
+
d := 2.5
|
|
54
|
+
Int(d * 2.0)
|
|
55
|
+
else if k == 2
|
|
56
|
+
d := "abc"
|
|
57
|
+
d.length()
|
|
58
|
+
else if k == 3
|
|
59
|
+
d := V(x: 1.0, y: 2.0)
|
|
60
|
+
Int(d.x + d.y)
|
|
61
|
+
else
|
|
62
|
+
d := true
|
|
63
|
+
if d then 11 else 12
|
|
64
|
+
|
|
65
|
+
fun loopBinders(xs: List[A], ys: List[B], zs: Slice[Float]) -> Int =
|
|
66
|
+
t := Tally(n: 0)
|
|
67
|
+
for d := range xs
|
|
68
|
+
t.n = t.n + d.x
|
|
69
|
+
for d := range ys
|
|
70
|
+
t.n = t.n + d.z
|
|
71
|
+
for d := range zs
|
|
72
|
+
t.n = t.n + Int(d)
|
|
73
|
+
for d := range 3
|
|
74
|
+
t.n = t.n + d
|
|
75
|
+
if t.n > 0
|
|
76
|
+
d := A(x: 100)
|
|
77
|
+
t.n = t.n + d.x
|
|
78
|
+
t.n
|
|
79
|
+
|
|
80
|
+
fun whiles() -> Int =
|
|
81
|
+
t := Tally(n: 0)
|
|
82
|
+
while t.n < 3
|
|
83
|
+
d := A(x: 1)
|
|
84
|
+
t.n = t.n + d.x
|
|
85
|
+
while t.n < 10
|
|
86
|
+
d := B(y: "b", z: 4)
|
|
87
|
+
t.n = t.n + d.z
|
|
88
|
+
t.n
|
|
89
|
+
|
|
90
|
+
fun inClosure() -> Int =
|
|
91
|
+
f := |n: Int|
|
|
92
|
+
if n > 0
|
|
93
|
+
d := A(x: n)
|
|
94
|
+
d.x
|
|
95
|
+
else
|
|
96
|
+
d := B(y: "b", z: 3)
|
|
97
|
+
d.z
|
|
98
|
+
f(0) + f(4)
|
|
99
|
+
|
|
100
|
+
fun siblingClosures() -> Int =
|
|
101
|
+
f := |n: Int|
|
|
102
|
+
d := A(x: n)
|
|
103
|
+
d.x
|
|
104
|
+
g := |n: Int|
|
|
105
|
+
d := B(y: "b", z: n * 2)
|
|
106
|
+
d.z + d.y.length()
|
|
107
|
+
h := |d: Float| Int(d * 2.0)
|
|
108
|
+
k := |d: Str| d.length()
|
|
109
|
+
f(1) + g(2) + h(1.5) + k("four")
|
|
110
|
+
|
|
111
|
+
fun patternThenLocal(a: Option[A]) -> Int =
|
|
112
|
+
match a
|
|
113
|
+
Some(d) => d.x
|
|
114
|
+
None =>
|
|
115
|
+
d := B(y: "b", z: 5)
|
|
116
|
+
d.z
|
|
117
|
+
|
|
118
|
+
fun arms(e: E) -> Int =
|
|
119
|
+
match e
|
|
120
|
+
One(d) => d.x
|
|
121
|
+
Two(d) => d.z
|
|
122
|
+
Num(d) => Int(d * 2.0)
|
|
123
|
+
Word(d) => d.length()
|
|
124
|
+
|
|
125
|
+
fun armLocals(e: E) -> Int =
|
|
126
|
+
match e
|
|
127
|
+
One(a) =>
|
|
128
|
+
d := a
|
|
129
|
+
d.x
|
|
130
|
+
Two(b) =>
|
|
131
|
+
d := b
|
|
132
|
+
d.z
|
|
133
|
+
Num(n) =>
|
|
134
|
+
d := n
|
|
135
|
+
Int(d * 2.0)
|
|
136
|
+
Word(s) =>
|
|
137
|
+
d := s
|
|
138
|
+
d.length()
|
|
139
|
+
|
|
140
|
+
fun twoMatches(e: E, o: Option[Str]) -> Int =
|
|
141
|
+
first := match e
|
|
142
|
+
One(d) => d.x
|
|
143
|
+
_ => 0
|
|
144
|
+
second := match o
|
|
145
|
+
Some(d) => d.length()
|
|
146
|
+
None => 0
|
|
147
|
+
third := match e
|
|
148
|
+
Num(d) => Int(d)
|
|
149
|
+
_ => 0
|
|
150
|
+
first + second + third
|
|
151
|
+
|
|
152
|
+
fun ifLets(a: Option[A], b: Option[B], r: Result[Float, Str]) -> Int =
|
|
153
|
+
t := Tally(n: 0)
|
|
154
|
+
if Some(d) := a
|
|
155
|
+
t.n = t.n + d.x
|
|
156
|
+
if Some(d) := b
|
|
157
|
+
t.n = t.n + d.z
|
|
158
|
+
if Ok(d) := r
|
|
159
|
+
t.n = t.n + Int(d)
|
|
160
|
+
else
|
|
161
|
+
d := "error"
|
|
162
|
+
t.n = t.n + d.length()
|
|
163
|
+
t.n
|
|
164
|
+
|
|
165
|
+
fun nested(flag: Bool) -> Int =
|
|
166
|
+
t := Tally(n: 0)
|
|
167
|
+
for i := range 2
|
|
168
|
+
if flag
|
|
169
|
+
d := A(x: i)
|
|
170
|
+
for j := range 2
|
|
171
|
+
e := B(y: "b", z: d.x + j)
|
|
172
|
+
t.n = t.n + e.z
|
|
173
|
+
else
|
|
174
|
+
d := B(y: "bb", z: i)
|
|
175
|
+
for j := range 2
|
|
176
|
+
e := A(x: d.z + d.y.length())
|
|
177
|
+
t.n = t.n + e.x
|
|
178
|
+
t.n
|
|
179
|
+
|
|
180
|
+
fun folds() -> Int =
|
|
181
|
+
a := [1, 2, 3].fold(0, |acc, d| acc + d)
|
|
182
|
+
b := ["x", "yy"].fold(0, |acc, d| acc + d.length())
|
|
183
|
+
c := [A(x: 4)].fold(0, |acc, d| acc + d.x)
|
|
184
|
+
e := [1.5, 2.5].fold(0.0, |acc, d| acc + d)
|
|
185
|
+
a + b + c + Int(e)
|
|
186
|
+
|
|
187
|
+
fun exprBody(e: E) -> Int = match e
|
|
188
|
+
One(a) =>
|
|
189
|
+
d := a
|
|
190
|
+
d.x
|
|
191
|
+
Two(b) =>
|
|
192
|
+
d := b
|
|
193
|
+
d.z
|
|
194
|
+
Num(n) =>
|
|
195
|
+
d := n
|
|
196
|
+
Int(d * 2.0)
|
|
197
|
+
Word(s) =>
|
|
198
|
+
d := s
|
|
199
|
+
d.length()
|
|
200
|
+
|
|
201
|
+
fun calledByName(flag: Bool) -> Int =
|
|
202
|
+
if flag
|
|
203
|
+
f := |n: Int| n + 1
|
|
204
|
+
f(1)
|
|
205
|
+
else
|
|
206
|
+
f := |s: Str| s.length()
|
|
207
|
+
f("abc")
|
|
208
|
+
|
|
209
|
+
fun valueBlocks(flag: Bool) -> Int =
|
|
210
|
+
a := if flag
|
|
211
|
+
d := A(x: 1)
|
|
212
|
+
d.x
|
|
213
|
+
else
|
|
214
|
+
d := B(y: "b", z: 2)
|
|
215
|
+
d.z
|
|
216
|
+
b := if flag
|
|
217
|
+
d := 2.5
|
|
218
|
+
Int(d * 2.0)
|
|
219
|
+
else
|
|
220
|
+
d := "four"
|
|
221
|
+
d.length()
|
|
222
|
+
a * 10 + b
|
|
223
|
+
|
|
224
|
+
fun pairs(flag: Bool) -> Int =
|
|
225
|
+
if flag
|
|
226
|
+
d, e := 1, "two"
|
|
227
|
+
d + e.length()
|
|
228
|
+
else
|
|
229
|
+
d, e := "three", 4.0
|
|
230
|
+
d.length() + Int(e)
|
|
231
|
+
|
|
232
|
+
test "one local name, two types in sibling loop bodies"
|
|
233
|
+
assert loops() == 24 + 6 + 8
|
|
234
|
+
|
|
235
|
+
test "one local name, two types in sibling if branches"
|
|
236
|
+
assert branches(true) == 1
|
|
237
|
+
assert branches(false) == 2
|
|
238
|
+
assert scalarBranches(0) == 7 && scalarBranches(1) == 5 && scalarBranches(2) == 3
|
|
239
|
+
assert scalarBranches(3) == 3 && scalarBranches(4) == 11
|
|
240
|
+
|
|
241
|
+
test "loop binders with one name and several element types"
|
|
242
|
+
assert loopBinders([A(x: 1)], [B(y: "b", z: 2)], [3.0].toSlice()) == 109
|
|
243
|
+
|
|
244
|
+
test "one local name, two types in sibling while bodies"
|
|
245
|
+
assert whiles() == 11
|
|
246
|
+
|
|
247
|
+
test "one local name, two types in a closure's branches and in sibling closures"
|
|
248
|
+
assert inClosure() == 7
|
|
249
|
+
assert siblingClosures() == 1 + 5 + 3 + 4
|
|
250
|
+
|
|
251
|
+
test "a pattern binder and a local with one name and two types"
|
|
252
|
+
assert patternThenLocal(None) == 5
|
|
253
|
+
assert patternThenLocal(Some(A(x: 9))) == 9
|
|
254
|
+
|
|
255
|
+
test "match arms binding one name with four types"
|
|
256
|
+
assert arms(One(a: A(x: 1))) == 1 && arms(Two(b: B(y: "b", z: 2))) == 2
|
|
257
|
+
assert arms(Num(n: 1.5)) == 3 && arms(Word(s: "four")) == 4
|
|
258
|
+
assert armLocals(One(a: A(x: 1))) == 1 && armLocals(Two(b: B(y: "b", z: 2))) == 2
|
|
259
|
+
assert armLocals(Num(n: 1.5)) == 3 && armLocals(Word(s: "four")) == 4
|
|
260
|
+
assert twoMatches(One(a: A(x: 1)), Some("ab")) == 3
|
|
261
|
+
assert twoMatches(Num(n: 5.0), None) == 5
|
|
262
|
+
|
|
263
|
+
test "if-pattern binders with one name and three types"
|
|
264
|
+
assert ifLets(Some(A(x: 1)), Some(B(y: "b", z: 2)), Ok(3.0)) == 6
|
|
265
|
+
assert ifLets(None, None, Err("no")) == 5
|
|
266
|
+
|
|
267
|
+
test "nested sibling scopes swap the two types"
|
|
268
|
+
assert nested(true) == 4
|
|
269
|
+
assert nested(false) == 10
|
|
270
|
+
|
|
271
|
+
test "closure parameters with one name and four types"
|
|
272
|
+
assert folds() == 6 + 3 + 4 + 4
|
|
273
|
+
|
|
274
|
+
test "sibling scopes in an expression body, in block values and in a multiple declaration"
|
|
275
|
+
assert exprBody(One(a: A(x: 1))) == 1 && exprBody(Two(b: B(y: "b", z: 2))) == 2
|
|
276
|
+
assert exprBody(Num(n: 1.5)) == 3 && exprBody(Word(s: "four")) == 4
|
|
277
|
+
assert calledByName(true) == 2 && calledByName(false) == 3
|
|
278
|
+
assert valueBlocks(true) == 15 && valueBlocks(false) == 24
|
|
279
|
+
assert pairs(true) == 4 && pairs(false) == 9
|