From a507341c7eec0ed782321bc9ed3e5165120b9433 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Fri, 4 Dec 2026 21:39:55 +0000 Subject: [PATCH] Keep a Some or tuple payload read before its variable is assigned MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq Signed-off-by: Christoph Knittel --- changes/inline-assigned-payload.fix.md | 1 + compiler/backend/lam_pass_remove_alias.ml | 28 ++++-- .../src/inline_assigned_payload_lib.mjs | 47 ++++++++++ .../src/inline_assigned_payload_lib.res | 39 ++++++++ .../src/inline_assigned_payload_test.mjs | 88 +++++++++++++++++++ .../src/inline_assigned_payload_test.res | 25 ++++++ 6 files changed, 219 insertions(+), 9 deletions(-) create mode 100644 changes/inline-assigned-payload.fix.md create mode 100644 tests/compiler_tests/src/inline_assigned_payload_lib.mjs create mode 100644 tests/compiler_tests/src/inline_assigned_payload_lib.res create mode 100644 tests/compiler_tests/src/inline_assigned_payload_test.mjs create mode 100644 tests/compiler_tests/src/inline_assigned_payload_test.res diff --git a/changes/inline-assigned-payload.fix.md b/changes/inline-assigned-payload.fix.md new file mode 100644 index 0000000000..9b45799634 --- /dev/null +++ b/changes/inline-assigned-payload.fix.md @@ -0,0 +1 @@ +- Fix the payload of a `Some` or a tuple field, made from a variable that is assigned later, giving the variable's new value. diff --git a/compiler/backend/lam_pass_remove_alias.ml b/compiler/backend/lam_pass_remove_alias.ml index e4b41b0ae8..888e21aaff 100644 --- a/compiler/backend/lam_pass_remove_alias.ml +++ b/compiler/backend/lam_pass_remove_alias.ml @@ -69,6 +69,14 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = Set_ident.union !assignable (Lambda_traverse.assigned_variables lam); lam in + (* What [meta.ident_tbl] records about a binding, like [Some(x)] or + [(x, 1)], gives [x] as it was when the binding was made. Moving a read of + [x] to a later use is wrong once something assigns [x] in between. *) + let stable (lam : Lambda.t) = + match lam with + | Lvar x -> not (Set_ident.mem x !assignable) + | _ -> true + in let inline_depth = ref 0 in let rec simpl (lam : Lambda.t) : Lambda.t = match lam with @@ -89,11 +97,13 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = (* ATTENTION: Main use case, we should detect inline all immutable block .. *) match simpl arg with - | Lvar v as l -> - Lam_util.field_flatten_get - (fun _ -> - if l == arg then lam else Lambda.prim ~primitive ~args:[l] loc) - v i info meta.ident_tbl + | Lvar v as l -> ( + let keep _ = + if l == arg then lam else Lambda.prim ~primitive ~args:[l] loc + in + match Lam_util.field_flatten_get keep v i info meta.ident_tbl with + | field when stable field -> field + | _ -> keep ()) | l -> if l == arg then lam else Lambda.prim ~primitive ~args:[l] loc) | Lprim { @@ -101,10 +111,10 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = args = [(Lvar v as lvar)]; } as x -> ( match Hash_ident.find_opt meta.ident_tbl v with - | Some (OptionalBlock (l, _)) -> l + | Some (OptionalBlock (l, _)) when stable l -> l (* [v] bound to [Some(l)]: unwrapping gives back [l], whether or not it was nested. Only substitute forms that are safe to repeat. *) - | Some (Normal_optional (Lvar _ as l)) -> l + | Some (Normal_optional (Lvar _ as l)) when stable l -> l | Some (Normal_optional (Lconst c as l)) when not (Lambda.const_is_allocating c) -> l @@ -114,12 +124,12 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = match Hash_ident.find_opt meta.ident_tbl id with | Some (Constant c) when is_const_some c -> simpl l2 | Some (ImmutableBlock _ | Normal_optional _) -> simpl l2 - | Some (OptionalBlock (l, Null)) -> + | Some (OptionalBlock (l, Null)) when stable l -> Lambda.if_ (Lambda.not_ Location.none (Lambda.prim ~primitive:Pis_null ~args:[l] Location.none)) (simpl l2) (simpl l3) - | Some (OptionalBlock (l, Null_undefined)) -> + | Some (OptionalBlock (l, Null_undefined)) when stable l -> Lambda.if_ (Lambda.not_ Location.none (Lambda.prim ~primitive:Pis_null_undefined ~args:[l] Location.none)) diff --git a/tests/compiler_tests/src/inline_assigned_payload_lib.mjs b/tests/compiler_tests/src/inline_assigned_payload_lib.mjs new file mode 100644 index 0000000000..6a58953438 --- /dev/null +++ b/tests/compiler_tests/src/inline_assigned_payload_lib.mjs @@ -0,0 +1,47 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.mjs"; + +function someBeforeAssign(n, q, q2) { + let s = q; + let o = Primitive_option.some(s); + for (let _for = 0; _for <= n; ++_for) { + s = q2; + } + return [ + Primitive_option.valFromOption(o), + s + ]; +} + +function tupleBeforeAssign(n, q, q2) { + let s = q; + let t_0 = s; + for (let _for = 0; _for <= n; ++_for) { + s = q2; + } + return [ + t_0, + s + ]; +} + +function nullableBeforeAssign(n, q, q2) { + let s = q; + let o = s; + for (let _for = 0; _for <= n; ++_for) { + s = q2; + } + if (o == null) { + return "none"; + } else { + return o; + } +} + +export { + someBeforeAssign, + tupleBeforeAssign, + nullableBeforeAssign, +} +/* No side effect */ diff --git a/tests/compiler_tests/src/inline_assigned_payload_lib.res b/tests/compiler_tests/src/inline_assigned_payload_lib.res new file mode 100644 index 0000000000..4cd99e717d --- /dev/null +++ b/tests/compiler_tests/src/inline_assigned_payload_lib.res @@ -0,0 +1,39 @@ +// Inlined by inline_assigned_payload_test.res. Each function is stored +// optimized, so [s] is a variable the loop assigns: the payload [o] or the +// field of [t] must keep the value [s] had before the loop. +@inline +let someBeforeAssign = (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 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) +} + +@inline +let nullableBeforeAssign = (n: int, q: Nullable.t, q2: Nullable.t) => { + let s = ref(q) + let o = Nullable.toOption(s.contents) + for _ in 0 to n { + s := q2 + } + switch o { + | Some(v) => v + | None => "none" + } +} diff --git a/tests/compiler_tests/src/inline_assigned_payload_test.mjs b/tests/compiler_tests/src/inline_assigned_payload_test.mjs new file mode 100644 index 0000000000..13da510c82 --- /dev/null +++ b/tests/compiler_tests/src/inline_assigned_payload_test.mjs @@ -0,0 +1,88 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Mocha from "mocha"; +import * as Test_utils from "./test_utils.mjs"; +import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.mjs"; + +function someBeforeAssign() { + 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 tupleBeforeAssign() { + let s = "a"; + let t_0 = s; + for (let _for = 0; _for <= 3; ++_for) { + s = "b"; + } + return [ + t_0, + s + ]; +} + +function nullableBeforeAssign() { + let s = "a"; + let o = s; + for (let _for = 0; _for <= 3; ++_for) { + s = null; + } + if (o == null) { + return "none"; + } else { + return o; + } +} + +Mocha.describe("Inline_assigned_payload_test", () => { + Mocha.test("the payload of a Some, made before its variable is assigned", () => { + let s = "a"; + let o = Primitive_option.some(s); + for (let _for = 0; _for <= 3; ++_for) { + s = "b"; + } + Test_utils.eq("File \"inline_assigned_payload_test.res\", line 15, characters 7-14", [ + "a", + "b" + ], [ + Primitive_option.valFromOption(o), + s + ]); + }); + Mocha.test("the field of a tuple, made before its variable is assigned", () => { + let s = "a"; + let t_0 = s; + for (let _for = 0; _for <= 3; ++_for) { + s = "b"; + } + Test_utils.eq("File \"inline_assigned_payload_test.res\", line 19, characters 7-14", [ + "a", + "b" + ], [ + t_0, + s + ]); + }); + Mocha.test("a nullable converted to an option before its variable is assigned", () => { + let s = "a"; + let o = s; + for (let _for = 0; _for <= 3; ++_for) { + s = null; + } + Test_utils.eq("File \"inline_assigned_payload_test.res\", line 23, characters 7-14", "a", o == null ? "none" : o); + }); +}); + +export { + someBeforeAssign, + tupleBeforeAssign, + nullableBeforeAssign, +} +/* Not a pure module */ diff --git a/tests/compiler_tests/src/inline_assigned_payload_test.res b/tests/compiler_tests/src/inline_assigned_payload_test.res new file mode 100644 index 0000000000..bbe6660d5e --- /dev/null +++ b/tests/compiler_tests/src/inline_assigned_payload_test.res @@ -0,0 +1,25 @@ +// A [Some] or a tuple made from a variable that is assigned later must keep +// the value the variable had when it was made. +open Mocha +open Test_utils + +let someBeforeAssign = () => Inline_assigned_payload_lib.someBeforeAssign(3, "a", "b") + +let tupleBeforeAssign = () => Inline_assigned_payload_lib.tupleBeforeAssign(3, "a", "b") + +let nullableBeforeAssign = () => + Inline_assigned_payload_lib.nullableBeforeAssign(3, Nullable.make("a"), Nullable.null) + +describe(__MODULE__, () => { + test("the payload of a Some, made before its variable is assigned", () => { + eq(__LOC__, Some(("a", "b")), someBeforeAssign()) + }) + + test("the field of a tuple, made before its variable is assigned", () => { + eq(__LOC__, ("a", "b"), tupleBeforeAssign()) + }) + + test("a nullable converted to an option before its variable is assigned", () => { + eq(__LOC__, "a", nullableBeforeAssign()) + }) +})