Repository navigation
[12.x] Evaluate a record spread's overrides in order and define a __proto__ field as an own property - #9460
Open
illusionalsagacity wants to merge 1 commit into
Conversation
…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>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport of #9416 to 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:Before:
After:
For a field renamed with
@as("__proto__")that the base does not have, the assignment to the copy ran the__proto__setter the copy inherits fromObject.prototype: an int was dropped and an object became the copy's prototype.Before:
After:
Fix
As on master:
Translcore.transl_recordsets the fields of the copy in declaration order, followed by the later assignments in source order.__proto__field set on the copy right after it is made, as an override or by a later assignment, is defined withObject.defineProperty(Js_exp_make.define_own_property). Other names are still assigned, and an assignment elsewhere,x.p = v, is unchanged.Differences from master:
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.__proto__case is handled by a newLam_compile.compile_copy_fields. It runs over the fields set on aPduprecordcopy right after the copy is made, and compiles every field other than__proto__exactly as before.jsx_preserve_test's element with spread props now lists its props in declaration order (className, thentitle), the same as on master.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.resis master's test. Without the fix, 7 of its 15 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, a throwing override, and both__proto__tests. The only other checked-in output that changes isjsx_preserve_test.mjs.Not fixed here: on 12.x an object literal of a record with a
__proto__field prints__proto__: v, which sets the prototype, where master prints["__proto__"]: v(see the test's{name: "own", proto: 1}argument). That comes from a different master change and doesn't affect this test's results.Investigated by Claude.