Semantic Diff

#9427 Evaluate the source of a record spread whose fields are all overridden

4 commits

6a7e3b8dbabcd2aa54643d86044d2f8ec2d6f804..pr-9427

8cb8eef9a · illusionalsagacity · Nov 25, 2026, 19:39 UTC

Evaluate the source of a record spread whose fields are all overridden

Why

When a record literal with a spread writes every field, the type checker reports warning 23 and also dropped the spread's source from the typed tree, so a source with side effects, such as a call, never ran. The same held for JSX props spreads. The source now stays in the typed tree; the record is built from the written fields alone, after the source is evaluated. A source that is a variable is left out of the Lambda term, so the size of a function and the output stay as they were; the Lambda passes drop any other source without side effects. A module with side effects that a source is read from stays imported.

modifiedTypedtree Texp_record

type t = {a: int, b: int}@val external f: unit => t = "f"let make = () => {...f(), a: 1, b: 2}
typed
Before
After
expressionexpression├─ Texp_record├─ Texp_record│  ├─ fields =│  ├─ fields =│  │     ├─ "a"│  │     ├─ "a"│  │     │  └─ expression│  │     │  └─ expression│  │     │     └─ Texp_constant Const_int 1│  │     │     └─ Texp_constant Const_int 1│  │     └─ "b"│  │     └─ "b"│  │        └─ expression│  │        └─ expression│  │           └─ Texp_constant Const_int 2│  │           └─ Texp_constant Const_int 2│  ├─ representation =│  ├─ representation =│  │  └─ Record_regular│  │  └─ Record_regular│  └─ extended_expression =│  └─ extended_expression =└─ None └─ Some   └─ expression      ├─ Texp_apply      └─ expression         ├─ Texp_ident "f/1004"         └─ <arg>            ├─ Nolabel            └─ expression               ├─ Texp_construct "()"               └─ []

modifiedJS record spread with every field overridden

type t = {a: int, b: int}@val external f: unit => t = "f"let make = () => {...f(), a: 1, b: 2}
js
Before
After
function make() {function make() { f(); return { return { a: 1, a: 1, b: 2 b: 2 }; };}}

modifiedJS source read from a module with side effects

let make = () => {...Effects.base, a: 1, b: 2}
js
Before
After
import * as Effects from "./Effects.mjs"; function make() {function make() { return { return { a: 1, a: 1, b: 2 b: 2 }; };}} export {export { make, make,}}/* No side effect *//* Effects Not a pure module */

modifiedTyping let rec check of an overridden source

type callback = {run: unit => int}@val external wrap: callback => callback = "wrap"let rec callback = {...wrap(callback), run: () => 1}
text
Before Rec_check
After Rec_check
accepted, without calling wrapThis kind of expression is not allowed as right-hand side of `let rec`

Review

  • Does a copy of the source remain when the record has optional fields or 20 fields or more? No: record_copies_fields holds when every field is written, so the source is never spread into the result.
  • Does the source affect generalization or let rec? As the first expression of a sequence would: its value is discarded, so the record stays polymorphic and let rec r = {...r, a: 1, b: 2} stays accepted, but a source that uses the recursive value early is now rejected, as is a source with a field left to take from it, as before.
  • Which existing checked-in outputs change? None.
cfbc15c06 · illusionalsagacity · Nov 26, 2026, 21:59 UTC

Build a recursive record into a dummy when its source runs first

Why

The record spread fix keeps a source whose fields are all overridden, so let rec n = {...makeSource(), next: n} becomes a block after a sequence or a let. compile_recursive_let only declared a dummy for a bare block, and otherwise declared the binding with its value, which read n before it was initialized. A block reached after a sequence or a let whose fields read one of the group's bindings is now built into a dummy declared first, as a bare block with such fields is. The same applied on master to a record built at the end of a sequence.

modifiedJS recursive record after its spread source

type rec node = {next: node, v: int}let rec n = {...makeSource(), next: n, v: 1}
js
Before
After
makeSource();let n = {}; let n = {Primitive_object.updateDummy(n, (makeSource(), { next: n, next: n, v: 1 v: 1};}));

modifiedJS recursive record at the end of a sequence

type rec node = {next: node, v: int}let rec n = {  Console.log("a")  {next: n, v: 1}}
js
Before
After
console.log("a");let n = {}; let n = {Primitive_object.updateDummy(n, (console.log("a"), { next: n, next: n, v: 1 v: 1};}));

Review

  • Do recursive modules, whose blocks follow lets of their functions, change? No: their fields are variables bound before the group, so they are still declared directly. No checked-in output other than the new tests changes.
  • Is the Lambda term without optimization covered? There is no switch to turn Lambda passes off; letCycle keeps a let before the block and cycle the sequence the passes leave of the source's let.
e74d411b4 · illusionalsagacity · Nov 27, 2026, 00:05 UTC

Declare a recursive record directly when its fields read none of the group

Why

block_after_effects took every field of a block after a sequence or a let, other than a function, a constant or a variable, to read the recursive group. A record of a let rec group whose fields are calls or reads of values outside the group was then built into a dummy with updateDummy when it could be declared directly. A field other than a function now reads the group only when Lam_hit.hit_variables finds one of the group's bindings in it.

modifiedJS record of a recursive group after a sequence

type r = {a: int, b: int, run: int => int}@val external log: unit => unit = "log"@val external compute: unit => int = "compute"let rec f = x => r.b + xand r = {  log()  {a: compute(), b: 1, run: x => f(x)}}
js
Before
After
let r = {}; function f(x) {function f(x) { return r.b + x | 0; return r.b + x | 0;}} Primitive_object.updateDummy(r, (log(), { a: compute(), log(); let r_a = compute(); function r_run(x) { return r.b + x | 0;} let r = { a: r_a, b: 1, b: 1, run: x => r.b + x | 0})); run: r_run};

Review

  • Do cycle, letCycle and sequenceCycle still use a dummy? Yes: their next field is the binding itself. Only the new counter case of the test changes.
e1188b717 · illusionalsagacity · Nov 27, 2026, 21:36 UTC

Find a recursive record's block after a recursive let too

Why

block_after_effects looked for the block of a let rec record after sequences and lets only. A spread source with a recursive helper, as in let rec n = {...{let rec loop = ...; loop()}, next: n}, has its helper hoisted out of it by Lam_pass_deep_flatten, which leaves the block after an Lletrec. compile_recursive_let then declared n with its value, which read n before it was initialized and threw a ReferenceError when the module loaded. The block is now also found after a recursive let. This answers Codex's review finding on #9427.

modifiedJS recursive record after a source with a recursive helper

type rec node = {value: int, next: node}let rec n = {  ...{    let rec loop = () => if again() {loop()} else {makeSource()}    loop()  },  value: 1,  next: n,}
js
Before
After
loop();let n = {}; let n = { function loop() { …} Primitive_object.updateDummy(n, (loop(), { value: 1, value: 1, next: n next: n};}));

Review

  • Is the Lambda term without optimization covered? There is still no switch to turn Lambda passes off; the test has the record both at module level and local to a function, where the helper stays a local binding.
  • Which checked-in outputs change? Only record_spread_source_effect_test.mjs, for the new cases.