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
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
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
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,@variadicarguments), 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.