archives

« Bugzilla Issues Index

#2987 — Destructuring Pattern as For-In Target: Early Error?


Currently, a destructuring pattern can appear to the left of "in" in a for-in statement head:

for ([a, b] in c);

However, since [[Enumerate]] will always return an iterator over strings, such a binding will always fail. (The destructuring source will always be a string.)

Should a destructuring pattern to the left of "in" result in an early error instead?


Proxies are not restricted to this limitation.

for ([a, b] in new Proxy({}, {*enumerate(){ yield [1,2]; }})) {
print(`${a}:${b}`); // prints "1:2"
}


It used to make sense to allow this since destructuring used to work on non objects.

I agree that this should be a syntax error.

André: We should not optimize for Proxies. In this case it would even be a non behaving Proxy.


I agree with Kevin and Erik. This probably should be a syntax error, now that string values can not be destructured.

Note that the description of [[Enumerate]] in 6.1.7.2 says it produces an "iterator object over the string values..." so your proxy does not provide a conforming definition for it.


However, now that I looked at it, it's going to considerably complicate the BNF and/or early error rules to make this be a static error.

I'm not sure it is really worthy the added spec. complexity.


We've reverted to allowing destructuring of string values. That means somebody might plausibly code:

for ([firstChar, ...rest] in c) ...

etc.

Given this (admittedly minor) utility and the complexity of disallowing it, I'm going to close this as WONTFIX.