Semantic Diff

#9429 Copy dict and array spreads before later elements that can change them

1 commit

aacaf7206841611e209512ff949c9b6cf6596228..pr-9429

bc27b18dd · illusionalsagacity · Nov 25, 2026, 19:42 UTC

Copy dict and array spreads before later elements that can change them

Why

dict{...base, "b": v} and [...base, v] copy base where the spread stands, before v is evaluated. sequence_arguments got the bare spread as an argument. A variable is safe to delay, so it stayed in the literal, after the statements of any later element that needs them (a try, or a switch that is not one expression), and a mutation of the base in those statements showed up in the result. An array spread is copied by Primitive_array.spread, which runs after all elements are evaluated, so there a plain call did the same. While the parts of a literal are sequenced, a spread is now the copy {...base} or [...base] when a later part has such statements or, in an array, a value that can run code (a call, an await, a read of a JS property). Otherwise the spread stays bare as before.

modifiedJS dict spread with an element that needs statements

@inline(never) let id = x => xlet f = (base: dict<int>) =>  dict{...base, "b": try { base->Dict.set("a", 100); id(5) } catch { | _ => 0 }}
js
Before
After
function f(base) {function f(base) { let tmp; let tmp = {...base}; let tmp$1; try { try { base["a"] = 100; base["a"] = 100; tmp = id(5); tmp$1 = id(5); } catch (exn) { } catch (exn) { tmp = 0; tmp$1 = 0; } } return { return { ...base, b: tmp ...tmp, b: tmp$1 }; };}}

modifiedJS array spread with an element that needs statements

@inline(never) let id = x => xlet f = (base: array<int>) =>  [...base, try { base->Array.push(100); id(5) } catch { | _ => 0 }]
js
Before
After
function f(base) {function f(base) { let tmp; let tmp = [...base]; let tmp$1; try { try { base.push(100); base.push(100); tmp = id(5); tmp$1 = id(5); } catch (exn) { } catch (exn) { tmp = 0; tmp$1 = 0; } } return Primitive_array.spread([ return Primitive_array.spread([ base, [tmp] tmp, [tmp$1] ]); ]);}}

modifiedJS array spread with a call after it

let call = (base: array<int>, f) => [...base, f()]let pure = (base: array<int>, x) => [...base, x + 1]
js
Before
After
function call(base, f) {function call(base, f) { return Primitive_array.spread([ return Primitive_array.spread([ base, [...base], [f()] [f()] ]); ]);}} function pure(base, x) {function pure(base, x) { return Primitive_array.spread([ return Primitive_array.spread([ base, base, [x + 1 | 0] [x + 1 | 0] ]); ]);}}

Review

  • Are list spreads affected? No, lists are immutable, so when they are copied doesn't show.
  • Why not emit [...base, f()]? An array literal with a spread in it is not safe for the code that looks into array literals (indexing, @variadic arguments), so the copy only ever stands inside the runtime call or a temporary.
  • Which checked-in outputs change? spread_literal_test.mjs, which has a call after a spread.