plum

#treesitter#compiler#wasm

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

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


0c0fbf1 — Peter John 2026-11-10T21:30:47+05:30
Codegen: a match the checker proved exhaustive never fails to compile
compiler/wasm-codegen/src/gctypes.rs CHANGED
@@ -93,8 +93,7 @@ pub struct GcTypeRegistry {
93
93
  /// `compileSource`), so `0` until then (nothing reads it earlier).
94
94
  pub closure_type_idx: u32,
95
95
  /// The single shared `array<anyref>` type index every `Array<T>`
96
- /// uses regardless of its own
96
+ /// uses regardless of its own `T`. `0` (the `Str` slot,
97
- /// `T` — see `libs/std/array.plum`'s header comment. `0` (the `Str` slot,
98
97
  /// harmlessly never read as such) until/unless the program actually
99
98
  /// instantiates `Array` at least once.
100
99
  pub array_type_idx: u32,
compiler/wasm-codegen/src/matching.rs CHANGED
@@ -68,24 +68,14 @@ pub(crate) fn exprHasResult(expr: &ast::Expr, ctx: &LocalCtx) -> bool {
68
68
  }
69
69
  }
70
70
 
71
- /// True if `cases` consists solely of enum-tag patterns (bare variant names or
72
- /// constructor patterns, no wildcard/binding/int/etc.) that between them cover every
73
- /// variant of a single enum type. When that holds, a match compiled in value position
74
- /// can never actually fall through past the last arm at runtime — even though the
75
- /// patterns don't include an explicit wildcard/binding catch-all — so the "ran out of
76
- /// patterns" fallback in `compile_match_arms` is provably unreachable code, not a real
77
- /// gap. `compileMatch` uses this to append a synthetic trap-and-never-fall-through
78
- /// wildcard arm (rather than let the arms recursion hit its non-exhaustive-match error)
79
- /// so previously-working exhaustive enum matches (e.g. `Next`/`End`)
80
- /// keep compiling even without a trailing wildcard, while a genuinely non-exhaustive
81
- /// match (an `Int` match, or an enum match missing a variant) still gets a clear error.
82
71
  /// True if `cases` already covers every combination of enum variants across all
83
72
  /// subject positions (by explicit tag/constructor patterns only — no binding or
84
73
  /// wildcard in any position), i.e. the match is exhaustive at runtime even though
85
74
  /// `compileMatchArmsMulti` can't see that from the remaining-cases slice alone.
86
75
  /// For a single subject this is "every variant of its enum is named somewhere";
87
- /// for `match a, b, ...` it's the full cross product (e.g. `Bool, Bool` needs all
76
+ /// for `match a, b, ...` it's the full cross product (two subjects of a two-variant
88
- /// 4 combinations named, matching `libs/std/bool.plum`'s `and`/`or`).
77
+ /// enum need all 4 combinations named). `compileMatch` then compiles the "no case
78
+ /// left" path as an `unreachable` trap, and the last arm untested.
89
79
  pub(crate) fn matchCoversEveryEnumVariant(
90
80
  cases: &[ast::Case],
91
81
  subject_vts: &[ValType],
@@ -272,16 +262,13 @@ pub(crate) fn compileMatchArmsMulti(
272
262
  ) -> Result<(), String> {
273
263
  let (case, rest) = match cases.split_first() {
274
264
  None => {
265
+ // The checker proved every match exhaustive (`plum_checker`'s "non-exhaustive
266
+ // match" error), also one whose coverage `exhaustive_fallback` can't see from
267
+ // the tags (`_, R` arms across two subjects): no value gets here.
275
- return match result_vt {
268
+ if result_vt.is_some() {
276
- Some(_) if exhaustive_fallback => {
277
- Instruction::Unreachable.encode(body);
269
+ Instruction::Unreachable.encode(body);
278
- Ok(())
279
- }
270
+ }
280
- Some(_) => Err(
281
- "codegen: function has a control-flow path that doesn't produce a return value (non-exhaustive match)".to_string(),
282
- ),
283
- None => Ok(()),
271
+ return Ok(());
284
- };
285
272
  }
286
273
  Some(pair) => pair,
287
274
  };
tests/core/exhaustive_matches.plum ADDED
@@ -0,0 +1,59 @@
1
+ # Matches the checker proves exhaustive compile whatever mix of variants, wildcards and
2
+ # nested patterns covers them. Codegen used to see coverage only when every arm named a
3
+ # variant at every position, and failed `f2` below with "non-exhaustive match".
4
+ import std/Int
5
+ import std/Str
6
+ import std/Option
7
+ import std/Result
8
+
9
+ enum C = R | G | B
10
+
11
+ enum Tr<T> =
12
+ | Lf
13
+ | Nd(l: Tr<T>, v: T, r: Tr<T>)
14
+
15
+ fun f1(o: Option<Result<Int, C>>) -> Int = match o
16
+ Some(Ok(n)) => n
17
+ Some(Err(R)) => 100
18
+ Some(Err(G)) => 200
19
+ Some(Err(B)) => 300
20
+ None => 0
21
+
22
+ fun f2(a: C, b: C) -> Int = match a, b
23
+ R, R => 1
24
+ R, _ => 2
25
+ G, B => 3
26
+ _, R => 4
27
+ _, G => 5
28
+ B, B => 6
29
+
30
+ fun f3<T>(t: Tr<T>) -> Int = match t
31
+ Nd(Lf, _, Lf) => 1
32
+ Nd(l, _, r) => f3(l) + f3(r) + 1
33
+ Lf => 0
34
+
35
+ fun f5(o: Option<C>) -> Int = match o
36
+ Some(R) => 1
37
+ None => 2
38
+ Some(_) => 3
39
+
40
+ test "wildcards across two subjects cover the rest"
41
+ assert f2(R, R) == 1
42
+ assert f2(R, B) == 2
43
+ assert f2(G, B) == 3
44
+ assert f2(G, R) == 4
45
+ assert f2(B, G) == 5
46
+ assert f2(G, G) == 5
47
+ assert f2(B, B) == 6
48
+
49
+ test "nested variants, a generic enum and a wildcard payload"
50
+ assert f1(Some(Ok(7))) == 7
51
+ assert f1(Some(Err(G))) == 200
52
+ assert f1(Some(Err(B))) == 300
53
+ assert f1(None) == 0
54
+ t: Tr<Str> := Nd(l: Nd(l: Lf, v: "a", r: Lf), v: "b", r: Lf)
55
+ assert f3(t) == 2
56
+ assert f3(Nd(l: Lf, v: 1, r: Lf)) == 1
57
+ assert f5(Some(R)) == 1
58
+ assert f5(Some(B)) == 3
59
+ assert f5(None) == 2