Repository navigation
Copy dict and array spreads before later elements that can change them - #9429
illusionalsagacity wants to merge 1 commit into
Conversation
Why:
`dict{...base, "b": v}` and `[...base, v]` copy `base` where the spread
stands, before `v` is evaluated. `sequence_arguments` got the bare spread
as an argument. A variable is safe to delay, so it stayed in the literal,
after the statements of any later element that needs them (a `try`, or a
`switch` that is not one expression), and a mutation of the base in those
statements showed up in the result. An array spread is copied by
`Primitive_array.spread`, which runs after all elements are evaluated, so
there a plain call did the same. While the parts of a literal are
sequenced, a spread is now the copy `{...base}` or `[...base]` when a later
part has such statements or, in an array, a value that can run code (a
call, an `await`, a read of a JS property). Otherwise the spread stays bare
as before.
JS modified (dict spread with an element that needs statements):
Example:
@inline(never) let id = x => x
let f = (base: dict<int>) =>
dict{...base, "b": try { base->Dict.set("a", 100); id(5) } catch { | _ => 0 }}
Before (js):
function f(base) {
let tmp;
try {
base["a"] = 100;
tmp = id(5);
} catch (exn) {
tmp = 0;
}
return {
...base,
b: tmp
};
}
After (js):
function f(base) {
let tmp = {...base};
let tmp$1;
try {
base["a"] = 100;
tmp$1 = id(5);
} catch (exn) {
tmp$1 = 0;
}
return {
...tmp,
b: tmp$1
};
}
JS modified (array spread with an element that needs statements):
Example:
@inline(never) let id = x => x
let f = (base: array<int>) =>
[...base, try { base->Array.push(100); id(5) } catch { | _ => 0 }]
Before (js):
function f(base) {
let tmp;
try {
base.push(100);
tmp = id(5);
} catch (exn) {
tmp = 0;
}
return Primitive_array.spread([
base,
[tmp]
]);
}
After (js):
function f(base) {
let tmp = [...base];
let tmp$1;
try {
base.push(100);
tmp$1 = id(5);
} catch (exn) {
tmp$1 = 0;
}
return Primitive_array.spread([
tmp,
[tmp$1]
]);
}
JS modified (array spread with a call after it):
Example:
let call = (base: array<int>, f) => [...base, f()]
let pure = (base: array<int>, x) => [...base, x + 1]
Before (js):
function call(base, f) {
return Primitive_array.spread([
base,
[f()]
]);
}
function pure(base, x) {
return Primitive_array.spread([
base,
[x + 1 | 0]
]);
}
After (js):
function call(base, f) {
return Primitive_array.spread([
[...base],
[f()]
]);
}
function pure(base, x) {
return Primitive_array.spread([
base,
[x + 1 | 0]
]);
}
Review: Are list spreads affected? No, lists are immutable, so when they are
copied doesn't show.
Review: Why not emit `[...base, f()]`? An array literal with a spread in it is
not safe for the code that looks into array literals (indexing, `@variadic`
arguments), so the copy only ever stands inside the runtime call or a
temporary.
Review: Which checked-in outputs change? spread_literal_test.mjs, which has a
call after a spread.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Changelog🐛 Bug fix
Semantic Diff1 commit · Full page Copy dict and array spreads before later elements that can change thembc27b18 · illusionalsagacity · Nov 25, 2026, 19:42 UTC
JS modified (dict spread with an element that needs statements) @inline(never) let id = x => x
let f = (base: dict<int>) =>
dict{...base, "b": try { base->Dict.set("a", 100); id(5) } catch { | _ => 0 }}
function f(base) {
- let tmp;
+ let tmp = {...base};
+ let tmp$1;
try {
base["a"] = 100;
- tmp = id(5);
+ tmp$1 = id(5);
} catch (exn) {
- tmp = 0;
+ tmp$1 = 0;
}
return {
- ...base,
- b: tmp
+ ...tmp,
+ b: tmp$1
};
}JS modified (array spread with an element that needs statements) @inline(never) let id = x => x
let f = (base: array<int>) =>
[...base, try { base->Array.push(100); id(5) } catch { | _ => 0 }]
function f(base) {
- let tmp;
+ let tmp = [...base];
+ let tmp$1;
try {
base.push(100);
- tmp = id(5);
+ tmp$1 = id(5);
} catch (exn) {
- tmp = 0;
+ tmp$1 = 0;
}
return Primitive_array.spread([
- base,
- [tmp]
+ tmp,
+ [tmp$1]
]);
}JS modified (array spread with a call after it) let call = (base: array<int>, f) => [...base, f()]
let pure = (base: array<int>, x) => [...base, x + 1]
function call(base, f) {
return Primitive_array.spread([
- base,
+ [...base],
[f()]
]);
}
function pure(base, x) {
return Primitive_array.spread([
base,
[x + 1 | 0]
]);
}Review
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #9429 +/- ##
==========================================
+ Coverage 82.21% 82.25% +0.03%
==========================================
Files 461 462 +1
Lines 65573 65620 +47
==========================================
+ Hits 53914 53973 +59
+ Misses 11659 11647 -12
🚀 New features to boost your workflow:
|
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. |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
The fix is correct. I checked arrays with several spreads, the same base spread twice, nested spreads, dicts with several spreads followed by a One cost in common code: an array spread followed by a call now copies the base twice, once with let a = (xs, x) => [...xs, f(x)]return Primitive_array.spread([[...xs], [f(x)]]);Reading a JS object field triggers it too ( Could this case be a native array literal instead? return [...xs, f(x)];JS evaluates array elements, spreads included, left to right. So
This PR already emits Small things:
This review was done by Claude. |
Part of #9398 (item 34). Found while reviewing the fixes for it.
Problem
dict{...base, "b": v}and[...base, v]copybasebeforevis evaluated. Whenvneeds statements (atry, or aswitchthat is not one expression), the copy was taken after them. An array is copied byPrimitive_array.spread, which runs after all elements, so there a plain call is enough.Before:
After:
With a call in an array:
Before:
After:
Fix
While the parts of a literal are sequenced, a spread is the copy
{...base}or[...base]if a later part needs statements or, in an array, can run code. Otherwise it stays bare. List spreads are unaffected, since lists are immutable.Trade-offs
[...xs, f(x)]is common and now copiesxstwice. Emitting native[...xs, f(x)]would avoid that, but passes that look into array literals (indexing,@variadic) would need auditing first. I'd like your opinion on this one.arraynow throws where a copy is made.Tests
dict_array_spread_order_test.reschecks the results at runtime for a later element that runs code in each part of its expression (operands, conditions, indexes, string appends, templates, dicts and records, including a record with an optional field), and pins the cases that run no code. Disabling any one of four checks incan_run_code(record spreads, object spreads, templates, the right side of a string append) makes a runtime test fail. One existing output changes,spread_literal_test.mjs.12.x
12.x has no dict spread, but its array spreads have the same bug: there both
[...base, f()]and thetrycase include the mutation ([1, 2, 100, 5]). I'll backport once this one is reviewed.The fix was written by Claude.