Fix #672: reject top-level / non-async for await...of in the type checker#713
Merged
Conversation
…hecker A `for await...of` drives the async-iterator protocol and is only valid in an async context, exactly like an `await` expression or `await using`. The type checker flagged those two (CheckAwait / CheckUsingDeclaration) but never checked the `await` modifier on `Stmt.ForOf`, so a top-level `for await` — or one inside a plain function — slipped through type checking and was then run as an ordinary synchronous `for...of`. Iterating an async generator that way fails at runtime in both modes with a misleading "not iterable" error. Reject it in VisitForOf with the same `'await' is only valid inside an async function.` diagnostic (TS1308) the await-expression and `await using` paths already use. SharpTS does not support top-level await, so this is consistent with the existing up-front rejection of top-level `await` expressions, instead of silently degrading to a sync loop. Adds both-mode regression tests for the top-level and non-async-function cases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A
for await...ofdrives the async-iterator protocol and is only valid in an async context — exactly like anawaitexpression orawait using. The type checker flagged those two (CheckAwait/CheckUsingDeclaration) but never checked theawaitmodifier onStmt.ForOf, so a top-levelfor await(or one inside a plain function) slipped through type checking and was then executed as an ordinary synchronousfor...of. Iterating an async generator that way failed at runtime in both modes with a misleading error:Runtime Error: for...of requires an iterable (array, Map, Set, or iterator).System.Exception: Runtime Error: Value is not iterable...at$Runtime.IterateToListFix
Reject it in
VisitForOfwith the same'await' is only valid inside an async function.diagnostic (TS1308) the await-expression andawait usingpaths already emit. SharpTS does not support top-level await, so this is consistent with the existing up-front rejection of top-levelawaitexpressions, rather than silently degrading to a sync loop:Now rejected up front in both modes (and also inside a non-async function, matching TS).
Tests
Adds both-mode regression tests in
AsyncGeneratorTests:ForAwaitOf_TopLevel_RejectedByTypeCheckerForAwaitOf_InNonAsyncFunction_RejectedByTypeCheckerFull suite green (12,838 passed). Conformance suites are unaffected by construction: Test262 runs with type-checking off for
.js(and no runtime path changed), and the TypeScriptConformance subset covers onlyassignmentCompatibility/conditional— nofor await.Note on #645
While picking up this branch I confirmed #645 (compiled
for awaitinside an async arrow throwingInvalidCastException) is already fixed onmain(commit 2b7fc82) and its repro now passes in both modes — closing that issue separately.Closes #672