Skip to content

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

Draft
illusionalsagacity wants to merge 3 commits into
rescript-lang:masterfrom
illusionalsagacity:fix-record-rest-source
Draft

illusionalsagacity wants to merge 3 commits into
rescript-lang:masterfrom
illusionalsagacity:fix-record-rest-source

Conversation

@illusionalsagacity

@illusionalsagacity illusionalsagacity commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Part of #9398 (item 4).

Problem

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, including the program's own, with the destructured local. So an assignment to r.x was lost, a read of r.x was stale after a write (an assignment, a method, a callee, an alias, an await), and x next to an unused rest was read too late.

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
}

Before:

function f(a) {
  let {x, ...rest} = a;
  x = 42;
  return [
    rest,
    x,
    x
  ];
}

function g(a, k) {
  k();
  return a.x;
}

After:

function f(a) {
  let {x, ...rest} = a;
  a.x = 42;
  return [
    rest,
    x,
    a.x
  ];
}

function g(a, k) {
  let x = a.x;
  k();
  return x;
}

Fix

  • The match compiler binds the fields a pattern takes variables from next to a rest with StrictOpt, as it does mutable fields. Only the program's own reads are left as reads of the source.
  • The record rest pass replaces no reads. It only moves the field reads right before the rest into the destructuring, so a field isn't read twice. Other excluded fields get a placeholder name. Dead code elimination keeps such a destructuring when the interface exports a field it binds, as it does when the rest is exported.
  • Names bound in a rest destructuring now count as bindings that shadow a JS global. Without that, a top-level let {shadowG, ...rest} could capture a later read of the global.

Not fixed here

Tests

record_rest_source_test.res; 12 of its tests fail with master's pass and matching code. record_rest_hidden_rest.res exports a bound field but not its rest. In existing outputs, two placeholder names become the pattern's names (record_rest_test.mjs, array_record_test.mjs) and external_shadow_test.mjs gains a case.

12.x

Not applicable, since 12.x has no record rest.

The fix was written by Claude. A second Claude agent reviewed the first version; this version addresses that review and has not been reviewed again.

…ields

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.

Matching modified (fields bound next to a rest):
Example:
    type xy = {x: int, y: int}
    type onlyY = {y: int}
    let f = ({x, ...onlyY as rest}: xy) => (x, rest)
Before (text): make_record_matching
    mutable field                          StrictOpt
    immutable field                        Alias
After (text): make_record_matching
    mutable field                          StrictOpt
    immutable field bound next to a rest   StrictOpt
    other immutable field                  Alias

JS modified (reads and writes of a record rest source):
Example:
    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
    }
Before (js):
    function f(a) {
      let {x, ...rest} = a;
      x = 42;
      return [
        rest,
        x,
        x
      ];
    }

    function g(a, k) {
      k();
      return a.x;
    }
After (js):
    function f(a) {
      let {x, ...rest} = a;
      a.x = 42;
      return [
        rest,
        x,
        a.x
      ];
    }

    function g(a, k) {
      let x = a.x;
      k();
      return x;
    }

JS modified (names of bound fields):
Example:
    type renamed = {@as("user-name") name: string, other: int}
    type onlyOther = {other: int}
    let h = ({name, ...onlyOther as rest}: renamed) => (name, rest)
Before (js):
    function h(param) {
      let {"user-name": __rest_field0, ...rest} = param;
      return [
        __rest_field0,
        rest
      ];
    }
