Semantic Diff

#9425 Read a record copy's mutable fields before overrides that can assign them

3 commits

ccfc97927987eab4a1b0b32f338f31c738eff11e..pr-9425

78fcd7bb2 · illusionalsagacity · Nov 25, 2026, 19:36 UTC

Read a record copy's mutable fields before overrides that can assign them

Why

A record spread {...base, a: e} of a record with fewer than 20 fields and no optional field, or of an array record, reads the fields of base one by one. The values were evaluated in the order the type declares the fields, so a mutable field declared after a was read after e had run, and an assignment in e, or while it awaited, showed up in the copy. A JSX spread of such props did the same. A mutable field that follows an override with side effects is now bound before all the overrides. Immutable fields, mutable fields declared before every such override, and copies whose overrides have no side effects compile as before. no_side_effects moves unchanged from Lam_analysis to Lambda_effects in compiler/typing, where the translation to Lambda can use it.

modifiedJS record spread copied field by field

type s = {a1: int, mutable b1: int}let f = (base: s) => {...base, a1: {base.b1 = 100; 5}}
js
Before
After
function f(base) {function f(base) { let b1 = base.b1; return { return { a1: (base.b1 = 100, 5), a1: (base.b1 = 100, 5), b1: base.b1 b1: b1 }; };}}

modifiedJS JSX spread of props copied field by field

@@config({flags: ["-bs-jsx", "4"]})@inline(never) let id = x => xtype props = {className: string, mutable title: string}module C = {  @react.componentWithProps  let make = (props: props) => React.string(props.title)}let jsxProps = (base: props) =>  <C {...base} className={base.title = "changed"; id("c")} />
js
Before
After
function jsxProps(base) {function jsxProps(base) { let title = base.title; return JsxRuntime.jsx(Record_copy$C, { return JsxRuntime.jsx(Record_copy$C, { className: (base.title = "changed", id("c")), className: (base.title = "changed", id("c")), title: base.title title: title }); });}}

Review

  • Why bind only some fields? A field read before the overrides in the literal already has the right value, and an immutable field can't be assigned through its own type, so binding them would only add variables to the output. A base coerced from a type where the field is mutable can still change under an immutable field; such reads stay in place.
  • Which checked-in outputs change? mario_game.mjs, where vpt is overridden by a call and four mutable fields follow it.
e3d01e0fd · illusionalsagacity · Nov 26, 2026, 22:04 UTC

Test the binding of copied mutable fields in Lambda

The record spread fix had only an end-to-end test, as Codex pointed out. A unit test now translates record spreads to raw Lambda and checks which fields are bound before the record is built and in what order: mutable fields after two calls, a mutable field before the first call, overrides without side effects, and immutable fields after a call. The first two fail without the fix; the other two are controls.

5bbbc1838 · illusionalsagacity · Nov 27, 2026, 00:04 UTC

Check which variable a copied field is read from in the Lambda test

The record spread unit test mapped a field read on any variable to the same value, so it couldn't tell a field read from the copied base from one read from the base before the copy, and it took the last binding named copy rather than the outermost one. It now records the variable of each read, checks the value each let binds, so a bound copy must read init, and stops at the first, outermost binding. An override that reads base itself is checked to read base. The two tests that failed without the fix still do.