plum

#treesitter#compiler#wasm

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

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


caea994 — Peter John 2026-11-10T11:25:09+05:30
wasm codegen: a type missing from the GC registry is an error, not a panic
compiler/wasm-codegen/src/expr.rs CHANGED
@@ -414,7 +414,7 @@ pub(crate) fn compileExpr(
414
414
  Some(info) if info.field_types.is_empty() => {
415
415
  let ety = enumTypeOfExpr(expr, info, ctx);
416
416
  let global_idx = *ctx.singleton_globals.get(&(canonTy(&ety), n.clone()))
417
- .unwrap_or_else(|| panic!("internal codegen error: payload-free variant '{}' has no singleton global", n));
417
+ .ok_or_else(|| format!("internal codegen error: payload-free variant '{}' of {} has no singleton global", n, ety))?;
418
418
  Instruction::GlobalGet(global_idx).encode(body);
419
419
  }
420
420
  Some(_) => {
@@ -520,7 +520,7 @@ pub(crate) fn compileExpr(
520
520
  let info = &ctx.enum_variants[&call.type_name];
521
521
  let ety = enumTypeOfExpr(expr, info, ctx);
522
522
  let global_idx = *ctx.singleton_globals.get(&(canonTy(&ety), call.type_name.clone()))
523
- .unwrap_or_else(|| panic!("internal codegen error: payload-free variant '{}' has no singleton global", call.type_name));
523
+ .ok_or_else(|| format!("internal codegen error: payload-free variant '{}' of {} has no singleton global", call.type_name, ety))?;
524
524
  Instruction::GlobalGet(global_idx).encode(body);
525
525
  }
526
526
  // A NAMED-payload enum variant construction (`Circle(radius: 5)`, as
compiler/wasm-codegen/src/gctypes.rs CHANGED
@@ -8,7 +8,10 @@ pub(crate) fn scratchValtype(ty: &PlumType, vname: &str) -> ValType {
8
8
  }
9
9
 
10
10
  /// GC type index of a pattern/`?` scratch local: variant `vname` of enum type `ty`,
11
- /// or (empty `vname`) the enum's own supertype.
11
+ /// or (empty `vname`) the enum's own supertype. A type the registry lacks (a subject whose
12
+ /// type was never recorded comes here as `Unit`) is noted like a missing type, so the
13
+ /// compile fails with that error instead of panicking; the local gets the `Str` byte
14
+ /// array's type meanwhile, which never reaches the output.
12
15
  pub(crate) fn scratchTypeIdx(r: &GcTypeRegistry, ty: &PlumType, vname: &str) -> u32 {
13
16
  let found = if vname.is_empty() {
14
17
  r.superIdx(ty).or_else(|| r.classIdx(ty))
@@ -16,10 +19,9 @@ pub(crate) fn scratchTypeIdx(r: &GcTypeRegistry, ty: &PlumType, vname: &str) ->
16
19
  r.variantIdx(ty, vname)
17
20
  };
18
21
  found.unwrap_or_else(|| {
19
- panic!(
20
- "internal codegen error: '{}' of {} missing from the GC type registry",
22
+ let msg = format!("internal codegen error: '{}' of {} missing from the GC type registry", vname, ty);
23
+ withGen(|s| s.missing_types.push(msg));
21
- vname, ty
24
+ r.byte_array_type_idx
22
- )
23
25
  })
24
26
  }
25
27
 
@@ -198,7 +200,12 @@ pub(crate) fn plumTypeToGcValtype(t: &PlumType, registry: &GcTypeRegistry) -> Va
198
200
  None if hasFreeTy(t) || !registry.known_enums.contains(n) => {
199
201
  ValType::Ref(RefType::ANYREF)
200
202
  }
203
+ // Noted like a missing type (the compile fails with it); `anyref` meanwhile.
204
+ None => {
201
- None => panic!("internal codegen error: type {} was not registered", t),
205
+ let msg = format!("internal codegen error: type {} was not registered", t);
206
+ withGen(|s| s.missing_types.push(msg));
207
+ ValType::Ref(RefType::ANYREF)
208
+ }
202
209
  },
203
210
  PlumType::TInt => ValType::I64,
204
211
  PlumType::TFloat => ValType::F64,
compiler/wasm-codegen/src/lib.rs CHANGED
@@ -173,12 +173,16 @@ mod compileEntryTests {
173
173
  const SRC: &str = "import std/Str\nimport std/Int\n\nfun main() -> Int = 1 + 2\n\ntest \"a\"\n assert 1 == 1\n";
174
174
 
175
175
  fn load() -> ast::Source {
176
+ loadSrc(SRC)
177
+ }
178
+
179
+ fn loadSrc(src: &str) -> ast::Source {
176
180
  static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
177
181
  let n = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
178
182
  let dir = std::env::temp_dir().join(format!("plum_compile_entry_{}_{}", std::process::id(), n));
179
183
  std::fs::create_dir_all(&dir).unwrap();
180
184
  let path = dir.join("main.plum");
181
- std::fs::write(&path, SRC).unwrap();
185
+ std::fs::write(&path, src).unwrap();
182
186
  plum_core::loadAndMergeDiag(&path, &plum_test_support::repoRoot()).expect("load")
183
187
  }
184
188
 
@@ -211,6 +215,19 @@ mod compileEntryTests {
211
215
  assert!(msg.contains("no type recorded"), "{msg}");
212
216
  }
213
217
 
218
+ /// The same with a `match`: its scratch local's type is looked up for the `Unit` the
219
+ /// missing type became, which is missing from the GC registry; still an error.
220
+ #[test]
221
+ fn missingTypesOfAMatchAreAnErrorNotAPanic() {
222
+ let src = loadSrc("import std/Int\nimport std/Option\n\nfun pick(o: Option[Int]) -> Int =\n match o\n Some(n) => n + 1\n None => 0\n\nfun main() -> Int = pick(Some(2)) + pick(None)\n");
223
+ let mut typed = plum_driver::check(&src, &CompileOptions::default().passes).expect("checks");
224
+ typed.expr_types.clear();
225
+ typed.inst_expr_types.clear();
226
+ typed.annotated.clear();
227
+ let Err(msg) = compileSourceInner(&src, &[], typed) else { panic!("no types") };
228
+ assert!(msg.contains("no type recorded") || msg.contains("missing from the GC type registry"), "{msg}");
229
+ }
230
+
214
231
  #[test]
215
232
  fn testsOptionNamesTests() {
216
233
  let src = load();