Repository navigation
Fix parsing of call with type arguments after left shift (<<) - #64854
Open
Lanz Skyler B. Busa (ArchiTecCTT) wants to merge 1 commit into
Open
Lanz Skyler B. Busa (ArchiTecCTT) wants to merge 1 commit into
Lanz Skyler B. Busa (ArchiTecCTT) wants to merge 1 commit into
Conversation
When an expression is followed by `<<`, the parser splits it into two `<` tokens and speculatively parses a type argument list, so that a generic function type can be the first type argument, as in `f<<T>(x: T) => T>(g)`. For `1 << alignof<T[]>()` that speculative parse reports errors but still ends at `>` followed by `(`, so it was accepted and the expression was misparsed as the call `1<...>()`. Reject the type argument interpretation when it started from a split `<<` and parsing the list reported errors, so `<<` is parsed as a left shift. Fixes microsoft#37760
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The narrow rollback and regression baselines support the intended fix with no unresolved findings; tests were not run during this review.
No open issues 🎉
What changed in this PR
Fixes #37760 by rejecting erroneous speculative type arguments after <<, allowing the native compiler to parse the intended left shift.
Changes:
- Rewinds failed
<<speculation while preserving valid generic calls. - Adds regression coverage and JavaScript, type, and symbol baselines.
| File | Description |
|---|---|
tsc/internal/parser/parser.go |
Adds diagnostic-based rollback for << speculation. |
tsc/testdata/tests/cases/compiler/leftShiftBeforeTypeArgumentCall.ts |
Covers shifts, comparisons, and valid generic calls. |
tsc/testdata/baselines/reference/compiler/leftShiftBeforeTypeArgumentCall.js |
Records correct emitted operators and calls. |
tsc/testdata/baselines/reference/compiler/leftShiftBeforeTypeArgumentCall.types |
Records expected inferred types. |
tsc/testdata/baselines/reference/compiler/leftShiftBeforeTypeArgumentCall.symbols |
Records expected symbol bindings. |
Review effort: Balanced
Author
|
@microsoft-github-policy-service agree |
This branch has not been deployed
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.
Fixes #37760
Problem
1 << alignof<T[]>()was misparsed, while the same code with>>parsed correctly:When an expression is followed by
<<,tryParseTypeArgumentsInExpressionrescans it as<so that a generic function type can be the first type argument (f<<T>(x: T) => T>(g)). Here the speculative parse of<alignof<T[]>as a type argument list reports errors, but it still ends at>followed by(. So it was accepted, and the expression became the call1<...>().Implementation
In
tryParseTypeArgumentsInExpression, if the speculative type argument list was started from a rescanned<<and parsing it produced any diagnostics, rewind and returnnil, so<<is parsed as a left shift operator. A plain<keeps its existing behavior, and a validf<<T>(x: T) => T>(...)still parses as type arguments.Tests
Added
tests/cases/compiler/leftShiftBeforeTypeArgumentCall.ts, covering<<,>>,>>>,<<=,>>=,>>>=,<, a parenthesized right operand, and ag<<U>(x: U) => U>(...)call that must still parse as type arguments. Before this change it produced 12 errors and emitted1(); now it has no errors and emits1 << alignof(). No other baselines changed.hereby test,test:all,lint,check:format, andgenerate(with a cleangit diff) pass locally.This change was written with the help of Claude Code; I have reviewed it and will handle review feedback myself.
🤖 Generated with Claude Code