Repository navigation
Prevent unbounded combined-constraint recursion during variance measurement #64528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3261,7 +3261,15 @@ func (r *Relater) structuredTypeRelatedTo(source *Type, target *Type, reportErro | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // needs to have its constraint hoisted into an intersection with said type parameter, this way | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // the type param can be compared with itself in the target (with the influence of its constraint to match other parts) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // For example, if `T extends 1 | 2` and `U extends 2 | 3` and we compare `T & U` to `T & U & (1 | 2 | 3)` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if result == TernaryFalse && (source.flags&TypeFlagsIntersection != 0 || source.flags&TypeFlagsTypeParameter != 0 && target.flags&TypeFlagsUnion != 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // We skip the combined-constraint check while measuring variances. Variance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // measurement compares marker instantiations, and there 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 this logic with a structurally new and ever larger source | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // that the relation cache and the deeply-nested-type guards never recognize as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // recursive, and the chase grows without bound (see microsoft/TypeScript#64423). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // The check never contributes to the measured relation between the marker types. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if result == TernaryFalse && len(r.c.varianceStack) == 0 && (source.flags&TypeFlagsIntersection != 0 || source.flags&TypeFlagsTypeParameter != 0 && target.flags&TypeFlagsUnion != 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you wanted this to not be dependent on checker current-comparison state (which is unstable, and why this is maybe not the best solution), you'd wanna do this (skipping this check) for a new relationship used by variance comparisons instead of inspecting
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the reply! I don't have enough experience with compiler internals to fully understand the implications or assess which approach would be best. My main motivation is simply to get our project compiling with TS7. For reference, I benchmarked the current PR against
No statistically significant differences were detected, although these results are preliminary given the different base revisions. Benchmark methodology and detailed resultsSeven alternating runs per binary. Mean wall-clock time and peak RSS measured using
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var sourceTypes []*Type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if source.flags&TypeFlagsIntersection != 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sourceTypes = source.Types() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LLM-generated test added
Regression test checked in as
bd456af6e3(tests/cases/compiler/effectiveConstraintChaseDuringVarianceMeasurement.tsplus.types/.symbolsbaselines), in response to the review comment asking for a checked-in test for the variance-stack guard.Minimized from the real repro. It is hand-distilled from the three.js TSL node types that triggered [7.0 regression] Unbounded type relation expansion during variance computation causes OOM #64423. Only the constructs needed to drive the combined-constraint comparison in
structuredTypeRelatedToduring variance measurement were kept — resolvinguv().div(vec2())measures the variances of the generic extension interfaces, whose hoisted constraints form deferred conditional intersections.It fails without the fix. On the pre-fix revision (
673a5f17d7) this file enters the unbounded relation chase and the process dies withruntime: out of memory. Removing or weakening the variance-stack guard reintroduces this immediately, so CI catches it instead of the regression going unnoticed.It passes with the fix and pins the exact output. With the guard, the file checks to completion in under a second with no diagnostics, and the baselines record the precise inferred types (e.g.
x : Node<"vec2">). This also guards the opposite failure mode: a future change that "fixes" a hang by making the checker give up and infer different types would fail the baseline comparison.Reproducing the pre-fix failure locally:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think Jake Bailey (@jakebailey)? :)