Skip to content

Reject structurally incomplete type arguments in expressions - #65510

Open
magic-akari wants to merge 4 commits into
microsoft:mainfrom
magic-akari:fix/type-argument-speculation
Open

magic-akari wants to merge 4 commits into
microsoft:mainfrom
magic-akari:fix/type-argument-speculation

Conversation

@magic-akari

@magic-akari magic-akari commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Type-argument speculation can recover incomplete type syntax and incorrectly accept a generic call where relational expressions were intended. For example:

console.log(a < b, (c > (d & e)));

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 tsc Go test suite, custom lint, and formatting checks for changed Go files.

Developed with AI assistance.

@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Nov 15, 2026
@typescript-automation typescript-automation Bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Nov 15, 2026
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.
@magic-akari
magic-akari force-pushed the fix/type-argument-speculation branch from 6956dcd to 5e17453 Compare November 15, 2026 16:42
@magic-akari magic-akari changed the title Reject incomplete type arguments during expression speculation Reject structurally incomplete type arguments in expressions Nov 15, 2026
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.
@magic-akari
magic-akari force-pushed the fix/type-argument-speculation branch from 5e17453 to c88af14 Compare November 15, 2026 18:51
@magic-akari
magic-akari marked this pull request as ready for review November 16, 2026 00:02
Copilot AI balanced review requested due to automatic review settings November 16, 2026 00:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tsc/internal/parser/parser.go Resolved
Comment thread tsc/internal/parser/parser.go Resolved
Comment thread tsc/internal/parser/parser.go Resolved
@magic-akari

Copy link
Copy Markdown
Contributor Author

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.
@jakebailey Jake Bailey (jakebailey) added the Domain: Performance Performance related label Nov 18, 2026
@github-project-automation github-project-automation Bot moved this to Waiting on author in PR Backlog Nov 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Domain: Performance Performance related For Backlog Bug PRs that fix a backlog bug For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Status: Waiting on author

5 participants