archives

« Bugzilla Issues Index

#2525 — Grammar ambiguity with let, for-of, and array/object literals


for-of loops introduce an ambiguity in the grammar when using let and an array or object literal.

Consider these two examples:

// Ex. 1 let declared variable 'of' iterating an array, or global variable
// 'let' iterating the first element of global variable 'of'?
var of = [ [ 5 ] ];

for (let of of [0]) { /* of == 0 or let == 5? */ }

// Ex. 2 let declared variable 'of' iterating a parenthesized array literal, or
// global variable 'let' iterating the result of calling function 'of'?
var of = function () { return [ 5 ]; }

for (let of of ([0])) { /* of == 0 or let == 5? */ }


Since this is code that can't exist in ES5 it seems the issue here is to simply choose one option over the other in the ambiguities. Luke Hoban suggested that morally the language wants to treat this as though 'let' and 'of' are maximally interpreted as keywords in this context since there is no back compat requirement. I.e. both of the both should be let declarations of a variable 'of' and the collection should be the array literal [0] in each.

Seems this will require a small spec change along the lines of:

for ([lookahead not in {let}] LeftHandSideExpression of AssignmentExpression ) Statement

However this will now disallow 'let' as a variable name in the LHSExpression, so further clarification would be required to allow that.


fixed in rev23 editor's draft.

Added [lookahead not in { let }] prefix for for-of
but a [lookahead not in { let [ }] prefix for for-in, just like ExpressionStatement.

Hence:

for (let of of [0])... //ForDeclaration of AssignmentExpression
for (let in [0])... //LeftHandSideExpression in Expression
for (let of in [0])... //ForDeclaration in Expression
for (let [of] in [0])...//LeftHandSideExpression in Expression


*** Bug 2522 has been marked as a duplicate of this bug. ***


fixed in rev23 draft