Skip to content

Skip the combined-constraint check for a dedicated variance relationship - #65548

Open
Alexis Delrieu (Amatewasu) wants to merge 4 commits into
microsoft:mainfrom
Amatewasu:fix-64423-variance-relation
Open

Alexis Delrieu (Amatewasu) wants to merge 4 commits into
microsoft:mainfrom
Amatewasu:fix-64423-variance-relation

Conversation

@Amatewasu

Copy link
Copy Markdown

Fixes #64423

Rework of #64528 applying the review feedback in #64528 (comment): the skip no longer depends on checker current-comparison state (r.c.varianceStack); variance measurement runs under a dedicated variance-assignability relationship, with hierarchical cache sharing against plain assignability as suggested there. The regression test and its baselines are carried over unchanged from #64528, and this PR is meant to replace it.

Problem

TypeScript 7 runs out of memory (unbounded growth, zero diagnostics) on programs TypeScript 6 checks fine. During variance measurement, the hoisted combined constraint of an intersection source can itself be an intersection with instantiable constituents (deferred conditional types produced by heavily overloaded generic builder types). A failed combined-constraint comparison then re-enters that check with a structurally new, ever-larger source that neither the relation cache nor the deeply-nested-type guards recognize as recursive; the chase grows without bound until the process dies.

Change

 getVariancesWorker (marker comparisons)
-  isTypeAssignableTo(typeWithSub, typeWithSuper)
+  isVarianceTypeAssignableTo(typeWithSub, typeWithSuper)   // c.varianceRelation
 ...
 structuredTypeRelatedTo (combined-constraint retry)
-  if result == False && len(r.c.varianceStack) == 0 && ...
+  if result == False && r.relation != r.c.varianceRelation && ...
  • New relationship c.varianceRelation (a *Relation cache like the existing five), with entry point isVarianceTypeAssignableTo. The three marker comparisons in getVariancesWorker (covariance, contravariance, independent-marker probe) run under it; the in/out annotation validation in checkTypeParameterDeferred keeps plain assignability.
  • The combined-constraint check is skipped exactly for this relationship: the skip is a property of the comparison, not of when it happens.
  • To keep behavior identical to assignability everywhere else, the three sites that special-case the assignable relation (isSimpleTypeRelatedTo, hasExcessProperties, the indexed-access base-constraint fallback) also accept the variance relation; an audit found no other assignable-specific branches.
  • Hierarchical cache sharing, in both provably safe directions:
    • variance lookups adopt plain-assignability failures (the combined-constraint check only converts failures into successes, so a failure holds everywhere);
    • variance successes are written through to the assignable cache (the check never runs under variance, so a variance success is a normal-path success). Assignable successes are not reused by variance (they may have been established by the retry); those pairs recompute once and cache in the variance map.
  • RelationCacheSizes gains a Variance field (additive) so the new cache is observable.

Scope vs. the varianceStack guard in #64528

The guard skipped the combined-constraint check for any comparison running while variance measurement was in progress, including fresh plain-assignability comparisons started inside that window. The relationship version skips it only for comparisons under the variance relationship. The chase lives in a single relater subtree (the retry re-enters the same relater), entered only from the marker comparisons, so the relationship cut covers every known trigger; no counter-case exists in the full corpus or the repros. If a case ever needs the wider cut, that is a one-line change.

Verification

Measured on the change atop #64528's head (Ubuntu container, 4 cores, Go 1.27.4), re-checked locally after the merge with current main:

Disclosure

This change, its verification, and this description were produced with LLM assistance, continuing the work in #64528 and the #64423 thread.

Checklist

The combined-constraint comparison in `structuredTypeRelatedTo` hoists the
constraints of the instantiable constituents of an intersection source into
a combined constraint and compares that constraint against the target. While
measuring variances, the hoisted constraint may itself be an intersection
with instantiable constituents (e.g. deferred conditional types, as produced
by heavily overloaded generic builder types), so a failed comparison re-enters
the same logic with a structurally new and ever larger source that neither the
relation cache nor the deeply-nested-type guards recognize as recursive, and
the chase grows without bound until the process runs out of memory.

Skip the check while the variance stack is non-empty; it never contributes to
the measured relation between the marker types.

Fixes microsoft#64423
…e measurement

Adds a minimized compiler fixture distilled from the three.js TSL node
types (microsoft#64423). Resolving `uv().div(vec2())` measures
the variances of generic extension interfaces whose hoisted constraints
form deferred conditional intersections; without the variance-stack guard
in `structuredTypeRelatedTo` the combined-constraint comparison re-enters
itself with structurally new, ever larger sources and the chase grows
without bound until the process exhausts memory. With the guard the file
checks to completion with no diagnostics, and the checked-in baselines pin
that result so the guard cannot be removed or weakened unnoticed.

Verified against the pre-fix revision that this fixture grows the relation
chase until the runtime runs out of memory, and that the fixed revision
terminates cleanly.
Instead of guarding the combined-constraint retry in structuredTypeRelatedTo
on the checker's current varianceStack state, introduce a dedicated
variance-assignability relationship (isVarianceTypeAssignableTo) that variance
measurement compares marker instantiations under. The combined-constraint
check is skipped exactly for that relationship, so the skip is a property of
the comparison, not of when it happens.

The new relationship behaves like plain assignability everywhere (the three
sites that special-case the assignable relation are covered) and shares caches
with it hierarchically: pairs that failed under plain assignability fail under
variance-assignability too (the retry can only turn failures into successes),
and successes established under variance-assignability are valid plain-
assignability successes (the retry never runs under variance).

Fixes microsoft#64423. Alternative implementation of microsoft#64528 implementing the review
feedback in microsoft#64528 (comment).
The regression test and baselines from microsoft#64528 are carried unchanged.
Copilot AI balanced review requested due to automatic review settings November 17, 2026 11:00
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 17, 2026
@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Nov 17, 2026

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.

🟡 Changes recommended

The new serialized variance field is missing from the generated client protocol and exact-object test expectations, leaving the typed API out of sync and causing test failures.

1 open issue
What changed in this PR

Addresses #64423’s unbounded type expansion during variance measurement in the native TypeScript checker. It replaces #64528’s state-based guard with a dedicated variance-assignability relationship.

Changes:

  • Skips combined-constraint retries specifically during variance comparisons while preserving other assignability behavior.
  • Adds directional cache sharing and exposes the variance cache size.
  • Adds a regression test with type and symbol baselines.
File Description
tsc/​testdata/​tests/​cases/​compiler/​effectiveConstraintChaseDuringVarianceMeasurement.ts Adds a regression case for unbounded variance expansion.
tsc/​testdata/​baselines/​reference/​compiler/​effectiveConstraintChaseDuringVarianceMeasurement.types Records expected inferred types.
tsc/​testdata/​baselines/​reference/​compiler/​effectiveConstraintChaseDuringVarianceMeasurement.symbols Records expected symbol resolution.
tsc/​internal/​checker/​types.go Adds the variance cache statistics field.
tsc/​internal/​checker/​relater.go Implements variance-specific comparisons and cache sharing.
tsc/​internal/​checker/​exports.go Includes variance cache size in reported statistics.
tsc/​internal/​checker/​checker.go Declares and initializes the variance relation cache.

Review effort: Balanced


type RelationCacheSizes struct {
Assignable int `json:"assignable"`
Variance int `json:"variance"`

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

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

[7.0 regression] Unbounded type relation expansion during variance computation causes OOM

3 participants