Semantic Diff

#9527 Simplify bodies inlined while compiling to JavaScript

1 commit

a507341c7eec0ed782321bc9ed3e5165120b9433..pr-9527

9af8bb988 · cknitt + Claude · Dec 04, 2026, 17:58 UTC

Simplify bodies inlined while compiling to JavaScript

Why

A call to another module's function that also passes a parameter of the caller is inlined late, while compiling to JavaScript, after the alias pass has run. So the inlined body kept what that pass removes elsewhere: a Some(x) argument was bound and unwrapped again, and a function argument was called instead of inlined. The alias pass now also runs on such a body before it is compiled.

modifiedJS a literal Some next to a parameter

let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0)
js
Before
After
function knownSome(x, f) {function knownSome(x, f) { let opt = x; let opt$1 = opt !== undefined ? Primitive_option.some(f(Primitive_option.valFromOption(opt))) : undefined; if (opt$1 !== undefined) { return opt$1; let opt = Primitive_option.some(f(x)); if (opt !== undefined) { return opt; } else { } else { return 0; return 0; } }}}

modifiedJS a function argument

let mapped = (opt: option<int>) => opt->Option.map(x => x + 1)
js
Before
After
function mapped(opt) {function mapped(opt) { if (opt !== undefined) { if (opt !== undefined) { return Primitive_option.some((x => x + 1 | 0)(opt)); return Primitive_option.some(opt + 1 | 0); } }}}

Review

  • Why does the late pass get a fresh ident_tbl? The table was filled by collect_info before simplify_alias 3, simplify_lets, sroa and simplify_exits rewrote the program, so at codegen it can give a function body or a Some payload naming a variable those passes removed or turned into an assigned one: inlining a let-bound local function from it printed p + k with no k in scope. The body is now simplified knowing only the parameters it binds; the caller is still compiled with the real meta.
  • Which variables count as assigned there? The pass used to take only the ones lam assigns, but at codegen lam is just the inlined body, so a variable of the caller assigned by a call in it looked immutable and its read moved past that call. The pass now starts from meta.assignable as well. In the other direction, the pass may inline a further function that assigns a variable of its own, so the variables the body assigns are collected after the pass, before it is compiled.
  • What stays? Option.map still wraps its result with Primitive_option.some; that needs a mark on the result of the call.