After (js):
    function h(param) {
      let {"user-name": name, ...rest} = param;
      return [
        name,
        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.
Review: 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.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.57143% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 82.28%. Comparing base (45dba2c) to head (4e0b9a4).
⚠️ Report is 16 commits behind head on master.

Files with missing lines Patch % Lines
compiler/backend/js_pass_record_rest.ml 97.56% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9421      +/-   ##
==========================================
+ Coverage   82.22%   82.28%   +0.06%     
==========================================
  Files         461      462       +1     
  Lines       65573    65691     +118     
==========================================
+ Hits        53916    54056     +140     
+ Misses      11657    11635      -22     
Files with missing lines Coverage Δ
compiler/backend/js_pass_external_shadow.ml 98.33% <100.00%> (+0.11%) ⬆️
compiler/backend/js_pass_flatten_and_mark_dead.ml 80.85% <100.00%> (+1.28%) ⬆️
compiler/typing/matching.ml 79.54% <100.00%> (+1.03%) ⬆️
compiler/backend/js_pass_record_rest.ml 97.82% <97.56%> (+4.34%) ⬆️

... and 23 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix code after let {a, ...rest} = r confusing a with r.a: an
    assignment to r.a was lost, a read of r.a could return an old value, and
    a could change when r.a was written later.

Semantic Diff

3 commits · Full page

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

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

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.

Matching modified (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 · make_record_matching

 mutable field                          StrictOpt
-immutable field                        Alias
+immutable field bound next to a rest   StrictOpt
+other immutable field                  Alias

JS modified (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

 function f(a) {
   let {x, ...rest} = a;
-  x = 42;
+  a.x = 42;
   return [
     rest,
     x,
-    x
+    a.x
   ];
 }
 
 function g(a, k) {
+  let x = a.x;
   k();
-  return a.x;
+  return x;
 }

JS modified (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

 function h(param) {
-  let {"user-name": __rest_field0, ...rest} = param;
+  let {"user-name": name, ...rest} = param;
   return [
-    __rest_field0,
+    name,
     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.
Keep a record rest that binds an exported field

cebfb67 · illusionalsagacity · Nov 26, 2026, 22:01 UTC

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".

JS modified (a record rest binding an exported field)

// record_rest_hidden_rest.resi: let x: int; let sumTo: int => int
type 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

 let match = make();
 
-(({x: __unused0, ...__rest}) => __rest)(match);
+let {x, ...rest} = match;
 
 …
 export {
   x,
   sumTo,
 }
✨ Keep the order of the copies of nested record rests

4e0b9a4 · illusionalsagacity · Nov 27, 2026, 21:32 UTC · new

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".

JS modified (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

 function nestedRests(o, f) {
-  let {inner: match, ...rest} = o;
+  let match = o.inner;
   let {i, ...innerRest} = match;
+  let {inner: __unused0, ...rest} = o;
   f();
   …
 }

JS modified (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

 function restsInTuple(param, f) {
   let match = param[0];
-  let {x: a, ...rest1} = match;
+  let a = match.x;
   let match$1 = param[1];
   let {x: b, ...rest2} = match$1;
+  let {x: __unused0, ...rest1} = match;
   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.

@pkg-pr-new

pkg-pr-new Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9421

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9421

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9421

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9421

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9421

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9421

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9421

commit: 4e0b9a4

@cknitt

cknitt commented Nov 26, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-28T05:55:39.555631Z 4e0b9a4 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: aacbc8aa14

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +68 to +71
| Some binding ->
bind
(bind_field fields binding)
~pending:[] ~after:(pending @ after) reads

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve exported field liveness when folding declarations

If a .resi exports a selected field but not the rest, and the rest's last reference disappears during Js_pass_tailcall_inline, folding the field declaration here loses its Exported metadata. Js_pass_flatten_and_mark_dead.mark_dead_code checks export_set only for the rest declaration's identifier, not its field identifiers, so it marks the destructuring Dead_non_pure. Js_dump.variable_declaration then emits only the rest-copy IIFE, leaving an undeclared field in export {x} and making the generated module fail to load. Propagate export liveness from folded field identifiers, or retain their standalone declarations.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reproduced. With an interface that exports x but not rest, and rest only passed to a function whose call is inlined in JS, the output was (({x: __unused0, ...__rest}) => __rest)(match); followed by export { x, ... }, and loading the module failed with ReferenceError: x is not defined. Master doesn't fold field reads into the destructuring, so this came from this PR.

Fixed in cebfb67. Js_pass_flatten_and_mark_dead.mark_dead_code now marks a record rest declaration Exported when its own name or one of the fields it binds is in export_set. The fields already share the declaration's ident_info for uses, so exports now work the same way. The output is now let {x, ...rest} = match;. Regression test: tests/tests/src/record_rest_hidden_rest.res/.resi, imported by a new test in record_rest_source_test.res. The test fails to load with the previous commit's compiler.

Investigated by Claude.

Comment on lines +31 to +35
when Js_op_util.same_vident read source
&& List.exists
(fun {record_rest_label; record_rest_ident} ->
record_rest_label = label && record_rest_ident = None)
fields ->

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restrict folding to reads introduced by the rest pattern

For records with getter-backed fields, this predicate also absorbs explicit user reads unrelated to the pattern. For example, let n = c.name; let {name: _, bump: _, ...onlyOther as rest} = c; (n, rest, n) retains a standalone read of name, but this pass folds it into the wildcard destructuring. The getter is consequently invoked once instead of twice; if it updates a retained field, the contents of rest also change. Only coalesce synthesized field reads belonging to this rest pattern, rather than arbitrary preceding field declarations.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't hold, so no change. Folding the explicit read is what keeps the getter count right. let {name: _, ...rest} = c doesn't read name in the source, but the JS destructuring still has to name name to leave it out of the rest, and that reads it. The source reads name once (let n = c.name). Without folding, the JS reads it twice. With folding, it reads it once.

I ran your example with name as a getter that counts calls and increments other:

  • this branch: let {name: n, bump: __unused1, ...rest} = c; gives ['read1', {other: 1}, 'read1']: one getter call, and rest.other as the source implies.
  • the unfolded form you suggest (let n = c.name; let {name: __unused0, bump: __unused1, ...rest} = c;, which is also what master emits here, since master never folds a preceding declaration) gives ['read1', {other: 2}, 'read1']: two getter calls, and rest sees the extra increment.

Only reads that sit directly before the rest, read a field the pattern leaves out, and are separated from it by nothing but other variable reads get folded. No write can happen between the read and the destructuring, so it makes no difference whether the read came from the pattern or from the program.

Investigated by Claude.

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".

JS modified (a record rest binding an exported field):
Example:
    // record_rest_hidden_rest.resi: let x: int; let sumTo: int => int
    type 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)
Before (js):
    let match = make();

    (({x: __unused0, ...__rest}) => __rest)(match);

    …
    export {
      x,
      sumTo,
    }
After (js):
    let match = make();

    let {x, ...rest} = match;

    …
    export {
      x,
      sumTo,
    }

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
@cknitt

cknitt commented Nov 27, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cebfb6706e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread compiler/backend/js_pass_record_rest.ml Outdated
in
let statements = before @ (rest :: after) in
match source.expression_desc with
| Var _ -> block self (List.rev statements) tail

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve copy order when folding nested record rests

For nested rest patterns on getter-backed records, this requeues an already-materialized inner Record_rest as if it were a movable read. When the outer rest absorbs let inner = o.inner, bind_fields moves the inner copy after the outer copy. For example, the checked-in nestedRests returns innerRest = {j: 42} instead of {j: 2} for o.inner = {i: 1, j: 2} and an enumerable o.z getter that sets o.inner.j = 42; the previous pass preserved the inner-before-outer copy order. Keep effectful rest declarations out of this buffer, or otherwise preserve their evaluation order.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed. I used o = {inner: {i: 1, j: 2}} with an enumerable getter z that sets o.inner.j = 42. nestedRests compiled to let {inner: match, ...rest} = o; let {i, ...innerRest} = match;, and node printed { z: 3 } { j: 42 } 1. The outer copy ran the getter before the inner copy.

Fixed in 4e0b9a4 ("Keep the order of the copies of nested record rests"). The pass no longer puts a rest declaration back among the movable reads. The rest stays where it is, and only the reads after it can move into a later rest's destructuring. Now it compiles to let match = o.inner; let {i, ...innerRest} = match; let {inner: __unused0, ...rest} = o; and prints { z: 3 } { j: 2 } 1.

The cost is one extra read. A field read before an earlier rest is read again by the later rest's destructuring, which is what happened before this PR. That applies to o.inner in nestedRests and to match.x in restsInTuple. Reading it once and still copying the inner record first would need a nested destructuring, which the JS IR's record rest can't express. The "a bound field is read once" test now expects 2 reads for nestedRests, with a comment explaining why.

Regression test: "nested rests are copied inner first" in tests/tests/src/record_rest_source_test.res (innerWrittenByGetter).

Investigated by Claude.

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".

JS modified (nested record rests):
Example:
    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)
    }
Before (js):
    function nestedRests(o, f) {
      let {inner: match, ...rest} = o;
      let {i, ...innerRest} = match;
      f();
      …
    }
After (js):
    function nestedRests(o, f) {
      let match = o.inner;
      let {i, ...innerRest} = match;
      let {inner: __unused0, ...rest} = o;
      f();
      …
    }

JS modified (record rests in a tuple):
Example:
    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)
    }
Before (js):
    function restsInTuple(param, f) {
      let match = param[0];
      let {x: a, ...rest1} = match;
      let match$1 = param[1];
      let {x: b, ...rest2} = match$1;
      f();
      …
    }
After (js):
    function restsInTuple(param, f) {
      let match = param[0];
      let a = match.x;
      let match$1 = param[1];
      let {x: b, ...rest2} = match$1;
      let {x: __unused0, ...rest1} = match;
      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.
Review: Which checked-in outputs change? Only record_rest_source_test's.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
@cknitt

cknitt commented Nov 28, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4e0b9a4abc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

| Some binding ->
bind
(bind_field fields binding)
~pending:[] ~after:(pending @ after) reads

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep unrelated field reads ahead of the rest copy

If a retained field has a getter, moving unmatched pending reads into after changes evaluation order. For example, let x = a.x; let y = a.y; let {x: _, ...onlyY as rest} = a; (x, y, rest, x, y) with an incrementing a.y getter becomes a rest destructuring followed by let y = a.y. It consequently returns y = 2, rest.y = 1 instead of y = 1, rest.y = 2. The nested-rest barrier does not protect these ordinary field/index reads; preserve their position relative to the copy unless their values are proven stable.

Useful? React with 👍 / 👎.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants