Keep the fields bound next to a record rest apart from the source's fields
Why
After let {x, ...rest} = r, the pattern's x is the value at the destructuring and r.x is the current one. The match compiler bound x as an alias, so Lambda inlined it as a read of r.x, and the record rest pass then replaced every r.x after the rest, also those in the program, with a local bound by the destructuring. An assignment to r.x was lost, a read of r.x was stale after a write by an assignment, a method, a callee, an alias, an await or a closure, and x next to an unused rest was read after such a write.
The match compiler now binds the fields a pattern takes variables from next to a rest with StrictOpt, as it does mutable fields, so that only the program's own reads are left as reads of the source. The record rest pass replaces no reads. It moves the field reads in the declarations right before the rest into the destructuring, so that a field is not read twice; the other excluded fields get a placeholder name. These bindings keep the names of the pattern and, like other declarations, now count as bindings that shadow a JS global.
modifiedMatching fields bound next to a rest
type xy = {x: int, y: int}type onlyY = {y: int}let f = ({x, ...onlyY as rest}: xy) => (x, rest)
text
modifiedJS reads and writes of a record rest source
type mutableXy = {mutable x: int, y: int}type xy = {x: int, y: int}type onlyY = {y: int}let f = (a: mutableXy) => { let b = (a :> xy) let {x, ...onlyY as rest} = b a.x = 42 (rest, x, b.x)}let g = (a: xy, k) => { let {x, ...onlyY as _rest} = a k() x}
js
modifiedJS names of bound fields
type renamed = {@as("user-name") name: string, other: int}type onlyOther = {other: int}let h = ({name, ...onlyOther as rest}: renamed) => (name, rest)
js
Review
- Is a bound field always read only once? Not when its value is read before a test, as for
{name: "a", ...rest}followed by a case that bindsname: the destructuring, which comes after the test, namesnameagain to leave it out of the rest. The value bound is the one tested. - Are fields bound by a pattern without a rest read at the match? Not always: Lambda still inlines those of immutable fields, which this commit does not change.