Don't store @inline bodies that update a local ref
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. So a Some payload, a tuple field or a module field made from the ref could be read after the ref was updated: the Some payload case since #9022, the others since BuckleScript. 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.
addedDiagnostic exported @inline function that updates a local ref
@inlinelet sumTo = n => { let total = ref(0) for i in 0 to n { total := total.contents + i } total.contents}
diagnostic
Before
After
none 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.
modifiedJS cross-module @inline that updates a ref
// Other.res@inlinelet 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.reslet main = () => Other.f(3, "a", "b")
js
Before returns ["b", "b"]
After returns ["a", "b"]
function main() {function main() { let s = "a"; for (let _for = 0; _for <= 3; ++_for) { s = "b"; } return [ s, s ]; return Other.f(3, "a", "b"); }}
Fixes #9533
Review
- Which cases do the tests cover? The warning fixture gets
sumTo. The ref-updating@inlinehelpers in the cross-module tests, and the repros of #9533 and theSomepayload, are removed: they compile to calls now, and keeping them@inlinewould need a warning suppression. What stays are refs of the caller, assigned by a callback passed next to an argument made from them. Those bodies are still inlined, and were not affected; their tests keep it that way. - How does this stay true? With
-check-lam, which the compiler tests use,Lam_check.checkruns after every pass and now also fails on an assignment beforeLam_pass_sroa, naming the pass that brought it in. Without this restriction, inlining a ref-updating@inlinefunction from another module fails it.