Skip to content

Keep a Some or tuple payload read before its variable is assigned - #9531

Closed
cknitt wants to merge 1 commit into
masterfrom
alias-assigned-payload
Closed

cknitt wants to merge 1 commit into
masterfrom
alias-assigned-payload

Conversation

@cknitt

@cknitt cknitt commented Dec 4, 2026

Copy link
Copy Markdown
Member

When the optimizer knows a variable holds Some(x) or a tuple (x, _), it reads x directly instead of unwrapping the option or reading the field. That is wrong if x is assigned between the binding and the read: the read then gets the new value.

Since #9022 substituted Some(x) payloads, this miscompiles @inline functions from another module whose body uses a ref:

// Other.res
@inline
let f = (n: int, q: 'a, q2: 'a) => {
  let s = ref(q)
  let o = Some(s.contents)
  for _ in 0 to n {
    s := q2
  }
  switch o {
  | Some(v) => Some((v, s.contents))
  | None => None
  }
}

// Main.res
let viaAlias = () => Other.f(3, "a", "b")

Before, viaAlias() returned ["b", "b"]:

let s = "a";
for (let _for = 0; _for <= 3; ++_for) {
  s = "b";
}
return [s, s];

After, it returns ["a", "b"]:

let s = "a";
let o = Primitive_option.some(s);
for (let _for = 0; _for <= 3; ++_for) {
  s = "b";
}
return [Primitive_option.valFromOption(o), s];

Bodies from other modules are stored after refs became assigned variables, so inlining them brings assignments into the alias pass. Since #9482 every closed @inline function stores its body, so any project can hit this. The pass now skips the substitution when the variable is assigned. The same check guards Null.toOption(x) bindings, which are read the same way; no repro was found for those.

The new inline_assigned_payload_test runs the Some and the tuple case, and the Null variant.

Found by a review of #9527, which would make this reachable for the caller's own variables too.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq

Why:
    The alias pass replaced the payload of a variable known to be
    `Some(x)`, and a field of one known to be `(x, _)`, with `x` itself.
    When `x` is assigned between the binding and the read, the read gets
    the new value. Since #9022 substituted `Some(x)` payloads, this is
    reachable through cross-module `@inline` bodies, which are stored
    after `Lam_pass_sroa` turned their refs into assigned variables. The
    pass now keeps the binding when the variable is assigned.

JS modified (cross-module @inline):
Example:
    // Other.res
    @inline
    let f = (n: int, q: 'a, q2: 'a) => {
      let s = ref(q)
      let o = Some(s.contents)
      for _ in 0 to n {
        s := q2
      }
      switch o {
      | Some(v) => Some((v, s.contents))
      | None => None
      }
    }
    @inline
    let g = (n: int, q: 'a, q2: 'a) => {
      let s = ref(q)
      let t = (s.contents, 1)
      for _ in 0 to n {
        s := q2
      }
      let (a, _) = t
      (a, s.contents)
    }

    // Main.res
    let viaAlias = () => Other.f(3, "a", "b")
    let viaField = () => Other.g(3, "a", "b")
Before (js): both return ["b", "b"]
    function viaAlias() {
      let s = "a";
      for (let _for = 0; _for <= 3; ++_for) {
        s = "b";
      }
      return [
        s,
        s
      ];
    }
    … viaField compiles to the same
After (js): both return ["a", "b"]
    function viaAlias() {
      let s = "a";
      let o = Primitive_option.some(s);
      for (let _for = 0; _for <= 3; ++_for) {
        s = "b";
      }
      return [
        Primitive_option.valFromOption(o),
        s
      ];
    }

    function viaField() {
      let s = "a";
      let t_0 = s;
      for (let _for = 0; _for <= 3; ++_for) {
        s = "b";
      }
      return [
        t_0,
        s
      ];
    }

Review: Why also guard `Js.Null.toOption(x)` bindings? They record `x` the
same way, and the unwrap and the `null` test read it later just like a
`Some(x)` payload; no repro was found there, the guard keeps it sound.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt added this pull request to stack #9532 December 4, 2026 22:20
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix the payload of a Some or a tuple field, made from a variable that is assigned later, giving the variable's new value.

Semantic Diff

1 commit · Full page

Keep a Some or tuple payload read before its variable is assigned

a507341 · cknitt + Claude · Dec 04, 2026, 21:39 UTC

Why

The alias pass replaced the payload of a variable known to be Some(x), and a field of one known to be (x, _), with x itself. When x is assigned between the binding and the read, the read gets the new value. Since #9022 substituted Some(x) payloads, this is reachable through cross-module @inline bodies, which are stored after Lam_pass_sroa turned their refs into assigned variables. The pass now keeps the binding when the variable is assigned.

JS modified (cross-module @inline)

// Other.res
@inline
let f = (n: int, q: 'a, q2: 'a) => {
  let s = ref(q)
  let o = Some(s.contents)
  for _ in 0 to n {
    s := q2
  }
  switch o {
  | Some(v) => Some((v, s.contents))
  | None => None
  }
}
@inline
let g = (n: int, q: 'a, q2: 'a) => {
  let s = ref(q)
  let t = (s.contents, 1)
  for _ in 0 to n {
    s := q2
  }
  let (a, _) = t
  (a, s.contents)
}

// Main.res
let viaAlias = () => Other.f(3, "a", "b")
let viaField = () => Other.g(3, "a", "b")

js · Before: both return ["b", "b"] · After: both return ["a", "b"]

 function viaAlias() {
   let s = "a";
+  let o = Primitive_option.some(s);
   for (let _for = 0; _for <= 3; ++_for) {
     s = "b";
   }
   return [
-    s,
+    Primitive_option.valFromOption(o),
     s
   ];
 }
-… viaField compiles to the same
+
+function viaField() {
+  let s = "a";
+  let t_0 = s;
+  for (let _for = 0; _for <= 3; ++_for) {
+    s = "b";
+  }
+  return [
+    t_0,
+    s
+  ];
+}

Review

  • Why also guard Js.Null.toOption(x) bindings? They record x the same way, and the unwrap and the null test read it later just like a Some(x) payload; no repro was found there, the guard keeps it sound.

@codecov

codecov Bot commented Dec 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.68%. Comparing base (2796a44) to head (a507341).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9531   +/-   ##
=======================================
  Coverage   82.67%   82.68%           
=======================================
  Files         469      469           
  Lines       66553    66595   +42     
=======================================
+ Hits        55023    55064   +41     
- Misses      11530    11531    +1     
Files with missing lines Coverage Δ
compiler/backend/lam_pass_remove_alias.ml 93.57% <100.00%> (+0.33%) ⬆️

... and 6 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.

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 656,878,220 → 656,895,553 (±0.00%).

Generated projects: 629,102,653 → 629,123,317 (±0.00%).

None of the 265 benchmarks changed by 0.1% or more.

Instructions

In total: 8,976,915,055 → 8,977,171,177 (±0.00%).

None of the 33 benchmarks changed by 0.1% or more.

Output size

In total: 27,594,641 → 27,594,872 (±0.00%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request a507341 compared with base 4375d6e in this run.

@pkg-pr-new

pkg-pr-new Bot commented Dec 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: a507341

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

@cknitt

cknitt commented Dec 5, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 5, 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-12-05T07:20:36.497103Z a507341 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. Keep it up!

Reviewed commit: a507341c7e

ℹ️ 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 added a commit that referenced this pull request Dec 5, 2026
Why:
    `Lam_pass_sroa` turns a local ref or mutable record into a variable
    that `Lassign` updates, and an `@inline` body is stored after it.
    Other modules inline such a body early in their pipeline, in passes
    that assume no variable is assigned before their own sroa. The
    reassigned-variable bugs of #9531 and #9533, and earlier #9424 and
    #9284, all came from such bodies. Other modules now call these
    functions, and warning 113 says why. No `@inline` function of the
    runtime or Belt updates a ref, and storing `@inline` bodies by default
    (#9482 and before it) has not been released: v13.0.0-alpha.7 stored
    them only with `-cross-module-opt`. Inlining while compiling to
    JavaScript still sees the caller's own assigned variables, so the
    guards of #9531 and #9533 stay.

Diagnostic added (exported @inline function that updates a local ref):
Example:
    @inline
    let sumTo = n => {
      let total = ref(0)
      for i in 0 to n {
        total := total.contents + i
      }
      total.contents
    }
Before (diagnostic): none
After (diagnostic):
    Warning number 113

    This @inline function is inlined only in this module, not in modules that use it: it updates a local ref or mutable record.

JS modified (cross-module @inline that updates a ref):
Example:
    // Other.res
    @inline
    let f = (n: int, q: 'a, q2: 'a) => {
      let s = ref(q)
      let t = (s.contents, 1)
      for _ in 0 to n {
        s := q2
      }
      let (a, _) = t
      (a, s.contents)
    }

    // Main.res
    let main = (n, q) => Other.f(n, q, "b")
Before (js): main(3, "a") returns ["b", "b"]
    function main(n, q) {
      let s = q;
      for (let _for = 0; _for <= n; ++_for) {
        s = "b";
      }
      return [
        s,
        s
      ];
    }
After (js): main(3, "a") returns ["a", "b"]
    function main(n, q) {
      return Other.f(n, q, "b");
    }

Review: Which guards are still needed? Tests with refs in the caller,
passed to an `@inline` function as arguments next to a callback that
assigns them, fail without the `stable` check of #9531 and without its
module-field extension in #9533. Removing #9533's `refine_let` check
changes none of them; it is kept as it is sound and cheap.

Review: How does this stay true? With `-check-lam`, which the compiler
tests use, `Lam_check.no_assign` fails right before `Lam_pass_sroa` if
the program assigns a variable. The passes before it never remove an
assignment, so one check catches any pass that brings one in. Without
this restriction, inlining a ref-updating `@inline` function from
another module fails it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt removed this pull request from stack #9532 December 5, 2026 10:18
@cknitt

cknitt commented Dec 5, 2026

Copy link
Copy Markdown
Member Author

Superseded by stack #9541. #9535 now stops storing @inline bodies that update a local ref, so no assigned variable reaches the alias pass and this guard has nothing left to catch. The Some payload repro moved into #9535's tests, where it compiles to a call. (Comment by Claude.)

@cknitt cknitt closed this Dec 5, 2026
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.

1 participant