Repository navigation
Skip the combined-constraint check for a dedicated variance relationship - #65548
Open
Alexis Delrieu (Amatewasu) wants to merge 4 commits into
Open
Alexis Delrieu (Amatewasu) wants to merge 4 commits into
Alexis Delrieu (Amatewasu) wants to merge 4 commits into
Conversation
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.
Contributor
There was a problem hiding this comment.
🟡 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
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 #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
c.varianceRelation(a*Relationcache like the existing five), with entry pointisVarianceTypeAssignableTo. The three marker comparisons ingetVariancesWorker(covariance, contravariance, independent-marker probe) run under it; the in/out annotation validation incheckTypeParameterDeferredkeeps plain assignability.isSimpleTypeRelatedTo,hasExcessProperties, the indexed-access base-constraint fallback) also accept the variance relation; an audit found no other assignable-specific branches.RelationCacheSizesgains aVariancefield (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:
effectiveConstraintChaseDuringVarianceMeasurement(Prevent unbounded combined-constraint recursion during variance measurement #64528's test): passes, all 9 configurations, baselines identical.go test ./internal/testrunner -run TestLocal): passes with zero baseline differences.go vet ./tsc/internal/checker/clean,go build ./cmd/tscOK. GitHub CI re-runs the suite on the merged head.Disclosure
This change, its verification, and this description were produced with LLM assistance, continuing the work in #64528 and the #64423 thread.
Checklist
main(merged 6867202 at branch creation)npx hereby test- full corpus run via the Go testrunner on the change commit; CI re-runs hereby on the headnpx hereby lint- not run locally (nonode_modulesin this checkout), same as Prevent unbounded combined-constraint recursion during variance measurement #64528npx hereby check:format- not run locally;gofmtclean on all touched files