plum

#treesitter#compiler#wasm

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

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


79f33a4 — Peter John 2026-11-10T11:31:55+05:30
wasm codegen: a fn's closures get distinct debug names
compiler/wasm-codegen/src/driver.rs CHANGED
@@ -818,11 +818,17 @@ impl<'a> Setup<'a> {
818
818
  pub(crate) fn registerClosureFns(&mut self) {
819
819
  let Setup { module, gc_types, units, raw_closures, closure_type_indices, closure_call_types, closures, closure_asts, .. } = self;
820
820
  let gc_types = gc_types.as_mut().unwrap();
821
+ // A unit's closures are named `f.closure`, `f.closure2`, ... in source order, so a
822
+ // backtrace tells them apart.
823
+ let mut per_unit: HashMap<usize, usize> = HashMap::new();
821
824
  for (i, (ui, rc)) in raw_closures.iter().enumerate() {
822
825
  let key = closureSigKey(&rc.param_ptypes, &rc.ret_ptype, &gc_types);
823
826
  let type_idx = getClosureCallType(module, closure_call_types, &key);
824
827
  let func_idx = module.addFunction(type_idx, &[]);
828
+ let nth = per_unit.entry(*ui).or_insert(0);
829
+ *nth += 1;
830
+ let suffix = if *nth == 1 { String::new() } else { nth.to_string() };
825
- module.nameFunction(func_idx, &format!("{}.closure", units[*ui].name));
831
+ module.nameFunction(func_idx, &format!("{}.closure{suffix}", units[*ui].name));
826
832
  let table_idx = module.addTableElement(func_idx);
827
833
  closures.insert(
828
834
  (*ui, rc.ptr),
compiler/wasm-codegen/tests/wasm_codegen/optimize_test.rs CHANGED
@@ -136,3 +136,29 @@ fn functionTypesAreDeclaredOnce() {
136
136
  }
137
137
  assert!(seen.len() > 3, "{seen:?}");
138
138
  }
139
+
140
+ /// A fn's closures get distinct names: `f.closure`, `f.closure2`.
141
+ #[test]
142
+ fn closuresOfOneFnHaveDistinctNames() {
143
+ let src = "\
144
+ import std/Int
145
+
146
+ struct Ops(f: fn(Int) -> Int, g: fn(Int) -> Int)
147
+
148
+ fun run(o: Ops, n: Int) -> Int =
149
+ if n > 1000 then n else o.f(o.g(n))
150
+
151
+ fun main() -> Int =
152
+ a := 3
153
+ b := 5
154
+ run(Ops(f: |n| n + a, g: |n| n * b), 1)
155
+ ";
156
+ let all = bodies(&compile("closurenames", src));
157
+ let closures: Vec<&str> = all.iter().map(|b| b.0.as_str()).filter(|n| n.contains(".closure")).collect();
158
+ let mut uniq = closures.clone();
159
+ uniq.sort();
160
+ uniq.dedup();
161
+ assert!(closures.len() >= 2, "{closures:?}");
162
+ assert_eq!(uniq.len(), closures.len(), "{closures:?}");
163
+ assert_eq!(runInt("closurenames", src), 8);
164
+ }