Repository navigation
Copy a JSX props spread before later props that need statements - #9426
Conversation
Changelog🐛 Bug fix
Semantic Diff3 commits · Full page Copy a JSX props spread before later props that need statementsfc944fa · illusionalsagacity · Nov 28, 2026, 21:08 UTC
JS modified (JSX props spread with a prop that needs statements) @@config({flags: ["-bs-jsx", "4"]})
@inline(never) let id = x => x
module Foo = {
type props = {mutable a: int, b?: int}
@react.componentWithProps
let make = (props: props) => React.int(props.a)
}
let f = (base: Foo.props) =>
<Foo {...base} b={try { base.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 JsxRuntime.jsx(Example$Foo, {
- ...base,
- b: tmp
+ ...tmp,
+ b: tmp$1
});
}Review
Copy a lone JSX props spread before the statements of its keya8b046c · illusionalsagacity · Nov 28, 2026, 21:10 UTC
JS modified (lone props spread with a key that needs statements) let mutateInKeyOfLoneSpread = (base: Foo.props) =>
<Foo key={try { base.a = 100; id("k") } catch { | _ => "" }} {...base} />
function mutateInKeyOfLoneSpread(base) {
- let tmp;
+ let tmp = {...base};
+ let tmp$1;
try {
base.a = 100;
- tmp = id("k");
+ tmp$1 = id("k");
} catch (exn) {
- tmp = "";
+ tmp$1 = "";
}
- return JsxRuntime.jsx(Jsx_props_spread_order_test$Foo, base, tmp);
+ return JsxRuntime.jsx(Jsx_props_spread_order_test$Foo, tmp, tmp$1);
}Review
Keep a JSX props spread of a record literal uncopied6f7f347 · illusionalsagacity · Nov 28, 2026, 21:11 UTC
JS modified (props spread of a record literal before a prop that needs statements) let freshRecord = (base: Foo.props) =>
<Foo {...{Foo.a: base.a}} b={try { base.a = 100; id(5) } catch { | _ => 0 }} />
function freshRecord(base) {
- let tmp = {...({
- a: base.a
- })};
+ let tmp = base.a;
let tmp$1;
try {
base.a = 100;
tmp$1 = id(5);
} catch (exn) {
tmp$1 = 0;
}
return JsxRuntime.jsx(Jsx_props_spread_order_test$Foo, {
- ...tmp,
+ ...({
+ a: tmp
+ }),
b: tmp$1
});
}Review
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9426 +/- ##
==========================================
+ Coverage 82.37% 82.44% +0.06%
==========================================
Files 462 465 +3
Lines 65781 65935 +154
==========================================
+ Hits 54186 54359 +173
+ Misses 11595 11576 -19
🚀 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. Bravo. 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: The way it gets there could be simpler. Right now the spread is always copied, and three places then undo the copy where it isn't needed:
An already fresh literal is still copied: <Foo {...{...base, c: 3}} b={try {base.a = 100; id(5)} catch {| _ => 0}} />let tmp = {...({...base, c: 3})};On master this was already correct: #9429's Small things:
This review was done by Claude. |
960b327 to
49a9ac2
Compare
|
Agreed, the copy-then-undo was more machinery than needed, and it copied a fresh literal twice: Fixed in 49a9ac2, now on top of #9429: the Pjsx hold copies the spread only when a later prop, child or key has statements that do more than define pure values, the same condition as New test Investigated by Claude. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 49a9ac260a
ℹ️ 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".
| (* JS evaluates the elements of an array literal, spreads included, | ||
| left to right *) | ||
| with_source_loc loc | ||
| (E.array |
There was a problem hiding this comment.
Preserve literal evaluation when folding a surrounding typeof
When a spread is passed directly to typeof, as in (xs, f) => typeof([...xs, f()]), this branch produces a J.Array with f() still inside the expression. Js_exp_make.typeof (compiler/backend/js_exp_make.ml:291–297) unconditionally folds Array _ to "object", so the call and any mutations or exceptions it causes disappear. Before this change, the operand was a Primitive_array.spread(...) call and remained evaluated. Materialize the effectful operand or make the literal-folding helper preserve its evaluation, and add a regression covering this composition.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Real, and it comes from #9429, which this PR builds on: typeof([...base, f()]) folded to "object" without calling f once the spread became a native array literal there.
Fixed in #9429 by f0b62fa: the typeof and null-check folds in js_exp_make.ml now require the operand to run no code. Regression test: tests/tests/src/literal_fold_effect_test.res. This branch is rebased onto it (head 06678fd).
Investigated by Claude.
|
Thanks, this is much simpler now. It goes on top of #9429 and will be ready once that one is. I built it merged with master and regenerated all test outputs: no diffs, all tests pass. One small thing: a lone spread with only a key isn't copied ( This review was done by Claude. |
49a9ac2 to
131d030
Compare
|
Thanks. This was fixed by 00f14c6, pushed after your review. A lone spread followed by a key whose expression has statements is now copied too, so Investigated by Claude. |
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
Why:
The props `{...base, b: v}` of `<Foo {...base} b={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 object literal, after the statements of any later prop,
child or key that needs them (a `try`, or a `switch` that is not one
expression). A mutation of the base in those statements showed up in the
props. As for dict spreads, `sequence_arguments` now turns the spread into
the copy `{...base}`, held in a temporary, when statements that can
change it come after it: those of a later prop, child or key, or a later
call held in front of them. Otherwise it stays in place and is merged
with the other props as before. A spread of an object literal is
fresh and is not copied.
JS modified (JSX props spread with a prop that needs statements):
Example:
@@config({flags: ["-bs-jsx", "4"]})
@inline(never) let id = x => x
module Foo = {
type props = {mutable a: int, b?: int}
@react.componentWithProps
let make = (props: props) => React.int(props.a)
}
let f = (base: Foo.props) =>
<Foo {...base} b={try { base.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 JsxRuntime.jsx(Example$Foo, {
...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 JsxRuntime.jsx(Example$Foo, {
...tmp,
b: tmp$1
});
}
Review: Is a lone spread copied? No, `<Foo {...base} />`, with or without a
key, still passes `base` itself.
Review: Why spread the copy again instead of assigning the props to it? The
element stays a `Jsx` node, which JSX preserve mode can print as JSX.
Review: Is the key evaluated in source order? No, after the props and
children, wherever it is written, as before. So the spread is copied
before the key's statements, also when the key is written first.
Review: Which checked-in outputs change? Only the new test's.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Why:
A props spread with no other props and no children was never copied,
even when the key needs statements. The key is evaluated after the
props, so those statements ran before the spread was read, and a change
they made to the base showed up in the props. With any other prop the
same key's change was not seen, as `mutateInKey` intends.
`sequence_arguments` decides when the spread needs a copy, so the
`Pjsx` case now passes `copy_jsx_spread` for every props spread. A
lone spread with no statements after it still passes the base itself.
JS modified (lone props spread with a key that needs statements):
Example:
let mutateInKeyOfLoneSpread = (base: Foo.props) =>
<Foo key={try { base.a = 100; id("k") } catch { | _ => "" }} {...base} />
Before (js):
function mutateInKeyOfLoneSpread(base) {
let tmp;
try {
base.a = 100;
tmp = id("k");
} catch (exn) {
tmp = "";
}
return JsxRuntime.jsx(Jsx_props_spread_order_test$Foo, base, tmp);
}
After (js):
function mutateInKeyOfLoneSpread(base) {
let tmp = {...base};
let tmp$1;
try {
base.a = 100;
tmp$1 = id("k");
} catch (exn) {
tmp$1 = "";
}
return JsxRuntime.jsx(Jsx_props_spread_order_test$Foo, tmp, tmp$1);
}
Review: What about a key with no statements, such as `key={setA(base, "k")}`?
The spread is still passed itself and React reads it after the call, as
before this branch; only statements, which run before the call, make
the copy.
Review: Which checked-in outputs change? `loneSpread`, whose key had a
`try` and now is a call, so it still pins a spread passed itself. The
changelog fragment now names the key apart from the later props and
children, and the lone spread.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
Why:
`copy_object` treated only an object literal as fresh, so a props spread
of a record literal, which compiles to a `Caml_block`, was copied into
`{...({a: base.a})}` before later statements. A record literal is a
new object too, so it is now held as it is: its fields are read into
temporaries before the statements, as for any literal held by
`evaluate_now`, and the literal stays in the props.
JS modified (props spread of a record literal before a prop that needs statements):
Example:
let freshRecord = (base: Foo.props) =>
<Foo {...{Foo.a: base.a}} b={try { base.a = 100; id(5) } catch { | _ => 0 }} />
Before (js):
function freshRecord(base) {
let tmp = {...({
a: base.a
})};
let tmp$1;
try {
base.a = 100;
tmp$1 = id(5);
} catch (exn) {
tmp$1 = 0;
}
return JsxRuntime.jsx(Jsx_props_spread_order_test$Foo, {
...tmp,
b: tmp$1
});
}
After (js):
function freshRecord(base) {
let tmp = base.a;
let tmp$1;
try {
base.a = 100;
tmp$1 = id(5);
} catch (exn) {
tmp$1 = 0;
}
return JsxRuntime.jsx(Jsx_props_spread_order_test$Foo, {
...({
a: tmp
}),
b: tmp$1
});
}
Review: Can a `Caml_block` be shared, so that later code changes it? No, it
builds a new object where it stands, like an object literal.
Review: Does a dict spread change? A dict spread is never a record literal,
so its output stays the same.
Review: Which checked-in outputs change? Only the new test's.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
131d030 to
6f7f347
Compare
Builds on #9429, so the commits before it here are #9429's; review from "Copy a JSX props spread before later props that need statements" on.
Part of #9398 (item 33). Found while reviewing the fixes for it.
Problem
<Foo {...base} b={v} />copiesbasebeforevis evaluated. Whenvneeds statements (atry, or aswitchthat is not one expression), the bare spread stayed in the object literal, after those statements, and a mutation in them showed up in the props.Before:
After:
Fix
As for dict spreads in #9429, the spread becomes the copy
{...base}, held in a temporary, when statements that can change it come after it: those of a later prop or child, of the key, or a later call held in front of them. This holds also for a spread with a key and no other props. Otherwise it stays in place and is merged with the other props as before. A spread of an object or record literal,<Foo {...{...base, c: 3}} ... />, is fresh and is not copied.<Foo {...base} />, and a lone spread whose key needs no statements, still passbaseitself.Tests
jsx_props_spread_order_test.res, including a spread of an object literal, which stayslet tmp = {...base, c: 3}without a second copy, a spread of a record literal, whose fields are read before the later statements, a key written before the spread, which is evaluated after the props, so its change of the base is not seen, and the same key on a spread with no other props. No existing checked-in output changes.Trade-off
Children are often compiled to statements, so a spread before them is now copied: in one form library,
let tmp = form.formPropsbefore<form {...form.formProps}>'s children becamelet tmp = {...form.formProps}. That costs an extra shallow copy per render. A spread with nothing to run after it is not copied.12.x
Not affected, from reading the code.
The fix was written by Claude.