Repository navigation
Keep the fields bound next to a record rest apart from the source's fields - #9421
illusionalsagacity wants to merge 3 commits into
Conversation
…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 Report❌ Patch coverage is
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
🚀 New features to boost your workflow:
|
Changelog🐛 Bug fix
Semantic Diff3 commits · Full page Keep the fields bound next to a record rest apart from the source's fieldsaacbc8a · illusionalsagacity · Nov 23, 2026, 20:40 UTC
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)
mutable field StrictOpt
-immutable field Alias
+immutable field bound next to a rest StrictOpt
+other immutable field AliasJS 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
}
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)
function h(param) {
- let {"user-name": __rest_field0, ...rest} = param;
+ let {"user-name": name, ...rest} = param;
return [
- __rest_field0,
+ name,
rest
];
}Review
Keep a record rest that binds an exported fieldcebfb67 · illusionalsagacity · Nov 26, 2026, 22:01 UTC
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)
let match = make();
-(({x: __unused0, ...__rest}) => __rest)(match);
+let {x, ...rest} = match;
…
export {
x,
sumTo,
}✨ Keep the order of the copies of nested record rests4e0b9a4 · illusionalsagacity · Nov 27, 2026, 21:32 UTC · new
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)
}
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)
}
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
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| | Some binding -> | ||
| bind | ||
| (bind_field fields binding) | ||
| ~pending:[] ~after:(pending @ after) reads |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.
| 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 -> |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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, andrest.otheras 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, andrestsees 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>
|
@codex review |
There was a problem hiding this comment.
💡 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".
| in | ||
| let statements = before @ (rest :: after) in | ||
| match source.expression_desc with | ||
| | Var _ -> block self (List.rev statements) tail |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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>
|
@codex review |
There was a problem hiding this comment.
💡 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 |
There was a problem hiding this comment.
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 👍 / 👎.
Part of #9398 (item 4).
Problem
After
let {x, ...rest} = r, the pattern'sxis the value at the destructuring andr.xis the current one. The match compiler boundxas an alias, so Lambda inlined it as a read ofr.x, and the record rest pass then replaced everyr.xafter the rest, including the program's own, with the destructured local. So an assignment tor.xwas lost, a read ofr.xwas stale after a write (an assignment, a method, a callee, an alias, anawait), andxnext to an unused rest was read too late.Before:
After:
Fix
StrictOpt, as it does mutable fields. Only the program's own reads are left as reads of the source.let {shadowG, ...rest}could capture a later read of the global.Not fixed here
{name: "a", ...rest}followed by a case that bindsname), the field is read twice. The value bound is the one tested.Tests
record_rest_source_test.res; 12 of its tests fail with master's pass and matching code.record_rest_hidden_rest.resexports 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) andexternal_shadow_test.mjsgains 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.