Repository navigation
Conversation
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>
Changelog🐛 Bug fix
Semantic Diff1 commit · Full page Keep a Some or tuple payload read before its variable is assigneda507341 · cknitt + Claude · Dec 04, 2026, 21:39 UTC
JS modified (cross-module // 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")
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
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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
🚀 New features to boost your workflow:
|
✅ Compiler benchmarksAllocated wordsIn 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. InstructionsIn total: 8,976,915,055 → 8,977,171,177 (±0.00%). None of the 33 benchmarks changed by 0.1% or more. Output sizeIn 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. |
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9531 |
|
@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. Keep it up! 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". |
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>
When the optimizer knows a variable holds
Some(x)or a tuple(x, _), it readsxdirectly instead of unwrapping the option or reading the field. That is wrong ifxis assigned between the binding and the read: the read then gets the new value.Since #9022 substituted
Some(x)payloads, this miscompiles@inlinefunctions from another module whose body uses aref:Before,
viaAlias()returned["b", "b"]:After, it returns
["a", "b"]:Bodies from other modules are stored after refs became assigned variables, so inlining them brings assignments into the alias pass. Since #9482 every closed
@inlinefunction stores its body, so any project can hit this. The pass now skips the substitution when the variable is assigned. The same check guardsNull.toOption(x)bindings, which are read the same way; no repro was found for those.The new
inline_assigned_payload_testruns theSomeand the tuple case, and theNullvariant.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