Repository navigation
Description
Three optimizations move a read of a reassigned variable past the assignment.
These bugs are old, not new regressions.
- All three substitutions come from BuckleScript and predate the 2024 rename of
jscomptocompiler. - Within one module they never see a reassigned variable, because refs only become reassigned JavaScript variables after these passes run.
- Function bodies inlined from another module are stored after that step, so they can carry reassigned variables. That's how such a variable gets in.
- Until Warn when other modules can't inline an @inline function #9482 (2026-11-30), only
-bs-cross-module-optstored such bodies, which the runtime uses and user projects normally don't. Since Warn when other modules can't inline an @inline function #9482, every closed@inlinefunction stores its body, so any project can now hit these. - For case 3, Bind elements and inlined arguments read from assigned variables #9284 (2026-11-19) taught the pass about reassigned variables. But its set misses those from bodies inlined during code generation, which was already possible before.
All examples use:
// Other.res
module type S = {
let x: string
}
module A = {
let x = "a"
}
module B = {
let x = "b"
}1. A module field read from a known tuple.
// Other.res
@inline
let viaTuple = (n: int, q: module(S), q2: module(S)) => {
let s = ref(q)
let t = (
{
let module(M) = s.contents
M.x
},
1,
)
for _ in 0 to n {
s := q2
}
let (a, _) = t
let module(M2) = s.contents
(a, M2.x)
}
// Main.res
let tupleEarly = () => Other.viaTuple(3, module(Other.A), module(Other.B))tupleEarly() returns ["b", "b"] instead of ["a", "b"]: the alias pass replaces t[0] with s.x, read after the loop.
2. A module field bound once and used once.
// Other.res
let get = (m: module(S)) => {
let module(M) = m
M.x
}
@inline
let viaGet = (n: int, q: module(S), q2: module(S)) => {
let s = ref(q)
let a = get(s.contents)
for _ in 0 to n {
s := q2
}
(a, get(s.contents))
}
// Main.res
let early = () => Other.viaGet(3, module(Other.A), module(Other.B))early() returns ["b", "b"] instead of ["a", "b"]: Lam_util.refine_let substitutes s.x for a (return [s.x, s.x]).
3. A tuple element, when the function is inlined while compiling to JavaScript.
// Other.res
@inline
let tupleBeforeAssign = (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 gLate = (n, q) => Other.tupleBeforeAssign(n, q, "b")gLate(3, "a") returns ["b", "b"] instead of ["a", "b"]. The function is inlined during code generation, because its arguments are parameters of gLate. Js_pass_flatten_and_mark_dead then replaces t[0] with s. #9284 taught that pass to leave reassigned variables alone, but its set of them misses variables from bodies inlined during code generation.
Found by a review of #9531, which fixes the same problem for Some(x) payloads and plain tuple elements.
(Filed by Claude.)
Activity
Don't store @inline bodies that update a local ref
Test cross-module inlining against a twin that isn't inlined