Semantic Diff

#9421 Keep the fields bound next to a record rest apart from the source's fields

3 commits

45dba2c9137d6be765c1d8d45f287d739e028498..pr-9421

aacbc8aa1 · illusionalsagacity · Nov 23, 2026, 20:40 UTC

Keep the fields bound next to a record rest apart from the source's fields

Why

After let {x, ...rest} = r, the pattern's x is the value at the destructuring and r.x is the current one. The match compiler bound x as an alias, so Lambda inlined it as a read of r.x, and the record rest pass then replaced every r.x after the rest, also those in the program, with a local bound by the destructuring. An assignment to r.x was lost, a read of r.x was stale after a write by an assignment, a method, a callee, an alias, an await or a closure, and x next to an unused rest was read after such a write.

The match compiler now binds the fields a pattern takes variables from next to a rest with StrictOpt, as it does mutable fields, so that only the program's own reads are left as reads of the source. The record rest pass replaces no reads. It moves the field reads in the declarations right before the rest into the destructuring, so that a field is not read twice; the other excluded fields get a placeholder name. These bindings keep the names of the pattern and, like other declarations, now count as bindings that shadow a JS global.

modifiedMatching fields bound next to a rest

type xy = {x: int, y: int}type onlyY = {y: int}let f = ({x, ...onlyY as rest}: xy) => (x, rest)
text
Before make_record_matching
After make_record_matching
mutable field StrictOptmutable field StrictOptimmutable field Alias immutable field bound next to a rest StrictOptother immutable field Alias

modifiedJS reads and writes of a record rest source

type mutableXy = {mutable x: int, y: int}type xy = {x: int, y: int}type onlyY = {y: int}let f = (a: mutableXy) => {  let b = (a :> xy)  let {x, ...onlyY as rest} = b  a.x = 42  (rest, x, b.x)}let g = (a: xy, k) => {  let {x, ...onlyY as _rest} = a  k()  x}
js
Before
After
function f(a) {function f(a) { let {x, ...rest} = a; let {x, ...rest} = a; x = 42; a.x = 42; return [ return [ rest, rest, x, x, x a.x ]; ];}} function g(a, k) {function g(a, k) { let x = a.x; k(); k(); return a.x; return x;}}

modifiedJS names of bound fields

type renamed = {@as("user-name") name: string, other: int}type onlyOther = {other: int}let h = ({name, ...onlyOther as rest}: renamed) => (name, rest)
js
Before
After
function h(param) {function h(param) { let {"user-name": __rest_field0, ...rest} = param; let {"user-name": name, ...rest} = param; return [ return [ __rest_field0, name, rest rest ]; ];}}

Review

  • Is a bound field always read only once? Not when its value is read before a test, as for {name: "a", ...rest} followed by a case that binds name: the destructuring, which comes after the test, names name again to leave it out of the rest. The value bound is the one tested.
  • Are fields bound by a pattern without a rest read at the match? Not always: Lambda still inlines those of immutable fields, which this commit does not change.
cebfb6706 · illusionalsagacity · Nov 26, 2026, 22:01 UTC

Keep a record rest that binds an exported field

Why

The record rest pass moves a field read into the destructuring next to it. When the interface exports that field but not the rest, and the rest's last read is dropped by inlining a call in JS, dead code elimination saw only the rest's name, which is not exported, and kept the destructuring as an expression without its bindings. The module then exported a name it never declares and failed to load. A record rest declaration is now exported when one of the fields it binds is, as it was already used when one of them is used.

Answers the Codex review finding "Preserve exported field liveness when folding declarations".

modifiedJS a record rest binding an exported field

// record_rest_hidden_rest.resi: let x: int; let sumTo: int => inttype xy = {x: int, y: int}type onlyY = {y: int}let make: unit => xy = %raw(`() => ({x: 1, y: 2})`)let {x, ...onlyY as rest} = make()let sum = (_: onlyY, n) => {  let total = ref(0)  for i in 1 to n {    total := total.contents + i  }  total.contents}let sumTo = n => sum(rest, n)
js
Before
After
let match = make();let match = make(); (({x: __unused0, ...__rest}) => __rest)(match);let {x, ...rest} = match; ……export {export { x, x, sumTo, sumTo,}}
4e0b9a4ab · illusionalsagacity · Nov 27, 2026, 21:32 UTC

Keep the order of the copies of nested record rests

Why

The record rest pass put a rest declaration back among the field reads that a later rest can move into its destructuring. For nested rests, let {inner: {i, ...innerRest}, ...rest} = o, the outer rest took let match = o.inner and so moved the inner rest's copy after its own copy of o. Copying o runs its getters: one that writes o.inner.j showed up in innerRest. A rest declaration is now left where it is, and only the reads after it can move past later rests, so the copies run in the order the match compiler gives. A field read before an earlier rest is now read again by a later rest's destructuring, as before the pass moved reads.

Answers the Codex review finding "Preserve copy order when folding nested record rests".

modifiedJS nested record rests

type inner = {i: int, j: int}type onlyJ = {j: int}type outer = {inner: inner, z: int}type onlyZ = {z: int}let nestedRests = (o: outer, f: unit => unit) => {  let {inner: {i, ...onlyJ as innerRest}, ...onlyZ as rest} = o  f()  (rest, innerRest, i)}
js
Before
After
function nestedRests(o, f) {function nestedRests(o, f) { let {inner: match, ...rest} = o; let match = o.inner; let {i, ...innerRest} = match; let {i, ...innerRest} = match; let {inner: __unused0, ...rest} = o; f(); f(); … …}}

modifiedJS record rests in a tuple

type xy = {x: int, y: int}type onlyY = {y: int}let restsInTuple = (  ({x: a, ...onlyY as rest1}, {x: b, ...onlyY as rest2}): (xy, xy),  f: unit => unit,) => {  f()  (a, rest1, b, rest2)}
js
Before
After
function restsInTuple(param, f) {function restsInTuple(param, f) { let match = param[0]; let match = param[0]; let {x: a, ...rest1} = match; let a = match.x; let match$1 = param[1]; let match$1 = param[1]; let {x: b, ...rest2} = match$1; let {x: b, ...rest2} = match$1; let {x: __unused0, ...rest1} = match; f(); f(); … …}}

Review

  • Why not move the outer rest's read instead? Its destructuring binds the inner source, which the inner rest's copy needs first: one read of o.inner and the inner copy first would take a nested destructuring, which the JS IR's record rest does not have.
  • Which checked-in outputs change? Only record_rest_source_test's.