Rename the assigned variable when inlining an assignment
Why
With -bs-cross-module-opt, inlining another module's function renames the variables bound in its body (Lam_bounded_vars.rewrite). The binders and the reads were renamed, but Lassign (v, l) kept the old v, which the caller doesn't declare. The generated code then assigned an undeclared variable, a ReferenceError when run, or the compiler crashed in Lam_dce when a later Lambda pass saw the assignment. The assigned variable is now renamed too.
modifiedLambda renaming of an assignment
text
modifiedJS inlined function that assigns a local variable
// Lib.res@@config({flags: ["-bs-cross-module-opt"]})@inlinelet mut = (x: int) => { let r = ref(x) r := r.contents + 1 if x > 3 { r := r.contents * 2 } r.contents} // Use.res@@config({flags: ["-bs-cross-module-opt"]})let a = (x, y) => Lib.mut(x) + Lib.mut(y)
js
modifiedJS constant argument, inlined in the Lambda passes
// Use.res, with the Lib.res above@@config({flags: ["-bs-cross-module-opt"]})let b = () => Lib.mut(5)
text
Fatal error: exception Failure("File \"compiler/backend/lam_dce.ml\", line 33, characters 31-38r/1003 not found")js
function b() { let r = 5; r = r + 1 | 0; r = (r << 1); return r;}
Review
- Can the map hold something other than a variable for an assigned one? No: only let-bound variables are assigned, and the map replaces those with variables; only parameters map to other terms.
- Why does the new test fail with a ReferenceError rather than the crash?
Lam_dceonly misses the old variable when no top-level variable of the module has its stamp, and hereaddBumpeddoes. The unit test checks the renaming itself. - Which checked-in outputs change? None but the new test's.