Skip to content

Copy a JSX props spread before later props that need statements - #9426

Merged
cknitt merged 3 commits into
rescript-lang:masterfrom
illusionalsagacity:fix-jsx-props-spread-order
Nov 29, 2026
Merged

cknitt merged 3 commits into
rescript-lang:masterfrom
illusionalsagacity:fix-jsx-props-spread-order

Conversation

@illusionalsagacity

@illusionalsagacity illusionalsagacity commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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} /> copies base before v is evaluated. When v needs statements (a try, or a switch that 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.

@@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:

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:

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
  });
}

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 pass base itself.

Tests

jsx_props_spread_order_test.res, including a spread of an object literal, which stays let 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.formProps before <form {...form.formProps}>'s children became let 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.

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix evaluation order for a JSX props spread <Foo {...base} b={value} />: the base is copied before the statements (such as a try) of a later prop or child, or of the key, which is evaluated after the props wherever it is written, so their mutations of the base no longer show up in the props. This also holds for a spread with a key and no other props.

Semantic Diff

3 commits · Full page

Copy a JSX props spread before later props that need statements

fc944fa · illusionalsagacity · Nov 28, 2026, 21:08 UTC

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)

@@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 }} />

js

 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

  • Is a lone spread copied? No, <Foo {...base} />, with or without a key, still passes base itself.
  • 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.
  • 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.
  • Which checked-in outputs change? Only the new test's.
Copy a lone JSX props spread before the statements of its key

a8b046c · illusionalsagacity · Nov 28, 2026, 21:10 UTC

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)

let mutateInKeyOfLoneSpread = (base: Foo.props) =>
  <Foo key={try { base.a = 100; id("k") } catch { | _ => "" }} {...base} />

js

 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

  • 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.
  • 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.
Keep a JSX props spread of a record literal uncopied

6f7f347 · illusionalsagacity · Nov 28, 2026, 21:11 UTC

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)

let freshRecord = (base: Foo.props) =>
  <Foo {...{Foo.a: base.a}} b={try { base.a = 100; id(5) } catch { | _ => 0 }} />

js

 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

  • 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.
  • Does a dict spread change? A dict spread is never a record literal, so its output stays the same.
  • Which checked-in outputs change? Only the new test's.

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.44%. Comparing base (db22926) to head (6f7f347).
⚠️ Report is 3 commits behind head on master.

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     
Files with missing lines Coverage Δ
compiler/backend/lam_compile.ml 92.31% <100.00%> (+1.31%) ⬆️

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

@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@9426

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 6f7f347

@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-29T06:59:37.214617Z 131d030 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 960b327758

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

@illusionalsagacity
illusionalsagacity marked this pull request as ready for review November 27, 2026 20:00
@cknitt

cknitt commented Nov 28, 2026

Copy link
Copy Markdown
Member

The fix is correct: <Foo {...base} b={v} /> now copies base before v's statements run. Ordinary props compile as before.

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:

  • runs_no_code now treats a bare {...r} as running no code;
  • sequence_arguments leaves the copy in place when nothing later needs statements;
  • the Pjsx translation strips {...x} again.

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: let tmp = {...base, c: 3}.

#9429's copy_spreads ~in_place:true does exactly what's needed: it copies only when a later part has statements, and it skips object literals. JSX props are in place, like dict parts. Could this PR use it, on top of #9429, and drop the runs_no_code change and the stripping in Pjsx?

Small things:

  • mutateInKey writes key= before {...base} and checks that the key's change is not seen. That's the opposite of source order. It's consistent with the key always being evaluated after the props, but the test could say this is intended.
  • This now conflicts with master after Copy a record spread's base before overrides that need statements #9416, so it needs a rebase.

This review was done by Claude.

@illusionalsagacity

Copy link
Copy Markdown
Contributor Author

Agreed, the copy-then-undo was more machinery than needed, and it copied a fresh literal twice: let tmp = {...({...base, c: 3})}.

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 copy_spreads, and it shares copy_object with the dict spreads, which skips object literals. copy_spreads itself treats every part as a spread, but in a Pjsx only the argument after the tag is one, so the hold applies the rule to that argument directly. (#9429 also dropped the in_place parameter, since its array spreads now become array literals.) The runs_no_code change and the stripping of {...x} in the Pjsx translation are gone. Since a copy is only made when later statements come, sequence_arguments always holds it in a temporary, so no bare copy reaches the translation.

New test freshLiteral in jsx_props_spread_order_test.res pins <Foo {...{...base, c: 3}} b={try ... } /> to let tmp = {...base, c: 3}, as on master. The comment on mutateInKey now says that the key is evaluated after the props and children wherever it is written, so not seeing its change of the base is intended. The other checked-in outputs are the same as before. Rebased onto #9429, which is rebased onto master after #9416.

Investigated by Claude.

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

Comment thread compiler/backend/lam_compile.ml Outdated
(* JS evaluates the elements of an array literal, spreads included,
left to right *)
with_source_loc loc
(E.array

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 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 👍 / 👎.

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.

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.

@cknitt

cknitt commented Nov 28, 2026

Copy link
Copy Markdown
Member

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 (<Foo {...base} key={try {base.a = 100; "k"} catch {| _ => "e"}} /> still passes base itself, as on master). That fits "a lone spread passes base itself", but the changelog fragment says the base is copied before a later prop, child or key. Maybe reword it, or add a test that pins this case.

This review was done by Claude.

@illusionalsagacity
illusionalsagacity force-pushed the fix-jsx-props-spread-order branch from 49a9ac2 to 131d030 Compare November 29, 2026 00:24
@illusionalsagacity

Copy link
Copy Markdown
Contributor Author

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 base.a = 100 in the key no longer shows up in the props. The same commit adds a test for that case in jsx_props_spread_order_test.res, and the changelog fragment now ends with "This also holds for a spread with a key and no other props."

Investigated by Claude.

@cknitt

cknitt commented Nov 29, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 131d030de0

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

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>
@cknitt
cknitt force-pushed the fix-jsx-props-spread-order branch from 131d030 to 6f7f347 Compare November 29, 2026 07:36

@cknitt cknitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me. I built it merged with current master, regenerated all test outputs and ran the full test suite: no diffs, all tests pass. I rebased it onto master after #9429 was merged.

This review was done by Claude.

@cknitt
cknitt merged commit 6b9bc07 into rescript-lang:master Nov 29, 2026
23 checks passed
@illusionalsagacity
illusionalsagacity deleted the fix-jsx-props-spread-order branch November 29, 2026 23:09
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