From 3afea67368e658982abbd9852027cd408aab0b43 Mon Sep 17 00:00:00 2001 From: Rob Date: Sat, 28 Nov 2026 13:10:34 -0500 Subject: [PATCH 1/2] Evaluate a record spread's overrides in order and define a __proto__ field as an own property (#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 83cf1de1984f1b00d45c63202a0fe032c079ce4f) Signed-off-by: Rob --- CHANGELOG.md | 1 + compiler/core/js_exp_make.ml | 15 + compiler/core/js_exp_make.mli | 5 + compiler/core/lam_compile.ml | 50 +- compiler/ml/translcore.ml | 6 +- tests/tests/src/jsx_preserve_test.mjs | 4 +- tests/tests/src/record_spread_order_test.mjs | 572 +++++++++++++++++++ tests/tests/src/record_spread_order_test.res | 340 +++++++++++ 8 files changed, 988 insertions(+), 5 deletions(-) create mode 100644 tests/tests/src/record_spread_order_test.mjs create mode 100644 tests/tests/src/record_spread_order_test.res diff --git a/CHANGELOG.md b/CHANGELOG.md index bb6a20bbe21..d5d6b4b9060 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - Integer patterns that alias the same int32 value (e.g. `0xFFFF_FFFF` and `-1`) may now produce unused-case warnings. https://github.com/rescript-lang/rescript/pull/8717 https://github.com/rescript-lang/rescript/pull/8858 - Fix constant folding of a `switch` picking the wrong case for `@as(int)` constructors and for coerced variants. https://github.com/rescript-lang/rescript/pull/9318 - Fix constant folding of comparisons between `@as(int)` constructors and other constructors, and merging of switch arms returning them. https://github.com/rescript-lang/rescript/pull/9319 +- Fix a record spread evaluating its overrides in reverse order, and defining a field renamed with `@as("__proto__")` as the prototype of the copy instead of an own property. https://github.com/rescript-lang/rescript/pull/9416 #### :nail_care: Polish diff --git a/compiler/core/js_exp_make.ml b/compiler/core/js_exp_make.ml index 1babd208a36..3b77fd0a97b 100644 --- a/compiler/core/js_exp_make.ml +++ b/compiler/core/js_exp_make.ml @@ -589,6 +589,21 @@ let rec string_append ?comment (e : t) (el : t) : t = let obj ?comment ?dup properties : t = {expression_desc = Object (dup, properties); comment} +let define_own_property (e : t) (name : string) (value : t) : t = + call ~info:Js_call_info.ml_full_call + (dot (js_global "Object") "defineProperty") + [ + e; + str name; + obj + [ + (Js_op.Lit "value", value); + (Js_op.Lit "writable", true_); + (Js_op.Lit "enumerable", true_); + (Js_op.Lit "configurable", true_); + ]; + ] + let str_equal (txt0 : string) (delim0 : External_arg_spec.delim) txt1 delim1 = if delim0 = delim1 then Ast_utf8_string.literals_equal txt0 txt1 else None diff --git a/compiler/core/js_exp_make.mli b/compiler/core/js_exp_make.mli index ec208532a59..18b2a18ea65 100644 --- a/compiler/core/js_exp_make.mli +++ b/compiler/core/js_exp_make.mli @@ -335,6 +335,11 @@ val fuse_to_seq : t -> t list -> t val obj : ?comment:string -> ?dup:J.expression -> J.property_map -> t +val define_own_property : t -> string -> t -> t +(** [Object.defineProperty(e, name, {value, ...})]: defines [name] as an own + data property of [e], as a property of an object literal does, where an + assignment would run a setter that [e] inherits *) + val true_ : t val false_ : t diff --git a/compiler/core/lam_compile.ml b/compiler/core/lam_compile.ml index 42844bc99c0..db9b007eacc 100644 --- a/compiler/core/lam_compile.ml +++ b/compiler/core/lam_compile.ml @@ -1779,6 +1779,48 @@ let compile output_prefix = in Js_output.output_of_block_and_expression lambda_cxt.continuation args_code exp + (* The fields set on the copy [copy_id] of a record right after it is made: + the overrides of a spread, and then the fields assigned to it by later + code. Each is an own property of the copy, as it is of an object literal. + Assigning a field makes one, except for [__proto__] when the base has no + own [__proto__]: the assignment runs the setter the copy inherits from + [Object.prototype], which sets the prototype of the copy, or does + nothing. So a [__proto__] field is defined on the copy instead. *) + and compile_copy_fields (lambda_cxt : Lam_compile_context.t) + (copy_id : Ident.t) (lam : Lam.t) : Js_output.t = + match lam with + | Lsequence + ( (Lprim {primitive = Psetfield (_, fld_info); args = [Lvar id'; value]} + as set), + rest ) + when Ident.same id' copy_id -> + let set_output = + match fld_info with + | Fld_record_set "__proto__" + | Fld_record_inline_set "__proto__" + | Fld_record_extension_set "__proto__" -> ( + let value_output = + compile_lambda + {lambda_cxt with continuation = NeedValue Not_tail} + value + in + match value_output.value with + | Some value -> + Js_output.make + (value_output.block + @ [ + S.exp (E.define_own_property (E.var copy_id) "__proto__" value); + ]) + | None -> assert false) + | Fld_record_set _ | Fld_record_inline_set _ + | Fld_record_extension_set _ -> + compile_lambda + {lambda_cxt with continuation = EffectCall Not_tail} + set + in + Js_output.append_output set_output + (compile_copy_fields lambda_cxt copy_id rest) + | _ -> compile_lambda lambda_cxt lam and compile_lambda (lambda_cxt : Lam_compile_context.t) (cur_lam : Lam.t) : Js_output.t = match cur_lam with @@ -1809,7 +1851,13 @@ let compile output_prefix = {lambda_cxt with continuation = Declare (let_kind, id)} arg in - Js_output.append_output args_code (compile_lambda lambda_cxt body) + let body_code = + match arg with + | Lprim {primitive = Pduprecord} -> + compile_copy_fields lambda_cxt id body + | _ -> compile_lambda lambda_cxt body + in + Js_output.append_output args_code body_code | Lletrec (id_args, body) -> (* There is a bug in our current design, it requires compile args first (register that some objects are jsidentifiers) diff --git a/compiler/ml/translcore.ml b/compiler/ml/translcore.ml index 199c90ea718..d1926c4f77a 100644 --- a/compiler/ml/translcore.ml +++ b/compiler/ml/translcore.ml @@ -1216,7 +1216,7 @@ and transl_record loc env fields repres opt_init_expr = (* Take a shallow copy of the init record, then mutate the fields of the copy *) let copy_id = Ident.create "newrecord" in - let update_field cont (lbl, definition, _opt) = + let update_field (lbl, definition, _opt) cont = match definition with | Kept _type -> cont | Overridden (_lid, expr) -> @@ -1241,7 +1241,9 @@ and transl_record loc env fields repres opt_init_expr = Pgenval, copy_id, Lprim (Pduprecord, [transl_exp init_expr], loc), - Array.fold_left update_field (Lvar copy_id) fields )) + (* The fields are set in declaration order, so the overrides are + evaluated in that order *) + Array.fold_right update_field fields (Lvar copy_id) )) and transl_match e arg pat_expr_list exn_pat_expr_list partial = let id = Typecore.name_pattern "exn" exn_pat_expr_list diff --git a/tests/tests/src/jsx_preserve_test.mjs b/tests/tests/src/jsx_preserve_test.mjs index 570ca4607cf..288192e52bc 100644 --- a/tests/tests/src/jsx_preserve_test.mjs +++ b/tests/tests/src/jsx_preserve_test.mjs @@ -65,8 +65,8 @@ let newrecord$1 = {...baseProps}; let _container_with_spread_props =
{"Hello, world!"} {"Hello, world!"} { + Mocha.test("an override that mutates the base", () => Test_utils.eq("File \"record_spread_order_test.res\", line 235, characters 7-14", mutateInOverride({ + a: 1, + b: 2 + }).a, 1)); + Mocha.test("evaluation order", () => { + clearOrder(); + traced({ + a: 1, + b: 2 + }); + Test_utils.eq("File \"record_spread_order_test.res\", line 241, characters 7-14", order, [ + "base", + "b" + ]); + }); + Mocha.test("evaluation order with several overrides", () => { + clearOrder(); + let res = tracedTwoOverrides({ + a: 1, + b: 2 + }); + Test_utils.eq("File \"record_spread_order_test.res\", line 247, characters 7-14", order, [ + "base2", + "a2", + "b2" + ]); + Test_utils.eq("File \"record_spread_order_test.res\", line 248, characters 7-14", [ + res.a, + res.b + ], [ + 7, + 5 + ]); + }); + Mocha.test("an expression override", () => { + let base = { + a: 1, + b: 2 + }; + let res = simpleOverride(base); + Test_utils.eq("File \"record_spread_order_test.res\", line 254, characters 7-14", [ + res.a, + res.b, + base.b + ], [ + 1, + 9, + 2 + ]); + }); + Mocha.test("a copy that is bound and then used", () => { + clearOrder(); + let res = boundThenUsed({ + a: 0, + b: 0 + }); + Test_utils.eq("File \"record_spread_order_test.res\", line 260, characters 7-14", order, [ + "a", + "b" + ]); + Test_utils.eq("File \"record_spread_order_test.res\", line 261, characters 7-14", [ + res.a, + res.b + ], [ + 1, + 2 + ]); + clearOrder(); + let res$1 = boundThenUsedTry({ + a: 0, + b: 0 + }); + Test_utils.eq("File \"record_spread_order_test.res\", line 264, characters 7-14", order, [ + "a", + "b" + ]); + Test_utils.eq("File \"record_spread_order_test.res\", line 265, characters 7-14", [ + res$1.a, + res$1.b + ], [ + 1, + 2 + ]); + }); + Mocha.test("fields assigned after the spread", () => { + clearOrder(); + Test_utils.eq("File \"record_spread_order_test.res\", line 270, characters 7-14", assignAfter({ + x: 0, + y: 0 + }), { + x: 2, + y: 1, + z: 1 + }); + Test_utils.eq("File \"record_spread_order_test.res\", line 271, characters 7-14", order, [ + "first", + "second" + ]); + clearOrder(); + Test_utils.eq("File \"record_spread_order_test.res\", line 273, characters 7-14", assignAfterExpression({ + x: 0, + y: 0 + }), { + x: 2, + y: 1, + z: 1 + }); + Test_utils.eq("File \"record_spread_order_test.res\", line 274, characters 7-14", order, [ + "first", + "second" + ]); + }); + Mocha.test("a field assigned a value that reads the copy", () => { + Test_utils.eq("File \"record_spread_order_test.res\", line 278, characters 7-14", readCopy({ + x: 0, + y: 0 + }), { + x: 1, + y: 2 + }); + Test_utils.eq("File \"record_spread_order_test.res\", line 279, characters 7-14", readCopyInSwitch({ + x: 1, + y: 2 + }), { + x: 10, + y: 5 + }); + }); + Mocha.test("the last assignment of a field wins", () => { + Test_utils.eq("File \"record_spread_order_test.res\", line 283, characters 7-14", assignTwice({ + x: 0, + y: 0 + }).x, 2); + Test_utils.eq("File \"record_spread_order_test.res\", line 284, characters 7-14", assignTwiceTry({ + x: 0, + y: 0 + }).x, 4); + }); + Mocha.test("overrides whose statements are pure", () => { + let base = { + a: 1, + b: 2 + }; + Test_utils.eq("File \"record_spread_order_test.res\", line 289, characters 7-14", pureStatements(base, { + a: 3, + b: 4 + }), { + a: 1, + b: 8 + }); + Test_utils.eq("File \"record_spread_order_test.res\", line 290, characters 7-14", pureStatementsAfterCall(base, { + a: 3, + b: 4 + }), { + a: 1, + b: 200 + }); + Test_utils.eq("File \"record_spread_order_test.res\", line 291, characters 7-14", callThenPureStatements(base, { + a: 3, + b: 4 + }), { + a: 1, + b: 200 + }); + }); + Mocha.test("a discarded result", () => { + clearOrder(); + Test_utils.eq("File \"record_spread_order_test.res\", line 296, characters 7-14", discarded({ + a: 1, + b: 2 + }), 1); + Test_utils.eq("File \"record_spread_order_test.res\", line 297, characters 7-14", order, [ + "a", + "b" + ]); + }); + Mocha.test("an override that throws", () => { + clearOrder(); + Test_utils.throws("File \"record_spread_order_test.res\", line 302, characters 11-18", () => { + throwing({ + a: 1, + b: 2 + }); + }); + Test_utils.eq("File \"record_spread_order_test.res\", line 303, characters 7-14", order, [ + "base", + "a" + ]); + }); + Mocha.test("an inline record", () => { + let match = inlineRecord({ + TAG: "Inline", + a: 1, + b: 2 + }); + if (typeof match !== "object") { + return Test_utils.ok("File \"record_spread_order_test.res\", line 309, characters 18-25", false); + } + let a = match.a; + Test_utils.eq("File \"record_spread_order_test.res\", line 308, characters 27-34", [ + a, + match.b + ], [ + 1, + 5 + ]); + }); + Mocha.test("an override of a field named __proto__ is an own property", () => { + let withInt = protoOverride({ + name: "int" + }, 5); + Test_utils.ok("File \"record_spread_order_test.res\", line 315, characters 7-14", Object.hasOwn(withInt, "__proto__")); + Test_utils.eq("File \"record_spread_order_test.res\", line 316, characters 7-14", withInt.__proto__, 5); + let withObject = protoOverride({ + name: "object" + }, { + z: 1 + }); + Test_utils.ok("File \"record_spread_order_test.res\", line 318, characters 7-14", Object.hasOwn(withObject, "__proto__")); + Test_utils.eq("File \"record_spread_order_test.res\", line 319, characters 7-14", withObject.__proto__, { + z: 1 + }); + Test_utils.ok("File \"record_spread_order_test.res\", line 320, characters 7-14", Object.getPrototypeOf(withObject) === Object.prototype); + let overridden = protoOverride({ + name: "own", + __proto__: 1 + }, 2); + Test_utils.eq("File \"record_spread_order_test.res\", line 322, characters 7-14", overridden.__proto__, 2); + }); + Mocha.test("a __proto__ field set on the copy is an own property", () => { + let assigned = protoAssignedAfter({ + name: "assigned" + }); + Test_utils.ok("File \"record_spread_order_test.res\", line 327, characters 7-14", Object.hasOwn(assigned, "__proto__")); + Test_utils.eq("File \"record_spread_order_test.res\", line 328, characters 7-14", assigned.__proto__, 5); + let used = protoUsedAfter({ + name: "used" + }); + Test_utils.ok("File \"record_spread_order_test.res\", line 330, characters 7-14", Object.hasOwn(used, "__proto__")); + Test_utils.eq("File \"record_spread_order_test.res\", line 331, characters 7-14", used.__proto__, 5); + }); + Mocha.test("an exception's record", () => { + let match = exceptionRecord({ + RE_EXN_ID: Extension, + a: 1, + b: 2, + c: undefined + }); + if (match.RE_EXN_ID !== Extension) { + return Test_utils.ok("File \"record_spread_order_test.res\", line 337, characters 14-21", false); + } + let a = match.a; + Test_utils.eq("File \"record_spread_order_test.res\", line 336, characters 30-37", [ + a, + match.b + ], [ + 1, + 5 + ]); + }); +}); + +export { + id, + mutateInOverride, + order, + trace, + traced, + tracedTwoOverrides, + boundThenUsed, + boundThenUsedTry, + assignAfter, + assignAfterExpression, + readCopy, + readCopyInSwitch, + assignTwice, + assignTwiceTry, + pureStatements, + pureStatementsAfterCall, + callThenPureStatements, + discarded, + throwing, + inlineRecord, + Extension, + exceptionRecord, + clearOrder, + simpleOverride, + protoOverride, + protoAssignedAfter, + protoUsedAfter, +} +/* Not a pure module */ diff --git a/tests/tests/src/record_spread_order_test.res b/tests/tests/src/record_spread_order_test.res new file mode 100644 index 00000000000..285fae084ac --- /dev/null +++ b/tests/tests/src/record_spread_order_test.res @@ -0,0 +1,340 @@ +open Mocha +open Test_utils + +/*** +`{...base, b: e}` copies `base` before `e` is evaluated, also when `e` needs +statements and the record has an optional field. +*/ + +@inline(never) let id = x => x + +type r = {mutable a: int, b: int, c?: int} + +let mutateInOverride = (base: r) => { + ...base, + b: try { + base.a = 100 + id(5) + } catch { + | _ => 0 + }, +} + +let order = [] + +@inline(never) +let trace = (name, value) => { + order->Array.push(name) + value +} + +let traced = (base: r) => { + ...trace("base", base), + b: try { + trace("b", id(5)) + } catch { + | _ => 0 + }, +} + +let tracedTwoOverrides = (base: r) => { + ...trace("base2", base), + a: trace("a2", 7), + b: switch id(base.a) { + | 1 => trace("b2", 5) + | n => n + }, +} + +let boundThenUsed = (base: r) => { + let x = {...base, a: trace("a", 1), b: trace("b", 2)} + ignore(id(x.a)) + x +} + +let boundThenUsedTry = (base: r) => { + let x = { + ...base, + a: try trace("a", id(1)) catch { + | _ => 0 + }, + b: try trace("b", id(2)) catch { + | _ => 0 + }, + } + ignore(id(x.a)) + x +} + +type m = {mutable x: int, mutable y: int, z?: int} + +let assignAfter = (base: m) => { + let copy = {...base, z: 1} + copy.y = try trace("first", id(1)) catch { + | _ => 0 + } + copy.x = try trace("second", id(2)) catch { + | _ => 0 + } + copy +} + +let assignAfterExpression = (base: m) => { + let copy = {...base, z: 1} + copy.y = trace("first", 1) + copy.x = trace("second", 2) + copy +} + +let readCopy = (base: m) => { + let copy = {...base, x: 1} + copy.y = copy.x + 1 + copy +} + +let readCopyInSwitch = (base: m) => { + let copy = {...base, x: 10} + copy.y = switch id(copy.x) { + | 10 => trace("ten", 5) + | n => n + } + copy +} + +let assignTwice = (base: m) => { + let copy = {...base, x: 1} + copy.x = 2 + copy +} + +let assignTwiceTry = (base: m) => { + let copy = { + ...base, + x: try id(3) catch { + | _ => 0 + }, + } + copy.x = 4 + copy +} + +/* The statements only define pure values */ +let pureStatements = (base: r, x: r) => { + ...base, + b: { + let n = x.b + n + n + }, +} + +/* The base is evaluated before the read of [x.a] */ +let pureStatementsAfterCall = (base: r, x: r) => { + ...{ + x.a = 100 + id(base) + }, + b: { + let n = x.a + n + n + }, +} + +let callThenPureStatements = (base: r, x: r) => { + ...base, + a: { + x.a = 100 + id(1) + }, + b: { + let n = x.a + n + n + }, +} + +/* A result that isn't used */ +let discarded = (base: r) => { + ignore({ + ...base, + a: trace("a", 1), + b: try trace("b", id(5)) catch { + | _ => 0 + }, + }) + 1 +} + +let throwing = (base: r) => {...trace("base", base), a: trace("a", 1), b: throw(Not_found)} + +type v = Inline({mutable a: int, b: int, c?: int}) | Empty + +let inlineRecord = v => + switch v { + | Inline(x) => + Inline({ + ...x, + b: try { + x.a = 100 + id(5) + } catch { + | _ => 0 + }, + }) + | Empty => Empty + } + +exception Extension({mutable a: int, b: int, c?: int}) + +let exceptionRecord = e => + switch e { + | Extension(x) => + Extension({ + ...x, + b: try { + x.a = 100 + id(5) + } catch { + | _ => 0 + }, + }) + | e => e + } + +let clearOrder = () => order->Array.splice(~start=0, ~remove=Array.length(order), ~insert=[]) + +let simpleOverride = (base: r) => {...base, b: id(9)} + +@val external hasOwn: ('a, string) => bool = "Object.hasOwn" +@val external getPrototypeOf: 'a => 'b = "Object.getPrototypeOf" +@val external objectPrototype: 'a = "Object.prototype" + +type proto<'a> = {name: string, @as("__proto__") proto?: 'a} + +let protoOverride = (base: proto<'a>, value: 'a) => { + ...base, + proto: try id(value) catch { + | _ => value + }, +} + +type mutableProto = {name: string, @as("__proto__") mutable mutableProto?: int} + +let protoAssignedAfter = (base: mutableProto) => { + let x = {...base, mutableProto: 5} + x.mutableProto = Some(5) + x +} + +let protoUsedAfter = (base: mutableProto) => { + let x = {...base, mutableProto: 5} + ignore(id(x.name)) + x +} + +describe(__MODULE__, () => { + test("an override that mutates the base", () => { + eq(__LOC__, mutateInOverride({a: 1, b: 2}).a, 1) + }) + + test("evaluation order", () => { + clearOrder() + ignore(traced({a: 1, b: 2})) + eq(__LOC__, order, ["base", "b"]) + }) + + test("evaluation order with several overrides", () => { + clearOrder() + let res = tracedTwoOverrides({a: 1, b: 2}) + eq(__LOC__, order, ["base2", "a2", "b2"]) + eq(__LOC__, (res.a, res.b), (7, 5)) + }) + + test("an expression override", () => { + let base = {a: 1, b: 2} + let res = simpleOverride(base) + eq(__LOC__, (res.a, res.b, base.b), (1, 9, 2)) + }) + + test("a copy that is bound and then used", () => { + clearOrder() + let res = boundThenUsed({a: 0, b: 0}) + eq(__LOC__, order, ["a", "b"]) + eq(__LOC__, (res.a, res.b), (1, 2)) + clearOrder() + let res = boundThenUsedTry({a: 0, b: 0}) + eq(__LOC__, order, ["a", "b"]) + eq(__LOC__, (res.a, res.b), (1, 2)) + }) + + test("fields assigned after the spread", () => { + clearOrder() + eq(__LOC__, assignAfter({x: 0, y: 0}), {x: 2, y: 1, z: 1}) + eq(__LOC__, order, ["first", "second"]) + clearOrder() + eq(__LOC__, assignAfterExpression({x: 0, y: 0}), {x: 2, y: 1, z: 1}) + eq(__LOC__, order, ["first", "second"]) + }) + + test("a field assigned a value that reads the copy", () => { + eq(__LOC__, readCopy({x: 0, y: 0}), {x: 1, y: 2}) + eq(__LOC__, readCopyInSwitch({x: 1, y: 2}), {x: 10, y: 5}) + }) + + test("the last assignment of a field wins", () => { + eq(__LOC__, assignTwice({x: 0, y: 0}).x, 2) + eq(__LOC__, assignTwiceTry({x: 0, y: 0}).x, 4) + }) + + test("overrides whose statements are pure", () => { + let base = {a: 1, b: 2} + eq(__LOC__, pureStatements(base, {a: 3, b: 4}), {a: 1, b: 8}) + eq(__LOC__, pureStatementsAfterCall(base, {a: 3, b: 4}), {a: 1, b: 200}) + eq(__LOC__, callThenPureStatements(base, {a: 3, b: 4}), {a: 1, b: 200}) + }) + + test("a discarded result", () => { + clearOrder() + eq(__LOC__, discarded({a: 1, b: 2}), 1) + eq(__LOC__, order, ["a", "b"]) + }) + + test("an override that throws", () => { + clearOrder() + throws(__LOC__, () => throwing({a: 1, b: 2})->ignore) + eq(__LOC__, order, ["base", "a"]) + }) + + test("an inline record", () => + switch inlineRecord(Inline({a: 1, b: 2})) { + | Inline({a, b}) => eq(__LOC__, (a, b), (1, 5)) + | Empty => ok(__LOC__, false) + } + ) + + test("an override of a field named __proto__ is an own property", () => { + let withInt = protoOverride({name: "int"}, 5) + ok(__LOC__, hasOwn(withInt, "__proto__")) + eq(__LOC__, withInt.proto, Some(5)) + let withObject = protoOverride({name: "object"}, {"z": 1}) + ok(__LOC__, hasOwn(withObject, "__proto__")) + eq(__LOC__, withObject.proto, Some({"z": 1})) + ok(__LOC__, getPrototypeOf(withObject) === objectPrototype) + let overridden = protoOverride({name: "own", proto: 1}, 2) + eq(__LOC__, overridden.proto, Some(2)) + }) + + test("a __proto__ field set on the copy is an own property", () => { + let assigned = protoAssignedAfter({name: "assigned"}) + ok(__LOC__, hasOwn(assigned, "__proto__")) + eq(__LOC__, assigned.mutableProto, Some(5)) + let used = protoUsedAfter({name: "used"}) + ok(__LOC__, hasOwn(used, "__proto__")) + eq(__LOC__, used.mutableProto, Some(5)) + }) + + test("an exception's record", () => + switch exceptionRecord(Extension({a: 1, b: 2})) { + | Extension({a, b}) => eq(__LOC__, (a, b), (1, 5)) + | _ => ok(__LOC__, false) + } + ) +}) From f29d198b5a142251d0c4c5a44f6fa3ed1094c698 Mon Sep 17 00:00:00 2001 From: Rob Date: Thu, 3 Dec 2026 20:21:18 -0500 Subject: [PATCH 2/2] Drop __proto__ handling from the 12.x backport Keep only the override-order fix from #9416, as requested on #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 --- CHANGELOG.md | 2 +- compiler/core/js_exp_make.ml | 15 -- compiler/core/js_exp_make.mli | 5 - compiler/core/lam_compile.ml | 50 +------ tests/tests/src/record_spread_order_test.mjs | 140 ++++--------------- tests/tests/src/record_spread_order_test.res | 48 ------- 6 files changed, 30 insertions(+), 230 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5d6b4b9060..d94c17de03b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ - Integer patterns that alias the same int32 value (e.g. `0xFFFF_FFFF` and `-1`) may now produce unused-case warnings. https://github.com/rescript-lang/rescript/pull/8717 https://github.com/rescript-lang/rescript/pull/8858 - Fix constant folding of a `switch` picking the wrong case for `@as(int)` constructors and for coerced variants. https://github.com/rescript-lang/rescript/pull/9318 - Fix constant folding of comparisons between `@as(int)` constructors and other constructors, and merging of switch arms returning them. https://github.com/rescript-lang/rescript/pull/9319 -- Fix a record spread evaluating its overrides in reverse order, and defining a field renamed with `@as("__proto__")` as the prototype of the copy instead of an own property. https://github.com/rescript-lang/rescript/pull/9416 +- Fix a record spread evaluating its overrides in reverse order. https://github.com/rescript-lang/rescript/pull/9416 #### :nail_care: Polish diff --git a/compiler/core/js_exp_make.ml b/compiler/core/js_exp_make.ml index 3b77fd0a97b..1babd208a36 100644 --- a/compiler/core/js_exp_make.ml +++ b/compiler/core/js_exp_make.ml @@ -589,21 +589,6 @@ let rec string_append ?comment (e : t) (el : t) : t = let obj ?comment ?dup properties : t = {expression_desc = Object (dup, properties); comment} -let define_own_property (e : t) (name : string) (value : t) : t = - call ~info:Js_call_info.ml_full_call - (dot (js_global "Object") "defineProperty") - [ - e; - str name; - obj - [ - (Js_op.Lit "value", value); - (Js_op.Lit "writable", true_); - (Js_op.Lit "enumerable", true_); - (Js_op.Lit "configurable", true_); - ]; - ] - let str_equal (txt0 : string) (delim0 : External_arg_spec.delim) txt1 delim1 = if delim0 = delim1 then Ast_utf8_string.literals_equal txt0 txt1 else None diff --git a/compiler/core/js_exp_make.mli b/compiler/core/js_exp_make.mli index 18b2a18ea65..ec208532a59 100644 --- a/compiler/core/js_exp_make.mli +++ b/compiler/core/js_exp_make.mli @@ -335,11 +335,6 @@ val fuse_to_seq : t -> t list -> t val obj : ?comment:string -> ?dup:J.expression -> J.property_map -> t -val define_own_property : t -> string -> t -> t -(** [Object.defineProperty(e, name, {value, ...})]: defines [name] as an own - data property of [e], as a property of an object literal does, where an - assignment would run a setter that [e] inherits *) - val true_ : t val false_ : t diff --git a/compiler/core/lam_compile.ml b/compiler/core/lam_compile.ml index db9b007eacc..42844bc99c0 100644 --- a/compiler/core/lam_compile.ml +++ b/compiler/core/lam_compile.ml @@ -1779,48 +1779,6 @@ let compile output_prefix = in Js_output.output_of_block_and_expression lambda_cxt.continuation args_code exp - (* The fields set on the copy [copy_id] of a record right after it is made: - the overrides of a spread, and then the fields assigned to it by later - code. Each is an own property of the copy, as it is of an object literal. - Assigning a field makes one, except for [__proto__] when the base has no - own [__proto__]: the assignment runs the setter the copy inherits from - [Object.prototype], which sets the prototype of the copy, or does - nothing. So a [__proto__] field is defined on the copy instead. *) - and compile_copy_fields (lambda_cxt : Lam_compile_context.t) - (copy_id : Ident.t) (lam : Lam.t) : Js_output.t = - match lam with - | Lsequence - ( (Lprim {primitive = Psetfield (_, fld_info); args = [Lvar id'; value]} - as set), - rest ) - when Ident.same id' copy_id -> - let set_output = - match fld_info with - | Fld_record_set "__proto__" - | Fld_record_inline_set "__proto__" - | Fld_record_extension_set "__proto__" -> ( - let value_output = - compile_lambda - {lambda_cxt with continuation = NeedValue Not_tail} - value - in - match value_output.value with - | Some value -> - Js_output.make - (value_output.block - @ [ - S.exp (E.define_own_property (E.var copy_id) "__proto__" value); - ]) - | None -> assert false) - | Fld_record_set _ | Fld_record_inline_set _ - | Fld_record_extension_set _ -> - compile_lambda - {lambda_cxt with continuation = EffectCall Not_tail} - set - in - Js_output.append_output set_output - (compile_copy_fields lambda_cxt copy_id rest) - | _ -> compile_lambda lambda_cxt lam and compile_lambda (lambda_cxt : Lam_compile_context.t) (cur_lam : Lam.t) : Js_output.t = match cur_lam with @@ -1851,13 +1809,7 @@ let compile output_prefix = {lambda_cxt with continuation = Declare (let_kind, id)} arg in - let body_code = - match arg with - | Lprim {primitive = Pduprecord} -> - compile_copy_fields lambda_cxt id body - | _ -> compile_lambda lambda_cxt body - in - Js_output.append_output args_code body_code + Js_output.append_output args_code (compile_lambda lambda_cxt body) | Lletrec (id_args, body) -> (* There is a bug in our current design, it requires compile args first (register that some objects are jsidentifiers) diff --git a/tests/tests/src/record_spread_order_test.mjs b/tests/tests/src/record_spread_order_test.mjs index c48f8eb7f6a..659f50382cb 100644 --- a/tests/tests/src/record_spread_order_test.mjs +++ b/tests/tests/src/record_spread_order_test.mjs @@ -2,7 +2,6 @@ import * as Mocha from "mocha"; import * as Test_utils from "./test_utils.mjs"; -import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js"; import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js"; function id(x) { @@ -231,54 +230,8 @@ function simpleOverride(base) { return newrecord; } -function protoOverride(base, value) { - let newrecord = {...base}; - let tmp; - try { - tmp = id(value); - } catch (exn) { - tmp = value; - } - Object.defineProperty(newrecord, "__proto__", { - value: Primitive_option.some(tmp), - writable: true, - enumerable: true, - configurable: true - }); - return newrecord; -} - -function protoAssignedAfter(base) { - let newrecord = {...base}; - Object.defineProperty(newrecord, "__proto__", { - value: 5, - writable: true, - enumerable: true, - configurable: true - }); - Object.defineProperty(newrecord, "__proto__", { - value: 5, - writable: true, - enumerable: true, - configurable: true - }); - return newrecord; -} - -function protoUsedAfter(base) { - let newrecord = {...base}; - Object.defineProperty(newrecord, "__proto__", { - value: 5, - writable: true, - enumerable: true, - configurable: true - }); - id(newrecord.name); - return newrecord; -} - Mocha.describe("Record_spread_order_test", () => { - Mocha.test("an override that mutates the base", () => Test_utils.eq("File \"record_spread_order_test.res\", line 235, characters 7-14", mutateInOverride({ + Mocha.test("an override that mutates the base", () => Test_utils.eq("File \"record_spread_order_test.res\", line 208, characters 7-14", mutateInOverride({ a: 1, b: 2 }).a, 1)); @@ -288,7 +241,7 @@ Mocha.describe("Record_spread_order_test", () => { a: 1, b: 2 }); - Test_utils.eq("File \"record_spread_order_test.res\", line 241, characters 7-14", order, [ + Test_utils.eq("File \"record_spread_order_test.res\", line 214, characters 7-14", order, [ "base", "b" ]); @@ -299,12 +252,12 @@ Mocha.describe("Record_spread_order_test", () => { a: 1, b: 2 }); - Test_utils.eq("File \"record_spread_order_test.res\", line 247, characters 7-14", order, [ + Test_utils.eq("File \"record_spread_order_test.res\", line 220, characters 7-14", order, [ "base2", "a2", "b2" ]); - Test_utils.eq("File \"record_spread_order_test.res\", line 248, characters 7-14", [ + Test_utils.eq("File \"record_spread_order_test.res\", line 221, characters 7-14", [ res.a, res.b ], [ @@ -318,7 +271,7 @@ Mocha.describe("Record_spread_order_test", () => { b: 2 }; let res = simpleOverride(base); - Test_utils.eq("File \"record_spread_order_test.res\", line 254, characters 7-14", [ + Test_utils.eq("File \"record_spread_order_test.res\", line 227, characters 7-14", [ res.a, res.b, base.b @@ -334,11 +287,11 @@ Mocha.describe("Record_spread_order_test", () => { a: 0, b: 0 }); - Test_utils.eq("File \"record_spread_order_test.res\", line 260, characters 7-14", order, [ + Test_utils.eq("File \"record_spread_order_test.res\", line 233, characters 7-14", order, [ "a", "b" ]); - Test_utils.eq("File \"record_spread_order_test.res\", line 261, characters 7-14", [ + Test_utils.eq("File \"record_spread_order_test.res\", line 234, characters 7-14", [ res.a, res.b ], [ @@ -350,11 +303,11 @@ Mocha.describe("Record_spread_order_test", () => { a: 0, b: 0 }); - Test_utils.eq("File \"record_spread_order_test.res\", line 264, characters 7-14", order, [ + Test_utils.eq("File \"record_spread_order_test.res\", line 237, characters 7-14", order, [ "a", "b" ]); - Test_utils.eq("File \"record_spread_order_test.res\", line 265, characters 7-14", [ + Test_utils.eq("File \"record_spread_order_test.res\", line 238, characters 7-14", [ res$1.a, res$1.b ], [ @@ -364,7 +317,7 @@ Mocha.describe("Record_spread_order_test", () => { }); Mocha.test("fields assigned after the spread", () => { clearOrder(); - Test_utils.eq("File \"record_spread_order_test.res\", line 270, characters 7-14", assignAfter({ + Test_utils.eq("File \"record_spread_order_test.res\", line 243, characters 7-14", assignAfter({ x: 0, y: 0 }), { @@ -372,12 +325,12 @@ Mocha.describe("Record_spread_order_test", () => { y: 1, z: 1 }); - Test_utils.eq("File \"record_spread_order_test.res\", line 271, characters 7-14", order, [ + Test_utils.eq("File \"record_spread_order_test.res\", line 244, characters 7-14", order, [ "first", "second" ]); clearOrder(); - Test_utils.eq("File \"record_spread_order_test.res\", line 273, characters 7-14", assignAfterExpression({ + Test_utils.eq("File \"record_spread_order_test.res\", line 246, characters 7-14", assignAfterExpression({ x: 0, y: 0 }), { @@ -385,20 +338,20 @@ Mocha.describe("Record_spread_order_test", () => { y: 1, z: 1 }); - Test_utils.eq("File \"record_spread_order_test.res\", line 274, characters 7-14", order, [ + Test_utils.eq("File \"record_spread_order_test.res\", line 247, characters 7-14", order, [ "first", "second" ]); }); Mocha.test("a field assigned a value that reads the copy", () => { - Test_utils.eq("File \"record_spread_order_test.res\", line 278, characters 7-14", readCopy({ + Test_utils.eq("File \"record_spread_order_test.res\", line 251, characters 7-14", readCopy({ x: 0, y: 0 }), { x: 1, y: 2 }); - Test_utils.eq("File \"record_spread_order_test.res\", line 279, characters 7-14", readCopyInSwitch({ + Test_utils.eq("File \"record_spread_order_test.res\", line 252, characters 7-14", readCopyInSwitch({ x: 1, y: 2 }), { @@ -407,11 +360,11 @@ Mocha.describe("Record_spread_order_test", () => { }); }); Mocha.test("the last assignment of a field wins", () => { - Test_utils.eq("File \"record_spread_order_test.res\", line 283, characters 7-14", assignTwice({ + Test_utils.eq("File \"record_spread_order_test.res\", line 256, characters 7-14", assignTwice({ x: 0, y: 0 }).x, 2); - Test_utils.eq("File \"record_spread_order_test.res\", line 284, characters 7-14", assignTwiceTry({ + Test_utils.eq("File \"record_spread_order_test.res\", line 257, characters 7-14", assignTwiceTry({ x: 0, y: 0 }).x, 4); @@ -421,21 +374,21 @@ Mocha.describe("Record_spread_order_test", () => { a: 1, b: 2 }; - Test_utils.eq("File \"record_spread_order_test.res\", line 289, characters 7-14", pureStatements(base, { + Test_utils.eq("File \"record_spread_order_test.res\", line 262, characters 7-14", pureStatements(base, { a: 3, b: 4 }), { a: 1, b: 8 }); - Test_utils.eq("File \"record_spread_order_test.res\", line 290, characters 7-14", pureStatementsAfterCall(base, { + Test_utils.eq("File \"record_spread_order_test.res\", line 263, characters 7-14", pureStatementsAfterCall(base, { a: 3, b: 4 }), { a: 1, b: 200 }); - Test_utils.eq("File \"record_spread_order_test.res\", line 291, characters 7-14", callThenPureStatements(base, { + Test_utils.eq("File \"record_spread_order_test.res\", line 264, characters 7-14", callThenPureStatements(base, { a: 3, b: 4 }), { @@ -445,24 +398,24 @@ Mocha.describe("Record_spread_order_test", () => { }); Mocha.test("a discarded result", () => { clearOrder(); - Test_utils.eq("File \"record_spread_order_test.res\", line 296, characters 7-14", discarded({ + Test_utils.eq("File \"record_spread_order_test.res\", line 269, characters 7-14", discarded({ a: 1, b: 2 }), 1); - Test_utils.eq("File \"record_spread_order_test.res\", line 297, characters 7-14", order, [ + Test_utils.eq("File \"record_spread_order_test.res\", line 270, characters 7-14", order, [ "a", "b" ]); }); Mocha.test("an override that throws", () => { clearOrder(); - Test_utils.throws("File \"record_spread_order_test.res\", line 302, characters 11-18", () => { + Test_utils.throws("File \"record_spread_order_test.res\", line 275, characters 11-18", () => { throwing({ a: 1, b: 2 }); }); - Test_utils.eq("File \"record_spread_order_test.res\", line 303, characters 7-14", order, [ + Test_utils.eq("File \"record_spread_order_test.res\", line 276, characters 7-14", order, [ "base", "a" ]); @@ -474,10 +427,10 @@ Mocha.describe("Record_spread_order_test", () => { b: 2 }); if (typeof match !== "object") { - return Test_utils.ok("File \"record_spread_order_test.res\", line 309, characters 18-25", false); + return Test_utils.ok("File \"record_spread_order_test.res\", line 282, characters 18-25", false); } let a = match.a; - Test_utils.eq("File \"record_spread_order_test.res\", line 308, characters 27-34", [ + Test_utils.eq("File \"record_spread_order_test.res\", line 281, characters 27-34", [ a, match.b ], [ @@ -485,40 +438,6 @@ Mocha.describe("Record_spread_order_test", () => { 5 ]); }); - Mocha.test("an override of a field named __proto__ is an own property", () => { - let withInt = protoOverride({ - name: "int" - }, 5); - Test_utils.ok("File \"record_spread_order_test.res\", line 315, characters 7-14", Object.hasOwn(withInt, "__proto__")); - Test_utils.eq("File \"record_spread_order_test.res\", line 316, characters 7-14", withInt.__proto__, 5); - let withObject = protoOverride({ - name: "object" - }, { - z: 1 - }); - Test_utils.ok("File \"record_spread_order_test.res\", line 318, characters 7-14", Object.hasOwn(withObject, "__proto__")); - Test_utils.eq("File \"record_spread_order_test.res\", line 319, characters 7-14", withObject.__proto__, { - z: 1 - }); - Test_utils.ok("File \"record_spread_order_test.res\", line 320, characters 7-14", Object.getPrototypeOf(withObject) === Object.prototype); - let overridden = protoOverride({ - name: "own", - __proto__: 1 - }, 2); - Test_utils.eq("File \"record_spread_order_test.res\", line 322, characters 7-14", overridden.__proto__, 2); - }); - Mocha.test("a __proto__ field set on the copy is an own property", () => { - let assigned = protoAssignedAfter({ - name: "assigned" - }); - Test_utils.ok("File \"record_spread_order_test.res\", line 327, characters 7-14", Object.hasOwn(assigned, "__proto__")); - Test_utils.eq("File \"record_spread_order_test.res\", line 328, characters 7-14", assigned.__proto__, 5); - let used = protoUsedAfter({ - name: "used" - }); - Test_utils.ok("File \"record_spread_order_test.res\", line 330, characters 7-14", Object.hasOwn(used, "__proto__")); - Test_utils.eq("File \"record_spread_order_test.res\", line 331, characters 7-14", used.__proto__, 5); - }); Mocha.test("an exception's record", () => { let match = exceptionRecord({ RE_EXN_ID: Extension, @@ -527,10 +446,10 @@ Mocha.describe("Record_spread_order_test", () => { c: undefined }); if (match.RE_EXN_ID !== Extension) { - return Test_utils.ok("File \"record_spread_order_test.res\", line 337, characters 14-21", false); + return Test_utils.ok("File \"record_spread_order_test.res\", line 289, characters 14-21", false); } let a = match.a; - Test_utils.eq("File \"record_spread_order_test.res\", line 336, characters 30-37", [ + Test_utils.eq("File \"record_spread_order_test.res\", line 288, characters 30-37", [ a, match.b ], [ @@ -565,8 +484,5 @@ export { exceptionRecord, clearOrder, simpleOverride, - protoOverride, - protoAssignedAfter, - protoUsedAfter, } /* Not a pure module */ diff --git a/tests/tests/src/record_spread_order_test.res b/tests/tests/src/record_spread_order_test.res index 285fae084ac..d6b64595afd 100644 --- a/tests/tests/src/record_spread_order_test.res +++ b/tests/tests/src/record_spread_order_test.res @@ -203,33 +203,6 @@ let clearOrder = () => order->Array.splice(~start=0, ~remove=Array.length(order) let simpleOverride = (base: r) => {...base, b: id(9)} -@val external hasOwn: ('a, string) => bool = "Object.hasOwn" -@val external getPrototypeOf: 'a => 'b = "Object.getPrototypeOf" -@val external objectPrototype: 'a = "Object.prototype" - -type proto<'a> = {name: string, @as("__proto__") proto?: 'a} - -let protoOverride = (base: proto<'a>, value: 'a) => { - ...base, - proto: try id(value) catch { - | _ => value - }, -} - -type mutableProto = {name: string, @as("__proto__") mutable mutableProto?: int} - -let protoAssignedAfter = (base: mutableProto) => { - let x = {...base, mutableProto: 5} - x.mutableProto = Some(5) - x -} - -let protoUsedAfter = (base: mutableProto) => { - let x = {...base, mutableProto: 5} - ignore(id(x.name)) - x -} - describe(__MODULE__, () => { test("an override that mutates the base", () => { eq(__LOC__, mutateInOverride({a: 1, b: 2}).a, 1) @@ -310,27 +283,6 @@ describe(__MODULE__, () => { } ) - test("an override of a field named __proto__ is an own property", () => { - let withInt = protoOverride({name: "int"}, 5) - ok(__LOC__, hasOwn(withInt, "__proto__")) - eq(__LOC__, withInt.proto, Some(5)) - let withObject = protoOverride({name: "object"}, {"z": 1}) - ok(__LOC__, hasOwn(withObject, "__proto__")) - eq(__LOC__, withObject.proto, Some({"z": 1})) - ok(__LOC__, getPrototypeOf(withObject) === objectPrototype) - let overridden = protoOverride({name: "own", proto: 1}, 2) - eq(__LOC__, overridden.proto, Some(2)) - }) - - test("a __proto__ field set on the copy is an own property", () => { - let assigned = protoAssignedAfter({name: "assigned"}) - ok(__LOC__, hasOwn(assigned, "__proto__")) - eq(__LOC__, assigned.mutableProto, Some(5)) - let used = protoUsedAfter({name: "used"}) - ok(__LOC__, hasOwn(used, "__proto__")) - eq(__LOC__, used.mutableProto, Some(5)) - }) - test("an exception's record", () => switch exceptionRecord(Extension({a: 1, b: 2})) { | Extension({a, b}) => eq(__LOC__, (a, b), (1, 5))