plum

#treesitter#compiler#wasm

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

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


5b345ac — Peter John 2026-11-10T11:24:51+05:30
wasm codegen: no test on the last case of an exhaustive match
compiler/wasm-codegen/src/matching.rs CHANGED
@@ -676,10 +676,18 @@ pub(crate) fn compileVariantConstructorArm(
676
676
 
677
677
  // A single ref.test against the variant's exact concrete type replaces the old
678
678
  // "range-check against HEAP_BASE, then conditionally load+compare a tag" dance.
679
+ // When this is the last case of a match proven exhaustive, a failed test could only
680
+ // reach the `unreachable` of "no case left", so the test goes: the cast below traps
681
+ // in its place.
682
+ let tested = !(rest.is_empty() && exhaustive_fallback);
683
+ let _tg = if tested {
679
- Instruction::LocalGet(scratch_local).encode(body);
684
+ Instruction::LocalGet(scratch_local).encode(body);
680
- Instruction::RefTestNonNull(HeapType::Concrete(variant_idx)).encode(body);
685
+ Instruction::RefTestNonNull(HeapType::Concrete(variant_idx)).encode(body);
681
- Instruction::If(blockTypeFor(result_vt)).encode(body);
686
+ Instruction::If(blockTypeFor(result_vt)).encode(body);
682
- let _tg = TailDepth::enter(&ctx.tail_depth);
687
+ Some(TailDepth::enter(&ctx.tail_depth))
688
+ } else {
689
+ None
690
+ };
683
691
  // Narrow the subject down to this variant's concrete type before destructuring
684
692
  // its fields — valid here specifically because the ref.test just above proved it.
685
693
  Instruction::LocalGet(scratch_local).encode(body);
@@ -715,17 +723,19 @@ pub(crate) fn compileVariantConstructorArm(
715
723
  state,
716
724
  &mut onMatch,
717
725
  )?;
726
+ if tested {
718
- Instruction::Else.encode(body);
727
+ Instruction::Else.encode(body);
719
- compileMatchArmsMulti(
728
+ compileMatchArmsMulti(
720
- rest,
729
+ rest,
721
- all_subjects,
730
+ all_subjects,
722
- result_vt,
731
+ result_vt,
723
- exhaustive_fallback,
732
+ exhaustive_fallback,
724
- body,
733
+ body,
725
- ctx,
734
+ ctx,
726
- state,
735
+ state,
727
- )?;
736
+ )?;
728
- Instruction::End.encode(body);
737
+ Instruction::End.encode(body);
738
+ }
729
739
  Ok(())
730
740
  }
731
741
 
compiler/wasm-codegen/tests/wasm_codegen/codegen_tests.rs CHANGED
@@ -257,6 +257,16 @@ fn stringInterpolationOfAFloatIsAClearError() {
257
257
  assert!(err.contains("Float"), "got: {}", err);
258
258
  }
259
259
 
260
+ /// The last case of a match proven exhaustive is not tested: two variants, one `ref.test`.
261
+ #[test]
262
+ fn lastCaseOfAnExhaustiveMatchIsNotTested() {
263
+ let src = "import std/Int\n\nenum Shape =\n | Circle(r: Int)\n | Rect(w: Int, h: Int)\n\nfun areaOf(s: Shape) -> Int =\n match s\n Circle(r) => r * r * 3\n Rect(w, h) => w * h\n\nfun main() -> Int =\n areaOf(Rect(w: 2, h: 5)) + areaOf(Circle(r: 1))\n";
264
+ let bytes = plum_test_support::compile("lastcase", src);
265
+ let ops = namedBodies(&bytes).into_iter().find(|(n, _)| n == "areaOf").expect("areaOf").1;
266
+ assert_eq!(ops.iter().filter(|o| o.starts_with("RefTest")).count(), 1, "{ops:?}");
267
+ assert_eq!(plum_test_support::runInt("lastcase", src), 13);
268
+ }
269
+
260
270
  /// String and float literal patterns compile (`tests/core/literal_patterns.plum` runs them).
261
271
  #[test]
262
272
  fn matchStringAndFloatPatternsCompile() {