Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normative: forbid for-of loops with variable named async #2256

Merged
merged 1 commit into from
Feb 24, 2021
Merged

Conversation

bakkot
Copy link
Contributor

@bakkot bakkot commented Dec 26, 2020

Fixes #2034, raised by @waldemarhorwat a year ago (sorry for the delay). See the linked slides for context. This makes for (async of [1,2,3]); a syntax error, as it is in all major engines except V8.

We said at the time that we could resolve this with a single token of lookahead, presumably by just preventing for-of loops where the first token is async. Unfortunately, let async = {x: 0}; for (async.x of [1,2,3]); is a legal program with no ambiguities and we probably shouldn't forbid it. So I've used a two token lookahead here. We use two tokens of lookahead in some other contexts; I don't think there's any issue with extending that to this case, especially now that #2254 has fixed up the definition.

We should add a few tests asserting that let async = {x: 0}; for (async.x of [1,2,3]); and for (async of => 0; false;); are legal and for (async of [1,2,3]); is a syntax error. Edit: done.

@bakkot bakkot added has consensus This has committee consensus. needs test262 tests The proposal should specify how to test an implementation. Ideally via github.com/tc39/test262 normative change Affects behavior required to correctly evaluate some ECMAScript source text labels Dec 26, 2020
spec.html Outdated Show resolved Hide resolved
@ljharb ljharb requested review from syg and a team January 6, 2021 23:46
@bakkot bakkot added ready to merge Editors believe this PR needs no further reviews, and is ready to land. and removed ready to merge Editors believe this PR needs no further reviews, and is ready to land. labels Jan 7, 2021
@bakkot
Copy link
Contributor Author

bakkot commented Jan 7, 2021

(Actually I guess we should hold off on this until there are tests.)

@bakkot bakkot added has test262 tests and removed needs test262 tests The proposal should specify how to test an implementation. Ideally via github.com/tc39/test262 labels Jan 18, 2021
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull request Jan 19, 2021
…ops. r=yulia

`for-of` loops mustn't start with the token sequence `async of`, because that
leads to a shift-reduce conflict when parsing `for (async of => {};;)` or
`for (async of [])`. This restriction doesn't apply to `for-await-of` loops,
because `async` in `for await (async of ...)` is always parsed as an identifier.

Parsing `for (async of ...)` already results in a SyntaxError, but that happens
because `assignExpr()` always tries to parse the sequence `async [no LineTerminator] of`
as the start of an async arrow function. That means `forHeadStart()` still needs
to handle the case when `async` and `of` are separated by a line terminator.

Part 3 will update the parser to allow `for await (async of ...)`.

Spec change: tc39/ecma262#2256

Depends on D100994

Differential Revision: https://phabricator.services.mozilla.com/D100995
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified that referenced this pull request Jan 25, 2021
…ops. r=yulia

`for-of` loops mustn't start with the token sequence `async of`, because that
leads to a shift-reduce conflict when parsing `for (async of => {};;)` or
`for (async of [])`. This restriction doesn't apply to `for-await-of` loops,
because `async` in `for await (async of ...)` is always parsed as an identifier.

Parsing `for (async of ...)` already results in a SyntaxError, but that happens
because `assignExpr()` always tries to parse the sequence `async [no LineTerminator] of`
as the start of an async arrow function. That means `forHeadStart()` still needs
to handle the case when `async` and `of` are separated by a line terminator.

Part 3 will update the parser to allow `for await (async of ...)`.

Spec change: tc39/ecma262#2256

Depends on D100994

Differential Revision: https://phabricator.services.mozilla.com/D100995

UltraBlame original commit: c260009506931dfe9e8c75906cd200e3ff687b79
jamienicol pushed a commit to jamienicol/gecko that referenced this pull request Jan 29, 2021
…ops. r=yulia

`for-of` loops mustn't start with the token sequence `async of`, because that
leads to a shift-reduce conflict when parsing `for (async of => {};;)` or
`for (async of [])`. This restriction doesn't apply to `for-await-of` loops,
because `async` in `for await (async of ...)` is always parsed as an identifier.

Parsing `for (async of ...)` already results in a SyntaxError, but that happens
because `assignExpr()` always tries to parse the sequence `async [no LineTerminator] of`
as the start of an async arrow function. That means `forHeadStart()` still needs
to handle the case when `async` and `of` are separated by a line terminator.

Part 3 will update the parser to allow `for await (async of ...)`.

Spec change: tc39/ecma262#2256

Depends on D100994

Differential Revision: https://phabricator.services.mozilla.com/D100995
@bakkot bakkot added es2021 ready to merge Editors believe this PR needs no further reviews, and is ready to land. labels Feb 12, 2021
@ljharb ljharb removed the ready to merge Editors believe this PR needs no further reviews, and is ready to land. label Feb 19, 2021
@bakkot bakkot added the ready to merge Editors believe this PR needs no further reviews, and is ready to land. label Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has consensus This has committee consensus. has test262 tests normative change Affects behavior required to correctly evaluate some ECMAScript source text ready to merge Editors believe this PR needs no further reviews, and is ready to land.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Grammar ambiguity in for ( async of
5 participants