Skip to content

Rename the assigned variable when inlining an assignment - #9424

Merged
cknitt merged 5 commits into
rescript-lang:masterfrom
illusionalsagacity:fix-inline-assign-rename
Nov 27, 2026
Merged

cknitt merged 5 commits into
rescript-lang:masterfrom
illusionalsagacity:fix-inline-assign-rename

Conversation

@illusionalsagacity

@illusionalsagacity illusionalsagacity commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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.rewrite renamed the binders and the reads, but not the variable of an assignment. The output assigned an undeclared variable, or the compiler crashed in Lam_dce.

// 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:

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:

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;
}

The inlined body is also the first Lassign the Lambda passes before Lam_pass_sroa see in a module, and three of them treated an assigned variable like any other:

  • Lam_util.refine_let made let p = r an alias, and Lam_pass_lets_dce replaced p by r. A read of p after r := 10 gave 10.
  • Lam_pass_remove_alias.simplify_alias computed 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_collect recorded a mutable let r = true as the constant true, so if r after r := !r took the true branch. A string switch on such a variable crashed in Lam_dce.
// Lib.res
@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
let c = () => Lib.readThenAssign(5, p => () => p) // 15, was 20

Fix

  • The assigned variable is renamed too (Lam_bounded_vars).
  • A variable some Lassign assigns is not an alias (Lam_util.refine_let, Lam_pass_lets_dce).
  • simplify_alias adds the variables an inlined body assigns before simplifying it further, as Lam_compile.compile_external_field_apply already does.
  • A mutable let records nothing about its variable (Lam_pass_collect).

Tests

inline_assign_cross_module_test.res, one case per problem above, and a unit test of Lam_bounded_vars.rewrite. No existing output changes.

Merging

Conflicts with fix-inline-static-exit in 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 defined and stops with an assertion failure in lam_check.ml. I'll backport once this one is reviewed.

The fix was written by Claude.

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>
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix inlining another module's function that assigns a local variable, as in let r = ref(x); r := r.contents + 1, with -bs-cross-module-opt: the compiler crashed, the generated code assigned an undeclared variable, a value read from the variable before an assignment was read after it instead, or a condition on the variable was decided by the value it started with.

Semantic Diff

5 commits · Full page

Rename the assigned variable when inlining an assignment

5f21374 · illusionalsagacity · Nov 25, 2026, 19:33 UTC

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)

text · Lam_bounded_vars.rewrite

-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 name

JS 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)

js

 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)

text · Before

Fatal error: exception Failure("File \"compiler/backend/lam_dce.ml\", line 33, characters 31-38r/1003 not found")

js · After

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_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.
  • Which checked-in outputs change? None but the new test's.
✨ Keep the binding of a copy of an assigned variable

c3dbaae · illusionalsagacity · Nov 27, 2026, 16:06 UTC · new

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 Lassigns, 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)

text · Lam_util.refine_let, Lam_pass_lets_dce

-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 Lassign

JS 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)

js

 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

  • Which checked-in outputs change? None but the new tests'.
✨ Count the variables an inlined body assigns as assigned

9f7f1d6 · illusionalsagacity · Nov 27, 2026, 16:06 UTC · new

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)

// 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)
}

js

 function c() {
   let r = 5;
+  let p = r;
   r = 10;
-  return r + r | 0;
+  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.
  • Which checked-in outputs change? None but the new test's.
✨ Record nothing about the value of a variable bound by a mutable let

6510a14 · illusionalsagacity · Nov 27, 2026, 16:06 UTC · new

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)

// 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)

js

 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")

text · Before

Fatal error: exception Failure("File \"compiler/backend/lam_dce.ml\", line 33, characters 31-38r/1007 not found")

js · After

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'.
✨ Report an assigned variable inlining can't rename instead of asserting

a51ced5 · illusionalsagacity · Nov 27, 2026, 16:07 UTC · new

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.

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.25%. Comparing base (aacaf72) to head (a51ced5).
⚠️ Report is 11 commits behind head on master.

