Skip to content

[12.x] Evaluate a record spread's overrides in order - #9460

Open
illusionalsagacity wants to merge 2 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-record-spread-order-12.x
Open

illusionalsagacity wants to merge 2 commits into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-record-spread-order-12.x

Conversation

@illusionalsagacity

@illusionalsagacity illusionalsagacity commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Backport of #9416 (override-order fix) to maintenance/12.x.

Problem

A record spread {...base, f: v} of a record with an optional field (or with 20 fields or more) is compiled on 12.x as a copy of the base followed by assignments to it. The Lambda for the overrides set the last declared field first, so the overrides were evaluated in reverse order:

@inline(never) let trace = (name, value) => { order->Array.push(name); value }
type r = {mutable a: int, b: int, c?: int}
let boundThenUsed = (base: r) => {
  let x = {...base, a: trace("a", 1), b: trace("b", 2)}
  ignore(id(x.a))
  x
}

Before:

function boundThenUsed(base) {
  let newrecord = {...base};
  newrecord.b = trace("b", 2);
  newrecord.a = trace("a", 1);
  id(newrecord.a);
  return newrecord;
}

After:

function boundThenUsed(base) {
  let newrecord = {...base};
  newrecord.a = trace("a", 1);
  newrecord.b = trace("b", 2);
  id(newrecord.a);
  return newrecord;
}

Fix

As on master:

  • Translcore.transl_record sets the fields of the copy in declaration order, followed by the later assignments in source order.

Differences from master:

  • 12.x has no Lam_compile.try_compile_record_spread. On master that function builds a spread as an object literal, which is what let an override's statements run before the base was copied; master's main fix makes it fall back to a copy and assignments. On 12.x a spread is always a copy and assignments, so the overrides' statements already run after the copy and that part doesn't apply. The literal form, and leaving out the copy of a discarded result or a throwing override, aren't ported either: they change only the shape of the output.
  • The __proto__ part of Copy a record spread's base before overrides that need statements #9416 was dropped from this backport (since Reject __proto__ as the JavaScript name of a field #9513 removed it on master and rejected __proto__ fields, and 12.x will deprecate them separately).
  • jsx_preserve_test's element with spread props now lists its props in declaration order (className, then title), the same as on master.
  • 12.x has no tests/project_tests/source_map, so master's source map test case is left out. Master's test file is reformatted with 12.x's formatter, and two of its comments that describe master's literal form are reworded.

Tests

tests/tests/src/record_spread_order_test.res is master's test (without the __proto__ cases). Without the fix, 5 of its 13 tests fail on 12.x: the order of several overrides, a copy that is bound and then used, pure statements after a call, a discarded result, and a throwing override. The only other checked-in output that changes is jsx_preserve_test.mjs.

Investigated by Claude.

…field as an own property (rescript-lang#9416)

A record spread `{...base, f: v}` of a record with an optional field (or
with 20 fields or more) is a copy of the base and assignments to it. The
Lambda for the overrides set the last declared field first, so the
overrides were evaluated in reverse order. The fields are now set in
declaration order, followed by the later assignments in source order.

For a field renamed with @as("__proto__") that the base does not have,
the assignment to the copy ran the __proto__ setter it inherits from
Object.prototype: an int was dropped and an object became the copy's
prototype. A __proto__ field set on the copy right after it is made, as
an override or by a later assignment, is now defined with
Object.defineProperty, as an object literal would. Other names are still
assigned, and an assignment elsewhere, x.p = v, is unchanged.

12.x always compiles a spread as a copy and assignments: it has no
Lam_compile.try_compile_record_spread, which on master builds the object
literal this PR makes wait for statements in the overrides. So the
overrides' statements already run after the copy on 12.x, the literal
and the dropped copy of a discarded result are not ported, and the
__proto__ case is handled by Lam_compile.compile_copy_fields, which
compiles the other fields as before.

The order also changes the props of jsx_preserve_test's spread element
to declaration order, as on master. 12.x has no source map project test,
so that part of master's tests is left out, and master's test is
reformatted with 12.x's formatter.

(cherry picked from commit 83cf1de)
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
@cknitt

cknitt commented Dec 3, 2026

Copy link
Copy Markdown
Member

#9513 made __proto__ an error as the JavaScript name of a field on master (see #9512), and removed the __proto__ part of #9416 there. On 12.x we plan a deprecation warning instead, so these fields still compile. But writes and reads of them stay broken there (#9457), so the spread fix alone doesn't make them safe.

Could you drop the __proto__ part from this backport and keep only the override-order fix? That means:

  • define_own_property in js_exp_make.ml/.mli
  • the Fld_record_set "__proto__" / Fld_record_inline_set "__proto__" / Fld_record_extension_set "__proto__" branch in lam_compile.ml
  • the proto* functions and the two __proto__ tests in record_spread_order_test.res
  • the __proto__ half of the CHANGELOG entry

I'll add the 12.x warning in a separate PR.

This comment was drafted by Claude.

Keep only the override-order fix from rescript-lang#9416, as requested on rescript-lang#9460: remove Js_exp_make.define_own_property, Lam_compile.compile_copy_fields, the __proto__ tests in record_spread_order_test, and the __proto__ part of the CHANGELOG entry.

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>
@illusionalsagacity illusionalsagacity changed the title [12.x] Evaluate a record spread's overrides in order and define a __proto__ field as an own property [12.x] Evaluate a record spread's overrides in order Dec 4, 2026
@illusionalsagacity

Copy link
Copy Markdown
Contributor Author

Dropped the __proto__ part in f29d198b5a and updated the PR title/description so this backport keeps only the override-order fix from #9416:

  • Reverted Js_exp_make.define_own_property (js_exp_make.ml/.mli) and Lam_compile.compile_copy_fields (lam_compile.ml).
  • Removed the proto* helpers and both __proto__ tests from record_spread_order_test.{res,mjs}.
  • Updated the CHANGELOG.md entry to mention only the override-order fix.

This branch has not been deployed

No deployments
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