Repository navigation
Rename the assigned variable when inlining an assignment - #9424
Conversation
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.
Lambda modified (renaming of an assignment):
Before (text): Lam_bounded_vars.rewrite
let r = x in r = r + 1; r ⇝ let r' = x in r = r' + 1; r'
After (text): Lam_bounded_vars.rewrite
let r = x in r = r + 1; r ⇝ let r' = x in r' = r' + 1; r'
a variable bound outside the body keeps its name
JS modified (inlined function that assigns a local variable):
Example:
// Lib.res
@@config({flags: ["-bs-cross-module-opt"]})
@inline
let 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)
Before (js):
function a(x, y) {
let r$1 = x;
r = r$1 + 1 | 0;
if (x > 3) {
r = (r$1 << 1);
}
let r$2 = y;
r = r$2 + 1 | 0;
if (y > 3) {
r = (r$2 << 1);
}
return r$1 + r$2 | 0;
}
After (js):
function a(x, y) {
let r = x;
r = r + 1 | 0;
if (x > 3) {
r = (r << 1);
}
let r$1 = y;
r$1 = r$1 + 1 | 0;
if (y > 3) {
r$1 = (r$1 << 1);
}
return r + r$1 | 0;
}
JS modified (constant argument, inlined in the Lambda passes):
Example:
// Use.res, with the Lib.res above
@@config({flags: ["-bs-cross-module-opt"]})
let b = () => Lib.mut(5)
Before (text):
Fatal error: exception Failure("File \"compiler/backend/lam_dce.ml\", line 33, characters 31-38r/1003 not found")
After (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.
Review: Why does the new test fail with a ReferenceError rather than the
crash? `Lam_dce` only misses the old variable when no top-level
variable of the module has its stamp, and here `addBumped` does. The
unit test checks the renaming itself.
Review: Which checked-in outputs change? None but the new test's.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Changelog🐛 Bug fix
Semantic Diff5 commits · Full page Rename the assigned variable when inlining an assignment5f21374 · illusionalsagacity · Nov 25, 2026, 19:33 UTC
Lambda modified (renaming of an assignment)
-let r = x in r = r + 1; r ⇝ let r' = x in r = r' + 1; r'
+let r = x in r = r + 1; r ⇝ let r' = x in r' = r' + 1; r'
+a variable bound outside the body keeps its nameJS modified (inlined function that assigns a local variable) // Lib.res
@@config({flags: ["-bs-cross-module-opt"]})
@inline
let 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)
function a(x, y) {
- let r$1 = x;
- r = r$1 + 1 | 0;
+ let r = x;
+ r = r + 1 | 0;
if (x > 3) {
- r = (r$1 << 1);
+ r = (r << 1);
}
- let r$2 = y;
- r = r$2 + 1 | 0;
+ let r$1 = y;
+ r$1 = r$1 + 1 | 0;
if (y > 3) {
- r = (r$2 << 1);
+ r$1 = (r$1 << 1);
}
- return r$1 + r$2 | 0;
+ return r + r$1 | 0;
}JS modified (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)
function b() {
let r = 5;
r = r + 1 | 0;
r = (r << 1);
return r;
}Review
✨ Keep the binding of a copy of an assigned variablec3dbaae · illusionalsagacity · Nov 27, 2026, 16:06 UTC · new
Lambda modified (binding a variable to an assigned one)
-let p = r in (r = 10; p + r) ⇝ r = 10; r + r
+let p = r in (r = 10; p + r) ⇝ let p = r in (r = 10; p + r)
+r: assigned by an LassignJS modified (inlined function that reads a variable before assigning it) // Lib.res
@@config({flags: ["-bs-cross-module-opt"]})
@inline
let readThenAssign = (x: int, f: int => unit => int) => {
let r = ref(x)
let g = f(r.contents)
r := 10
g() + r.contents
}
@inline
let copyThenAssign = (x: int) => {
let r = ref(x)
let s = r.contents
r := s + 1
s + r.contents
}
// Use.res
@@config({flags: ["-bs-cross-module-opt"]})
let c = () => Lib.readThenAssign(5, p => () => p)
let d = () => Lib.copyThenAssign(1)
function c() {
let r = 5;
+ let p = r;
r = 10;
- return r + r | 0;
+ return p + r | 0;
}
function d() {
let r = 1;
- r = r + 1 | 0;
- return r + r | 0;
+ let s = r;
+ r = s + 1 | 0;
+ return s + r | 0;
}Review
✨ Count the variables an inlined body assigns as assigned9f7f1d6 · illusionalsagacity · Nov 27, 2026, 16:06 UTC · new
JS modified (closure bound before it is passed to an inlined function) // Lib.res
@@config({flags: ["-bs-cross-module-opt"]})
@inline
let readThenAssign = (x: int, f: int => unit => int) => {
let r = ref(x)
let g = f(r.contents)
r := 10
g() + r.contents
}
// Use.res
@@config({flags: ["-bs-cross-module-opt"]})
let c = () => {
let capture = p => () => p
Lib.readThenAssign(5, capture)
}
function c() {
let r = 5;
+ let p = r;
r = 10;
- return r + r | 0;
+ return p + r | 0;
}Review
✨ Record nothing about the value of a variable bound by a mutable let6510a14 · illusionalsagacity · Nov 27, 2026, 16:06 UTC · new
JS modified (inlined function that tests a variable after assigning it) // Lib.res
@@config({flags: ["-bs-cross-module-opt"]})
@inline
let negated = (x: bool) => {
let r = ref(x)
r := !r.contents
r.contents ? 1 : 2
}
@inline
let matchAfterAssign = (x: string) => {
let r = ref(x)
r := "b"
switch r.contents {
| "a" => 1
| _ => 2
}
}
// Use.res
@@config({flags: ["-bs-cross-module-opt"]})
let a = () => Lib.negated(true)
function a() {
let r = true;
r = !r;
- return 1;
+ if (r) {
+ return 1;
+ } else {
+ return 2;
+ }
}JS modified (inlined function that switches on a string after assigning it) // Use.res, with the Lib.res above
@@config({flags: ["-bs-cross-module-opt"]})
let b = () => Lib.matchAfterAssign("a")
function b() {
let r = "a";
r = "b";
let match = r;
if (match === "a") {
return 1;
} else {
return 2;
}
}Review
✨ Report an assigned variable inlining can't rename instead of assertinga51ced5 · illusionalsagacity · Nov 27, 2026, 16:07 UTC · new
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #9424 +/- ##
==========================================
+ Coverage 82.21% 82.25% +0.03%
==========================================
Files 461 462 +1
Lines 65573 65622 +49
==========================================
+ Hits 53914 53979 +65
+ Misses 11659 11643 -16
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Thanks for the fix! One case still goes wrong, and it now fails silently: master crashes on it, and with this PR it compiles to the wrong result. // Lib.res
@@config({flags: ["-bs-cross-module-opt"]})
@inline
let mut2 = (x: int, f: int => unit => int) => {
let r = ref(x)
let g = f(r.contents)
r := 10
g() + r.contents
}
// Use.res
@@config({flags: ["-bs-cross-module-opt"]})
let c = () => Lib.mut2(5, p => () => p)
function c() {
let r = 5;
r = 10;
return r + r | 0;
}I think the cause is in Small thing: the This review was done by Claude. |
Why:
A binding `let p = r` was an alias: `Lam_util.refine_let` made it
one and `Lam_pass_lets_dce` replaced `p` by `r`, both for any
variable. When `r` is assigned after the binding, a read of `p`
then gives the new value. Within a module this didn't happen, since
only `Lam_pass_sroa`, after both, makes `Lassign`s, but a body
inlined from another module with `-bs-cross-module-opt` comes with
its own. A variable some `Lassign` assigns is no longer an alias.
Lambda modified (binding a variable to an assigned one):
Before (text): Lam_util.refine_let, Lam_pass_lets_dce
let p = r in (r = 10; p + r) ⇝ r = 10; r + r
After (text): Lam_util.refine_let, Lam_pass_lets_dce
let p = r in (r = 10; p + r) ⇝ let p = r in (r = 10; p + r)
r: assigned by an Lassign
JS modified (inlined function that reads a variable before assigning it):
Example:
// Lib.res
@@config({flags: ["-bs-cross-module-opt"]})
@inline
let readThenAssign = (x: int, f: int => unit => int) => {
let r = ref(x)
let g = f(r.contents)
r := 10
g() + r.contents
}
@inline
let copyThenAssign = (x: int) => {
let r = ref(x)
let s = r.contents
r := s + 1
s + r.contents
}
// Use.res
@@config({flags: ["-bs-cross-module-opt"]})
let c = () => Lib.readThenAssign(5, p => () => p)
let d = () => Lib.copyThenAssign(1)
Before (js):
function c() {
let r = 5;
r = 10;
return r + r | 0;
}
function d() {
let r = 1;
r = r + 1 | 0;
return r + r | 0;
}
After (js):
function c() {
let r = 5;
let p = r;
r = 10;
return p + r | 0;
}
function d() {
let r = 1;
let s = r;
r = s + 1 | 0;
return s + r | 0;
}
Review: Which checked-in outputs change? None but the new tests'.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Why:
`Lam_pass_remove_alias.simplify_alias` computes the variables some
`Lassign` assigns once, before it inlines. A body inlined from
another module with `-bs-cross-module-opt` may assign variables,
renamed by the inlining, so they were missing. Inlining a call in
that body then moved a read of one past an assignment. The variables
an inlined body assigns are now added before it is simplified
further, as `Lam_compile.compile_external_field_apply` does for the
bodies it inlines.
JS modified (closure bound before it is passed to an inlined function):
Example:
// Lib.res
@@config({flags: ["-bs-cross-module-opt"]})
@inline
let readThenAssign = (x: int, f: int => unit => int) => {
let r = ref(x)
let g = f(r.contents)
r := 10
g() + r.contents
}
// Use.res
@@config({flags: ["-bs-cross-module-opt"]})
let c = () => {
let capture = p => () => p
Lib.readThenAssign(5, capture)
}
Before (js):
function c() {
let r = 5;
r = 10;
return r + r | 0;
}
After (js):
function c() {
let r = 5;
let p = r;
r = 10;
return p + r | 0;
}
Review: Why does a closure written in the call not need this? There the
variables are found by the next `simplify_alias` round, which
inlines the call to the closure; a bound closure is inlined in the
same round as the body.
Review: Which checked-in outputs change? None but the new test's.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Why:
`Lam_pass_collect` recorded what any `let` binds, a mutable one too,
so `Lam_pass_remove_alias` took a variable bound to a constant for
that constant even after an `Lassign`. It decided a condition on the
variable, or a string switch, by the value it started with. Within a
module no `Lassign` exists yet when these passes run, but a body
inlined from another module with `-bs-cross-module-opt` comes with
its own. A mutable `let` now records nothing about its variable.
JS modified (inlined function that tests a variable after assigning it):
Example:
// Lib.res
@@config({flags: ["-bs-cross-module-opt"]})
@inline
let negated = (x: bool) => {
let r = ref(x)
r := !r.contents
r.contents ? 1 : 2
}
@inline
let matchAfterAssign = (x: string) => {
let r = ref(x)
r := "b"
switch r.contents {
| "a" => 1
| _ => 2
}
}
// Use.res
@@config({flags: ["-bs-cross-module-opt"]})
let a = () => Lib.negated(true)
Before (js):
function a() {
let r = true;
r = !r;
return 1;
}
After (js):
function a() {
let r = true;
r = !r;
if (r) {
return 1;
} else {
return 2;
}
}
JS modified (inlined function that switches on a string after assigning it):
Example:
// Use.res, with the Lib.res above
@@config({flags: ["-bs-cross-module-opt"]})
let b = () => Lib.matchAfterAssign("a")
Before (text):
Fatal error: exception Failure("File \"compiler/backend/lam_dce.ml\", line 33, characters 31-38r/1007 not found")
After (js):
function b() {
let r = "a";
r = "b";
let match = r;
if (match === "a") {
return 1;
} else {
return 2;
}
}
Review: Which checked-in outputs change? None but the new tests'.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
`Lam_bounded_vars.rewrite` stopped with `assert false` when the variable of an `Lassign` was mapped to a term other than a variable. It can't be today, but if it ever is, the failure now names the variable. Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
|
Thanks, you were right: this case compiled to the wrong result, and there was more behind it. The function c() {
let r = 5;
let p = r;
r = 10;
return p + r | 0;
}The stale
Both now leave the binding alone when
One more case of the same kind. All three come from the same thing: before
I updated the changelog fragment and the PR description to cover the new cases. Investigated by Claude. |
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
cknitt
left a comment
There was a problem hiding this comment.
Thanks for digging further! I re-checked the four new commits: the refine_let, lets_dce and pass_collect changes look right, and simplify_alias has no other reads of the old assignable. I also tried a ref read through a record pattern (let {contents: s} = r), locally and through a cross-module inline with and without a closure. All give the right result.
This review was done by Claude.
Part of #9398 (item 39). Found while reviewing the fixes for it.
Problem
With
-bs-cross-module-opt, inlining another module's function renames the variables bound in its body.Lam_bounded_vars.rewriterenamed the binders and the reads, but not the variable of an assignment. The output assigned an undeclared variable, or the compiler crashed inLam_dce.Before:
After:
The inlined body is also the first
Lassignthe Lambda passes beforeLam_pass_sroasee in a module, and three of them treated an assigned variable like any other:Lam_util.refine_letmadelet p = ran alias, andLam_pass_lets_dcereplacedpbyr. A read ofpafterr := 10gave 10.Lam_pass_remove_alias.simplify_aliascomputed the assigned variables once, before inlining, so it missed the ones in the inlined body and moved a read of one past the assignment.Lam_pass_collectrecorded a mutablelet r = trueas the constanttrue, soif rafterr := !rtook thetruebranch. A string switch on such a variable crashed inLam_dce.Fix
Lam_bounded_vars).Lassignassigns is not an alias (Lam_util.refine_let,Lam_pass_lets_dce).simplify_aliasadds the variables an inlined body assigns before simplifying it further, asLam_compile.compile_external_field_applyalready does.letrecords nothing about its variable (Lam_pass_collect).Tests
inline_assign_cross_module_test.res, one case per problem above, and a unit test ofLam_bounded_vars.rewrite. No existing output changes.Merging
Conflicts with
fix-inline-static-exitin the unit test file only.12.x
12.x is affected too, and worse: there the cross-module test doesn't compile. The compiler's own check reports
r/1003 used before definedand stops with an assertion failure inlam_check.ml. I'll backport once this one is reviewed.The fix was written by Claude.