Skip to content

Experiment: Dependent contextual inference - #64092

Open
Devansh Jethmalani (devanshj) wants to merge 7 commits into
microsoft:mainfrom
devanshj:dependent-contextual-inference-2
Open

Experiment: Dependent contextual inference#64092
Devansh Jethmalani (devanshj) wants to merge 7 commits into
microsoft:mainfrom
devanshj:dependent-contextual-inference-2

Conversation

@devanshj

@devanshj Devansh Jethmalani (devanshj) commented Aug 29, 2026

Copy link
Copy Markdown

Fixes #64091

Please see the linked issue for description and discussion, this is just a shabby implementation to play with the idea, the issue is more important than the pull request!

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.

Pull request overview

Adds iterative dependent contextual inference to the compiler for self-referential generic constraints.

Changes:

  • Rechecks context-sensitive expressions until inference stabilizes.
  • Adds diagnostic staging and cache-reset infrastructure.
  • Adds compiler tests and baselines for several library patterns and edge cases.

Reviewed changes

Copilot reviewed 38 out of 52 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tsc/internal/checker/checker.go Implements iterative contextual inference.
tsc/internal/ast/diagnostic.go Adds staged diagnostic handling.
tsc/internal/ast/utilities.go Adds recursive AST traversal.
tsc/internal/diagnostics/diagnosticMessages.json Defines the pass-limit diagnostic.
tsc/internal/diagnostics/diagnostics_generated.go Generates the new diagnostic binding.
tsc/testdata/tests/cases/compiler/dependentContextualInferenceBasic.ts Tests basic dependent inference.
tsc/testdata/tests/cases/compiler/dependentContextualInferenceMultiplePasses.ts Tests multi-pass inference.
tsc/testdata/tests/cases/compiler/dependentContextualInferenceTooManyPasses.ts Tests the pass limit.
tsc/testdata/tests/cases/compiler/dependentContextualInferenceCircular.ts Tests circular inference.
tsc/testdata/tests/cases/compiler/dependentContextualInferenceZustand.ts Tests a Zustand-style API.
tsc/testdata/tests/cases/compiler/dependentContextualInferenceXState.ts Tests an XState-style API.
tsc/testdata/tests/cases/compiler/dependentContextualInferenceTanstackQuery.ts Tests a TanStack Query-style API.
tsc/testdata/tests/cases/compiler/dependentContextualInferenceAiSdk.ts Tests an AI SDK-style API.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceBasic.js Records basic JavaScript emit.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceBasic.symbols Records basic symbols.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceBasic.types Records basic inferred types.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceMultiplePasses.js Records multi-pass emit.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceMultiplePasses.symbols Records multi-pass symbols.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceMultiplePasses.types Records multi-pass types.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceTooManyPasses.js Records pass-limit emit.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceTooManyPasses.symbols Records pass-limit symbols.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceTooManyPasses.types Records pass-limit types.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceTooManyPasses.errors.txt Records the pass-limit error.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceCircular.js Records circular-case emit.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceCircular.symbols Records circular-case symbols.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceCircular.types Records circular-case types.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceZustand.js Records Zustand-style emit.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceZustand.symbols Records Zustand-style symbols.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceZustand.types Records Zustand-style types.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceXState.js Records XState-style emit.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceXState.symbols Records XState-style symbols.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceXState.types Records XState-style types.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceTanstackQuery.js Records TanStack-style emit.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceTanstackQuery.symbols Records TanStack-style symbols.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceTanstackQuery.types Records TanStack-style types.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceAiSdk.js Records AI SDK-style emit.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceAiSdk.symbols Records AI SDK-style symbols.
tsc/testdata/baselines/reference/compiler/dependentContextualInferenceAiSdk.types Records AI SDK-style types.
tsc/testdata/baselines/reference/compiler/circularMappedTypeConstraint.types Updates existing mapped-type inference output.
Files not reviewed (1)
  • tsc/internal/diagnostics/diagnostics_generated.go: Generated file

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread tsc/internal/checker/checker.go Outdated
c.resetNodeCheck(node)
c.revertStagedDiagnostics()
previousValueType = valueType
valueType = c.checkExpressionWithContextualTypeWorker(node, contextualType, inferenceContext, CheckModeNormal)
Comment on lines +7575 to +7577
if contextualType.flags&TypeFlagsTypeParameter == 0 || !c.isTypeParameterDependent(contextualType.AsTypeParameter()) || checkMode&CheckModeContextual != 0 {
return c.checkExpressionWithContextualTypeWorker(node, contextualType, inferenceContext, checkMode)
}
Comment on lines +243 to +244
isStaging bool
stagingDiagnostics []*Diagnostic
Comment on lines +7590 to +7593
if passes >= 5 {
c.commitStagedDiagnostics()
c.error(node, diagnostics.Dependent_contextual_inference_requires_too_many_passes_and_possibly_infinite)
return valueType
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Feature request: Dependent contextual inference

2 participants