diff --git a/CHANGELOG.md b/CHANGELOG.md index bb6a20bbe2..d94c17de03 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. https://github.com/rescript-lang/rescript/pull/9416 #### :nail_care: Polish diff --git a/compiler/ml/translcore.ml b/compiler/ml/translcore.ml index 199c90ea71..d1926c4f77 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 570ca4607c..288192e52b 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 208, 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 214, 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 220, characters 7-14", order, [ + "base2", + "a2", + "b2" + ]); + Test_utils.eq("File \"record_spread_order_test.res\", line 221, 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 227, 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 233, characters 7-14", order, [ + "a", + "b" + ]); + Test_utils.eq("File \"record_spread_order_test.res\", line 234, 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 237, characters 7-14", order, [ + "a", + "b" + ]); + Test_utils.eq("File \"record_spread_order_test.res\", line 238, 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 243, characters 7-14", assignAfter({ + x: 0, + y: 0 + }), { + x: 2, + y: 1, + z: 1 + }); + 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 246, characters 7-14", assignAfterExpression({ + x: 0, + y: 0 + }), { + x: 2, + y: 1, + z: 1 + }); + 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 251, characters 7-14", readCopy({ + x: 0, + y: 0 + }), { + x: 1, + y: 2 + }); + Test_utils.eq("File \"record_spread_order_test.res\", line 252, 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 256, characters 7-14", assignTwice({ + x: 0, + y: 0 + }).x, 2); + Test_utils.eq("File \"record_spread_order_test.res\", line 257, 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 262, characters 7-14", pureStatements(base, { + a: 3, + b: 4 + }), { + a: 1, + b: 8 + }); + 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 264, 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 269, characters 7-14", discarded({ + a: 1, + b: 2 + }), 1); + 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 275, characters 11-18", () => { + throwing({ + a: 1, + b: 2 + }); + }); + Test_utils.eq("File \"record_spread_order_test.res\", line 276, 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 282, characters 18-25", false); + } + let a = match.a; + Test_utils.eq("File \"record_spread_order_test.res\", line 281, characters 27-34", [ + a, + match.b + ], [ + 1, + 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 289, characters 14-21", false); + } + let a = match.a; + Test_utils.eq("File \"record_spread_order_test.res\", line 288, 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, +} +/* 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 0000000000..d6b64595af --- /dev/null +++ b/tests/tests/src/record_spread_order_test.res @@ -0,0 +1,292 @@ +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)} + +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 exception's record", () => + switch exceptionRecord(Extension({a: 1, b: 2})) { + | Extension({a, b}) => eq(__LOC__, (a, b), (1, 5)) + | _ => ok(__LOC__, false) + } + ) +})