Files with missing lines Patch % Lines
compiler/backend/lam_bounded_vars.ml 57.14% 3 Missing ⚠️
compiler/backend/lam_pass_lets_dce.ml 50.00% 1 Missing ⚠️
tests/ounit_tests/ounit_lam_bounded_vars_tests.ml 94.44% 1 Missing ⚠️
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     
Files with missing lines Coverage Δ
compiler/backend/lam_pass_collect.ml 100.00% <100.00%> (+1.51%) ⬆️
compiler/backend/lam_pass_remove_alias.ml 91.05% <100.00%> (+0.96%) ⬆️
compiler/backend/lam_util.ml 95.77% <100.00%> (+0.12%) ⬆️
compiler/backend/lam_pass_lets_dce.ml 87.17% <50.00%> (+1.46%) ⬆️
tests/ounit_tests/ounit_lam_bounded_vars_tests.ml 92.10% <94.44%> (+1.62%) ⬆️
compiler/backend/lam_bounded_vars.ml 71.60% <57.14%> (+2.27%) ⬆️

... and 14 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9424

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9424

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9424

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9424

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9424

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9424

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9424

commit: a51ced5

@cknitt

cknitt commented Nov 26, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T19:42:52.114807Z a51ced5 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 5f21374b57

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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

cknitt commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

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)

c() should be 15. With this PR it is 20:

function c() {
  let r = 5;
  r = 10;
  return r + r | 0;
}

I think the cause is in Lam_pass_remove_alias.simplify_alias. It computes meta.assignable once, before inlining. The renamed r from the inlined body isn't in it, so the read of r is substituted into p and moved after r := 10. If the closure is bound first as @inline(never) let mk = p => () => p and passed as mk, the output is right. The inlining path in lam_compile.ml already adds Lambda_traverse.assigned_variables inlined to assignable. Doing the same after the cross-module inline should fix this. Could you add this case to the tests?

Small thing: the assert false for a non-variable mapping in lam_bounded_vars.ml holds today as far as I can see, but a clear error message would be nicer than a crash if that ever changes.

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>
@illusionalsagacity

Copy link
Copy Markdown
Contributor Author

Thanks, you were right: this case compiled to the wrong result, and there was more behind it.

The mut2 case. Fixed, with the test you asked for (readThenAssign in inline_assign_cross_module_lib.res, asserting c() is 15). The output is now:

function c() {
  let r = 5;
  let p = r;
  r = 10;
  return p + r | 0;
}

The stale meta.assignable in simplify_alias was part of it but not what broke this exact case. With -debug-ir, simplify_alias already bound p = r there: the closure call is inlined in the next round, which recomputes assignable and finds r. The binding was then removed later:

  • Lam_util.refine_let made let p = r an Alias for any variable.
  • Lam_pass_lets_dce replaced p by r for any variable.

Both now leave the binding alone when r is assigned. Commit c3dbaae. The same commit tests let s = r.contents; r := s + 1; s + r.contents, which gave 4 instead of 3.

assignable in simplify_alias. Your change was needed too: if the closure is bound first (let capture = p => () => p; Lib.readThenAssign(5, capture)), it is inlined in the same round, and the result was still 20. simplify_alias now adds Lambda_traverse.assigned_variables of every inlined body before simplifying it further, like compile_external_field_apply does. Commit 9f7f1d6, with that case as a test.

One more case of the same kind. Lam_pass_collect recorded a mutable let r = true as the constant true. After r := !r, r.contents ? 1 : 2 compiled to return 1, and a string switch on such a variable crashed in Lam_dce. A mutable let now records nothing about its variable. Commit 6510a14, with both cases as tests.

All three come from the same thing: before Lam_pass_sroa, a module's Lambda has no Lassign, so these passes never had to handle one, and a body inlined from another module brings its own. No existing output changes.

assert false in lam_bounded_vars.ml. Replaced with Ext_fmt.failwithf ~loc:__LOC__, which names the variable, as Lam_dce does. Commit a51ced5.

I updated the changelog fragment and the PR description to cover the new cases.

Investigated by Claude.

@cknitt

cknitt commented Nov 27, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: a51ced59d4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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 cknitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cknitt
cknitt marked this pull request as ready for review November 27, 2026 19:50
@cknitt
cknitt merged commit 6a7e3b8 into rescript-lang:master Nov 27, 2026
23 checks passed
@illusionalsagacity
illusionalsagacity deleted the fix-inline-assign-rename branch November 27, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants