plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
b1bbd4b
— Peter John
2026-11-16T19:26:14+05:30
Fix two closure/match bugs: a closure binder shared by two arms is callable in each; a nullary variant arm in a specialised closure no longer matches everything
- compiler/core/src/binders.rs +1 -1
- compiler/core/src/closure_spec.rs +7 -1
- compiler/core/src/forms.rs +8 -0
- docs/backlog.md +0 -2
- std/game/Game.plum +5 -8
- std/game/ScreenSlice.plum +7 -7
- tests/core/closure_match_arms.plum +71 -0
compiler/core/src/binders.rs
CHANGED
|
@@ -176,7 +176,7 @@ fn renameBinder(p: &mut CasePattern, old: &str, new: &str) {
|
|
|
176
176
|
}
|
|
177
177
|
|
|
178
178
|
/// A binder holding a closure is called by name: `f(x)` names `f` without a `Var`.
|
|
179
|
-
fn renameCallsIn(stmts: &mut [Stmt], old: &str, new: &str) {
|
|
179
|
+
pub(crate) fn renameCallsIn(stmts: &mut [Stmt], old: &str, new: &str) {
|
|
180
180
|
fn inExpr(e: &mut Expr, old: &str, new: &str) {
|
|
181
181
|
match &mut e.kind {
|
|
182
182
|
ExprKind::FnCall(c) if c.name == old => c.name = new.to_string(),
|
compiler/core/src/closure_spec.rs
CHANGED
|
@@ -869,10 +869,16 @@ impl<'h> Ctx<'h> {
|
|
|
869
869
|
refreshBlockIds(&mut body);
|
|
870
870
|
let mut binders: HashSet<String> = cl.params.iter().cloned().collect();
|
|
871
871
|
let mut assigned: HashSet<String> = HashSet::new();
|
|
872
|
+
// A capitalised pattern name is a tag (`B =>`), not a binder: renamed, it would bind
|
|
873
|
+
// and match every value.
|
|
872
874
|
renameStmts(&mut body.stmts, &mut |n, kind| match kind {
|
|
873
|
-
Name::Decl
|
|
875
|
+
Name::Decl => {
|
|
876
|
+
binders.insert(n.clone());
|
|
877
|
+
}
|
|
878
|
+
Name::Pat if !crate::visit::isTag(n) => {
|
|
874
879
|
binders.insert(n.clone());
|
|
875
880
|
}
|
|
881
|
+
Name::Pat => {}
|
|
876
882
|
Name::Plain => {
|
|
877
883
|
assigned.insert(n.clone());
|
|
878
884
|
}
|
compiler/core/src/forms.rs
CHANGED
|
@@ -829,6 +829,14 @@ fn uniquePatternBinders(stmts: &mut [Stmt], seen: &mut HashSet<String>, counter:
|
|
|
829
829
|
renameExpr(g, &mut rn);
|
|
830
830
|
}
|
|
831
831
|
renameStmts(&mut case.body.stmts, &mut rn);
|
|
832
|
+
// A binder holding a closure is called by name (`f(x)` holds no `Var`).
|
|
833
|
+
if let Some(g) = &mut case.guard {
|
|
834
|
+
let mut one = [Stmt::Expr(std::mem::replace(g, Expr::Int(0)), 0, 0)];
|
|
835
|
+
crate::binders::renameCallsIn(&mut one, &b, &fresh);
|
|
836
|
+
let [Stmt { kind: StmtKind::Expr(back, ..), .. }] = one else { unreachable!() };
|
|
837
|
+
*g = back;
|
|
838
|
+
}
|
|
839
|
+
crate::binders::renameCallsIn(&mut case.body.stmts, &b, &fresh);
|
|
832
840
|
}
|
|
833
841
|
}
|
|
834
842
|
}
|
docs/backlog.md
CHANGED
|
@@ -6,8 +6,6 @@ Open work on `simpler-language`, rewritten 2026-11-11. Only what is still open i
|
|
|
6
6
|
|
|
7
7
|
The retained scene tree was replaced by master's screen view in functional form: a `Game(screens: ..)` of `Screen`s, a `ScreenSlice<A>` per screen with rules (master's `Slice<A>`), a `view` each frame that the game draws and hit-tests itself, commands as values for sounds and navigation (`std/game/Cmd`, returned by a slice's `update` beside its `then` actions, since 2026-11-16; one `update` and `subscriptions`, Elm style, since the same day), undo and time travel by re-simulation, and the headless kit (`docs/game.md`, "Beside master", has the table of differences). `SceneTree`, `Ctx`, `Clickable`, `Store` and `ui` are gone, with the examples that used them (`orb_run`, `kids/balloon_pop`, now `examples/games/orbitrunner` and `balloonpop`), `benchmarks/scene_trees` and two web-test stress fixtures (their results stay in `docs/benchmarks` and `compiler/game/web-test/BENCHMARK.md`). Orbit Runner's state checksum over four seeds of 3,600 ticks was master's (925878360) until its rules returned their follow-ups as `then` actions on 2026-11-16; it is 946756219 since. On 2026-11-13 it was brought up to master at e91c40ce (stages 24 and 25 and the review after 24): `View` and `Actor[View]`, actors stepped by the game through the screen's `stepped(dt)`, `Screen.hud`, `steps` and `camera`, one walk over `view`, turned boxes and text, the other mouse buttons and keys, the per-screen backgrounds and dropped pointer events of a transition (the "Beside master" table has what still differs). On 2026-11-13 (branch `sl/game-ports`) master's later framework work followed: the stepping of what a `Group` or an `Overlay` holds, frames on demand in the hosts (`frameIdle`), `plum run --dev` and its time travel keys, the window's size on the tick's input, the `Ticker`, a `Label`'s width from Arial's advance widths (in std and the browser's layout; master still estimates it) and `plum compile <folder>`. Open:
|
|
8
8
|
|
|
9
|
-
- **A closure field bound by the same name in two arms of a `match` can't be called in the second.** `match self` / `One(f) => f(x)` / `Two(f) => f(x)` on `enum E = One(f: fn(Int) -> Int) | Two(f: fn(Int) -> Int)` is "undefined function 'f'" at the second arm's call; naming the binders apart (`Two(g) => g(x)`) works. std/game's `Sub.onEvent` names each arm's binder after its subscription (`turn`, `down`, `up`, `move`).
|
|
10
|
-
- **A `match` on a nullary variant inside a closure in a function body matches every value.** `xs.fold(0, |s, e| match e` with an arm `B => s + 1` counts every element; the same match in a named function, or in a closure in a test, is right, and a variant with fields (`A(n) =>`) is right. std/game works around it (the matches are in methods: `Game.travelled`, `Game.redone`). Repro: `fun count(xs: List<E>) -> Int = xs.fold(0, |s, e| match e` / `B => s + 1` / `_ => s)` with `enum E = A(n: Int) | B | C`, `count([C])` gives 1.
|
|
11
9
|
- **Pass re-check failures met on the way** (fixed in std, not in the compiler): fusion devirtualising `s.frames().onDemand()` on a `Screen` with one implementer mistypes the fused call (std matches on `frames()` instead); dead-code removal is reverted when it removes every type a downcast to a trait could match (it met `Container as c`, which master has since folded into `view` too).
|
|
12
10
|
- **What std's delivery fold still copies** (the screen is in place since 2026-11-16, `@copy` in `Delivery.deliverTo`; since the Elm-style screens, Orbit Runner's `PlayingScreen.update` 5 (steer, the frame's spread after `stepped`, scored, hit and start over, whose `best` is read before the spread now), `rules`, `deal`, `grown`, `at` and the derived `stepped`: 29 spreads in place, 27 before; a call on a receiver of a bounded type parameter, `s.update(a)` with `S: ScreenSlice<A>`, is stamped with the bound in the analysis' copy of the body and resolves among its implementers, where it resolved to every `update` of the program, `Game.update` and `Viewport.update` among them): an update that reads `self` again after a spread of it copies (Orbit Runner reads its sounds first, `sounds := self.assets`); a screen fold whose result `passes` can't follow (`Range.fold` over the screen; Orbit Runner's `deal` folds a `Rolled<List<Orb>>` instead); Balloon Pop's `update`, one spread in place, the others copies, as its `view` keeps `self` (`s.view()` in `eventInto` then shares the copy) and its frame action spreads inside a fold's closure; an orb's and a mine's `step` (R3: one spread of `self`, the head of the list it is stepped in, `List.head`, which std's read-only walks `List.all`, `filter` and `next` hand to closures and results, and `Ticker.input` binds out of a field read; the screen's `mines` and `orbs` are also handed to the view's upcasts). The proof the fold needed: read-only parameters through calls (`readOnlyParams`: a screen's `view` hands `self` to the fused copies of its closures), a field taken out of a fresh value (rechecked by `analyse` when the field turns out shared), a value stored beside reads of itself (`Next(state: self, then: [if self.over ..])`), a local used once standing for its value in `passes`. The cost: the in-place pass takes about 2.5 times as long (debug, Orbit Runner 183 to 480 ms): 9 version rounds where there were 5, each about 30% dearer.
|
|
13
11
|
- **A program with no `Sized` or `Touch` view drops the dead code pass**: std's `Views.touchOf`, `sizedHit` and `missesArea` match `Touch as t` and `Sized as s` on a `View`, and once the pass has dropped every type that is one, the re-check calls each match impossible ("no type is both a View and a Touch"). Every game has such a view, so only a minimal test program meets it. A trait default's copy is told apart since 2026-11-16 (`defaultPositions` reads the trait's declarations), but these are statics.
|
std/game/Game.plum
CHANGED
|
@@ -230,14 +230,11 @@ struct Game(screens: List<Screen>) =
|
|
|
230
230
|
# The game after the undos, redos and rewinds the tick's commands asked for, in order
|
|
231
231
|
# (`obey` keeps them for the end of the tick), which it then forgets: what ends a tick,
|
|
232
232
|
# and the kit's `dispatch` (std/game/TestRig).
|
|
233
|
-
fun travelAsked(self, io: Io) -> Game = self.travel.fold(Game(..self, travel: Nil), |g, c|
|
|
233
|
+
fun travelAsked(self, io: Io) -> Game = self.travel.fold(Game(..self, travel: Nil), |g, c| match c
|
|
234
|
-
|
|
235
|
-
# The game after a move through its history that a command asked for.
|
|
236
|
-
fun travelled(self, io: Io, c: Cmd) -> Game = match c
|
|
237
|
-
Undo =>
|
|
234
|
+
Undo => g.undo(io)
|
|
238
|
-
Redo =>
|
|
235
|
+
Redo => g.redo(io)
|
|
239
|
-
RewindTo(k) =>
|
|
236
|
+
RewindTo(k) => g.rewindTo(io, k)
|
|
240
|
-
_ =>
|
|
237
|
+
_ => g)
|
|
241
238
|
|
|
242
239
|
# The tick itself: what `step` does live, and what a rewind does for each tick it plays
|
|
243
240
|
# again (`replayed`): then of the tick's commands only a `GoTo` is done again.
|
std/game/ScreenSlice.plum
CHANGED
|
@@ -126,14 +126,14 @@ enum Sub<A> =
|
|
|
126
126
|
fun onEvent(self, e: InputEvent, at: Vec2) -> Option<A> = match self
|
|
127
127
|
OnKeyDown(k, a) => if e.isKeyDownOf(k) then Some(a) else None
|
|
128
128
|
OnKeyUp(k, a) => if e.isKeyUpOf(k) then Some(a) else None
|
|
129
|
-
OnWheel(
|
|
129
|
+
OnWheel(act) => match e
|
|
130
|
-
Wheel(dy) => Some(
|
|
130
|
+
Wheel(dy) => Some(act(dy, at))
|
|
131
131
|
_ => None
|
|
132
|
-
OnPointerDown(
|
|
132
|
+
OnPointerDown(act) => if e.isPointerDown() then Some(act(at)) else None
|
|
133
|
-
OnPointerUp(
|
|
133
|
+
OnPointerUp(act) => if e.isPointerUp() then Some(act(at)) else None
|
|
134
|
-
OnPointerMove(
|
|
134
|
+
OnPointerMove(act) => if e.isPointerMove() then Some(act(at)) else None
|
|
135
|
-
OnButtonDown(
|
|
135
|
+
OnButtonDown(act) => if e.buttonDown() >= 0 then Some(act(e.buttonDown(), at)) else None
|
|
136
|
-
OnButtonUp(
|
|
136
|
+
OnButtonUp(act) => if e.buttonUp() >= 0 then Some(act(e.buttonUp(), at)) else None
|
|
137
137
|
_ => None
|
|
138
138
|
|
|
139
139
|
# Its action for what is held, or None.
|
tests/core/closure_match_arms.plum
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Two `match` bugs around closures (docs/backlog.md, fixed 2026-11-16):
|
|
2
|
+
# - a closure field bound by the same name in two arms was "undefined function" at the second
|
|
3
|
+
# arm's call: forms renames the second arm's binder (`f__1`) and missed calls by that name;
|
|
4
|
+
# - a nullary variant arm (`B =>`) in a closure handed to a fn (`fold`) matched every value:
|
|
5
|
+
# closure specialisation renamed the tag with the closure's binders, and `__i0_B` binds.
|
|
6
|
+
import std/Int
|
|
7
|
+
import std/collections/List
|
|
8
|
+
import std/Test
|
|
9
|
+
|
|
10
|
+
enum Op =
|
|
11
|
+
| Inc(f: fn(Int) -> Int)
|
|
12
|
+
| Dbl(f: fn(Int) -> Int)
|
|
13
|
+
| Keep
|
|
14
|
+
|
|
15
|
+
fun apply(self, x: Int) -> Int = match self
|
|
16
|
+
Inc(f) => f(x)
|
|
17
|
+
Dbl(f) => f(x) * 2
|
|
18
|
+
Keep => x
|
|
19
|
+
|
|
20
|
+
fun applyGuarded(self, x: Int) -> Int = match self
|
|
21
|
+
Inc(f) when f(x) > 10 => 100
|
|
22
|
+
Inc(f) => f(x)
|
|
23
|
+
Dbl(f) when f(x) > 10 => 200
|
|
24
|
+
Dbl(f) => f(x) * 2
|
|
25
|
+
Keep => x
|
|
26
|
+
|
|
27
|
+
enum E =
|
|
28
|
+
| A(n: Int)
|
|
29
|
+
| B
|
|
30
|
+
| C
|
|
31
|
+
|
|
32
|
+
struct Counter(z: Int) =
|
|
33
|
+
fun count(self, xs: List<E>) -> Int = xs.fold(self.z, |s, e| match e
|
|
34
|
+
B => s + 1
|
|
35
|
+
_ => s)
|
|
36
|
+
|
|
37
|
+
primitive ClosureMatchArmsTest[UnitTest] =
|
|
38
|
+
# a closure bound by the same name in two arms is called in each
|
|
39
|
+
fun aClosureBoundByTheSameNameInTwoArms() =
|
|
40
|
+
assert Inc(f: |x| x + 1).apply(1) == 2
|
|
41
|
+
assert Dbl(f: |x| x + 3).apply(2) == 10
|
|
42
|
+
assert Keep.apply(7) == 7
|
|
43
|
+
assert Inc(f: |x| x + 1).applyGuarded(20) == 100
|
|
44
|
+
assert Inc(f: |x| x + 1).applyGuarded(1) == 2
|
|
45
|
+
assert Dbl(f: |x| x + 3).applyGuarded(20) == 200
|
|
46
|
+
assert Dbl(f: |x| x + 3).applyGuarded(2) == 10
|
|
47
|
+
|
|
48
|
+
# a nullary variant arm in a closure handed to a fn matches only that variant
|
|
49
|
+
fun aNullaryVariantArmInAClosure() =
|
|
50
|
+
assert count([C]) == 0
|
|
51
|
+
assert count([B, C, A(n: 1), B]) == 2
|
|
52
|
+
assert countBlock([C, A(n: 2)]) == 0
|
|
53
|
+
assert countBlock([B]) == 1
|
|
54
|
+
assert Counter(z: 10).count([C, B]) == 11
|
|
55
|
+
assert sumA([A(n: 3), B, A(n: 4), C]) == 7
|
|
56
|
+
|
|
57
|
+
fun count(xs: List<E>) -> Int = xs.fold(0, |s, e| match e
|
|
58
|
+
B => s + 1
|
|
59
|
+
_ => s)
|
|
60
|
+
|
|
61
|
+
fun countBlock(xs: List<E>) -> Int =
|
|
62
|
+
r := xs.fold(0, |s, e| match e
|
|
63
|
+
B => s + 1
|
|
64
|
+
C => s
|
|
65
|
+
A(_) => s)
|
|
66
|
+
r
|
|
67
|
+
|
|
68
|
+
fun sumA(xs: List<E>) -> Int = xs.fold(0, |s, e| match e
|
|
69
|
+
A(n) => s + n
|
|
70
|
+
B => s
|
|
71
|
+
_ => s)
|