Repository navigation
Reject structurally incomplete type arguments in expressions - #65510
magic-akari wants to merge 4 commits into
Conversation
Propagate structural failures during speculative type parsing while preserving recoverable diagnostics on recognized types. This lets typed arrows and nested relational expressions fall back to expression parsing. Add TS/TSX compiler baselines and focused parser state rollback tests.
6956dcd to
5e17453
Compare
Give member and call suffixes a shared entry point so a failed type-argument interpretation is not retried by an initial member-expression pass. Share angle-closure facts across nested probes to avoid rescanning the same suffix. Remember structural failures in their parsing context so later attempts skip type and type-argument candidates already known to fail. Reuse diagnostic-free value-expression boundaries during disposable arrow and type-argument probes instead of repeatedly building subtrees that will be discarded. Rebuild opaque values before committing a successful outer probe, preserving binding recovery, lexical diagnostics, and comment directives. Restore queued JSDoc declarations and diagnostics when discarding a probe so rebuilding its AST does not insert them twice. Exclude values already carrying diagnostics or producing JSDoc side effects from boundary reuse, avoiding redundant arrow reconstruction during recovery.
5e17453 to
c88af14
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
Cross-cutting parser cache, rollback, and opaque-expression reconstruction changes affect speculation and AST-commit invariants, requiring maintainer review and runtime regression, fuzz, and performance validation unavailable in this review environment.
No open issues 🎉
What changed in this PR
This PR updates the native TypeScript parser to reject structurally incomplete type arguments and fall back to expression parsing, addressing #60874, #37760, #43720, and #61975 while preserving completed generics and recoverable diagnostics.
Changes:
- Separates recovering and speculative type parsing, propagating structural failures through nested types.
- Reduces repeated speculative work through shared suffix parsing, cached delimiter facts, and reusable expression boundaries.
- Adds regression tests and baselines for TS/TSX ambiguity, diagnostic recovery, syntax-tree reconstruction, and JSDoc preservation.
| File | Description |
|---|---|
tsc/testdata/tests/cases/compiler/typeArgumentSpeculationRelationalExpression.ts |
Tests relational fallback, completed generics, and expression reconstruction. |
tsc/testdata/tests/cases/compiler/typeArgumentSpeculationRelatedIssues.ts |
Adds reproductions for three linked parsing issues. |
tsc/testdata/tests/cases/compiler/typeArgumentSpeculationRecovery.ts |
Tests diagnostics and parsing after malformed candidates. |
tsc/testdata/tests/cases/compiler/arrowFunctionSpeculationJSDoc.ts |
Tests JSDoc declarations across nested arrow speculation. |
tsc/testdata/baselines/reference/compiler/typeArgumentSpeculationRelationalExpression.types |
Records inferred types for ambiguity cases. |
tsc/testdata/baselines/reference/compiler/typeArgumentSpeculationRelationalExpression.symbols |
Records symbol bindings for ambiguity cases. |
tsc/testdata/baselines/reference/compiler/typeArgumentSpeculationRelationalExpression.js |
Records emitted expressions and generic erasure. |
tsc/testdata/baselines/reference/compiler/typeArgumentSpeculationRelatedIssues.types |
Records types for linked-issue regressions. |
tsc/testdata/baselines/reference/compiler/typeArgumentSpeculationRelatedIssues.symbols |
Records bindings for linked-issue regressions. |
tsc/testdata/baselines/reference/compiler/typeArgumentSpeculationRelatedIssues.js |
Records emit for linked-issue regressions. |
tsc/testdata/baselines/reference/compiler/typeArgumentSpeculationRecovery.types |
Records types produced during recovery. |
tsc/testdata/baselines/reference/compiler/typeArgumentSpeculationRecovery.symbols |
Records bindings produced during recovery. |
tsc/testdata/baselines/reference/compiler/typeArgumentSpeculationRecovery.js |
Records emit after malformed candidates. |
tsc/testdata/baselines/reference/compiler/typeArgumentSpeculationRecovery.errors.txt |
Records expected recovery diagnostics. |
tsc/testdata/baselines/reference/compiler/arrowFunctionSpeculationJSDoc.types |
Records preserved JSDoc type information. |
tsc/testdata/baselines/reference/compiler/arrowFunctionSpeculationJSDoc.symbols |
Records bindings for the JSDoc regression. |
tsc/internal/scanner/scanner.go |
Adds position restoration that preserves collected directives. |
tsc/internal/parser/type_parser.go |
Introduces separate type-parsing policies and entry points. |
tsc/internal/parser/type_parser_test.go |
Tests rollback, lexical diagnostics, directives, and JSDoc state. |
tsc/internal/parser/type_argument_lookahead.go |
Shares delimiter facts to avoid repeated closure scans. |
tsc/internal/parser/speculative_expression.go |
Caches diagnostic-free expression boundaries for disposable probes. |
tsc/internal/parser/parser.go |
Integrates structural rejection, probe reconstruction, and shared suffix parsing. |
Review effort: Balanced
Ryan Cavanaugh (RyanCavanaugh)
left a comment
There was a problem hiding this comment.
I don't know if this is going to meet the bar for merging - while the linked bugs are a bit annoying, this is a huge change for the sake of a very limited set of scenarios. But at a minimum it'd be nice to have better comments / terminology, if it's just an agent doing the updates.
I agree that the implementation is more complex than I initially expected. The core change could be simpler, but TypeScript also needs to preserve its existing error recovery behavior and compatibility for invalid input. Accounting for those cases is what makes the implementation more involved. |
Replace vague terminology and repeated implementation details with comments about parsing failures, placeholder nodes, and rollback guarantees. Rename the expression-cache marker to usedExpressionCache and explain why cache hits require rebuilding a complete AST after successful speculation.
Type-argument speculation can recover incomplete type syntax and incorrectly accept a generic call where relational expressions were intended. For example:
The parenthesized comparison must remain a separate argument.
Propagate structural failures through speculative type productions so incomplete candidates fall back to expression parsing. Keep recoverable diagnostics on recognized types and retain the existing rules for accepting completed type arguments.
Nested fallback can amplify repeated scanning and parsing. Use a shared member/call suffix entry point to remove the duplicate initial member pass. Share delimiter facts and context-specific structural failures so retries skip repeated closure scans and candidates already known to fail. Disposable probes reuse diagnostic-free value-expression boundaries instead of repeatedly building temporary subtrees; successful outer probes rebuild opaque values before committing the AST.
Add TS/TSX compiler baselines for the affected expressions, valid generics, recoverable diagnostics, and AST reconstruction. Focused parser tests cover state rollback, lexical diagnostics, and comment directives. Validated with the full
tscGo test suite, custom lint, and formatting checks for changed Go files.Developed with AI assistance.