Skip to content

Commit a970563

Browse files
committed
Intern deferred conditional types by their content
A conditional type instantiation is also reached through distributive and tail-recursive instantiation, so the same deferred conditional type, with the same root, mappings, and alias, could be created more than once. Such copies can be told apart only by type id, which follows the order in which code was checked. Deferred conditional types are now interned by their root, the content of their mapper, and their alias. Those made for a constraint stay apart from the others, as the instantiation caches of a conditional type already keep them: evaluating a conditional type for a constraint can include its true branch where evaluating it otherwise would not. Sharing the deferred types made for one with the other reused the comparisons reached from them, and checking calls with SetOptional of generic keys then exceeded the instantiation depth limit (seen in type-fest). Instantiations of InferData and UnpackKeyValue, whose recursion is bounded, are no longer reported as infinitely circular when inferred through a closure.
1 parent d638605 commit a970563

10 files changed

Lines changed: 328 additions & 91 deletions

File tree

‎packages/typescript/enums/objectFlags.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ export declare enum ObjectFlags {
4949
IsNeverIntersectionComputed = 1 << 25,
5050
IsNeverIntersection = 1 << 26,
5151
IsConstrainedTypeVariable = 1 << 27,
52+
ConditionalForConstraint = 1 << 30,
5253
}

‎packages/typescript/enums/objectFlags.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ export var ObjectFlags;
4949
ObjectFlags[ObjectFlags["IsNeverIntersectionComputed"] = 33554432] = "IsNeverIntersectionComputed";
5050
ObjectFlags[ObjectFlags["IsNeverIntersection"] = 67108864] = "IsNeverIntersection";
5151
ObjectFlags[ObjectFlags["IsConstrainedTypeVariable"] = 134217728] = "IsConstrainedTypeVariable";
52+
ObjectFlags[ObjectFlags["ConditionalForConstraint"] = 1073741824] = "ConditionalForConstraint";
5253
})(ObjectFlags || (ObjectFlags = {}));

‎tsc/internal/api/enum_values_generated.go‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tsc/internal/checker/checker.go‎

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ type Checker struct {
678678
objectLiteralTypes map[CacheHashKey]*Type
679679
spreadTypes map[CacheHashKey]*Type
680680
objectContentTypes map[CacheHashKey]*Type
681+
deferredConditionalTypes map[deferredConditionalTypeKey]*Type
681682
spreadSymbols map[spreadSymbolKey]*ast.Symbol
682683
symbolsWithType map[symbolWithTypeKey]*ast.Symbol
683684
paddedProperties map[symbolWithTypeKey]*ast.Symbol
@@ -1025,6 +1026,7 @@ func NewChecker(program Program, tracer *Tracer) (*Checker, *sync.Mutex) {
10251026
c.objectLiteralTypes = make(map[CacheHashKey]*Type)
10261027
c.spreadTypes = make(map[CacheHashKey]*Type)
10271028
c.objectContentTypes = make(map[CacheHashKey]*Type)
1029+
c.deferredConditionalTypes = make(map[deferredConditionalTypeKey]*Type)
10281030
c.spreadSymbols = make(map[spreadSymbolKey]*ast.Symbol)
10291031
c.symbolsWithType = make(map[symbolWithTypeKey]*ast.Symbol)
10301032
c.paddedProperties = make(map[symbolWithTypeKey]*ast.Symbol)
@@ -18083,6 +18085,34 @@ func (b *keyBuilder) writeTypes(types []*Type) {
1808318085
}
1808418086
}
1808518087

18088+
// writeMapperContent writes the mappings of a mapper as compareTypeMappers
18089+
// compares them, and reports false for mappers it cannot compare by content.
18090+
func (b *keyBuilder) writeMapperContent(m *TypeMapper) bool {
18091+
if m == nil {
18092+
b.writeByte(0)
18093+
return true
18094+
}
18095+
switch m.Kind() {
18096+
case TypeMapperKindSimple:
18097+
m := castTypeMapper[SimpleTypeMapper](m, TypeMapperKindSimple)
18098+
b.writeByte(1)
18099+
b.writeType(m.source)
18100+
b.writeType(m.target)
18101+
return true
18102+
case TypeMapperKindArray:
18103+
m := castTypeMapper[ArrayTypeMapper](m, TypeMapperKindArray)
18104+
b.writeByte(2)
18105+
b.writeTypes(m.sources)
18106+
b.writeTypes(m.targets)
18107+
return true
18108+
case TypeMapperKindMerged:
18109+
m := castTypeMapper[MergedTypeMapper](m, TypeMapperKindMerged)
18110+
b.writeByte(3)
18111+
return b.writeMapperContent(m.m1) && b.writeMapperContent(m.m2)
18112+
}
18113+
return false
18114+
}
18115+
1808618116
func (b *keyBuilder) writeAlias(alias *TypeAlias) {
1808718117
if alias != nil {
1808818118
b.writeByte(1)
@@ -25600,12 +25630,10 @@ func (c *Checker) getConditionalType(root *ConditionalRoot, mapper *TypeMapper,
2560025630
}
2560125631
}
2560225632
// Return a deferred type for a check that is neither definitely true nor definitely false
25603-
result = c.newConditionalType(root, mapper, combinedMapper)
25604-
if alias != nil {
25605-
result.alias = alias
25606-
} else {
25607-
result.alias = c.instantiateTypeAlias(root.alias, mapper)
25633+
if alias == nil {
25634+
alias = c.instantiateTypeAlias(root.alias, mapper)
2560825635
}
25636+
result = c.getDeferredConditionalType(root, mapper, combinedMapper, alias, forConstraint)
2560925637
break
2561025638
}
2561125639
if extraTypes != nil {
@@ -25614,6 +25642,46 @@ func (c *Checker) getConditionalType(root *ConditionalRoot, mapper *TypeMapper,
2561425642
return result
2561525643
}
2561625644

25645+
type deferredConditionalTypeKey struct {
25646+
root *ConditionalRoot
25647+
key CacheHashKey
25648+
}
25649+
25650+
// getDeferredConditionalType interns deferred conditional types by their root,
25651+
// the content of their mapper, and their alias, which determine everything
25652+
// about them, so distributive and tail-recursive instantiations reach the same
25653+
// deferred types. Those made for a constraint are kept apart from the others,
25654+
// as getConditionalTypeInstantiation keeps them: evaluating a conditional type
25655+
// for a constraint can include its true branch where evaluating it otherwise
25656+
// would not, and the types and comparisons reached from a deferred type made
25657+
// for one are not reused for the other.
25658+
func (c *Checker) getDeferredConditionalType(root *ConditionalRoot, mapper *TypeMapper, combinedMapper *TypeMapper, alias *TypeAlias, forConstraint bool) *Type {
25659+
var b keyBuilder
25660+
if !b.writeMapperContent(mapper) {
25661+
result := c.newConditionalType(root, mapper, combinedMapper)
25662+
result.alias = alias
25663+
if forConstraint {
25664+
result.objectFlags |= ObjectFlagsConditionalForConstraint
25665+
}
25666+
return result
25667+
}
25668+
b.writeAlias(alias)
25669+
if forConstraint {
25670+
b.writeByte(1)
25671+
}
25672+
key := deferredConditionalTypeKey{root: root, key: b.hash()}
25673+
if existing := c.deferredConditionalTypes[key]; existing != nil {
25674+
return existing
25675+
}
25676+
result := c.newConditionalType(root, mapper, combinedMapper)
25677+
result.alias = alias
25678+
if forConstraint {
25679+
result.objectFlags |= ObjectFlagsConditionalForConstraint
25680+
}
25681+
c.deferredConditionalTypes[key] = result
25682+
return result
25683+
}
25684+
2561725685
// We tail-recurse for generic conditional types that (a) have not already been evaluated and cached, and
2561825686
// (b) are non distributive, have a check type that is unaffected by instantiation, or have a non-union check
2561925687
// type. Note that recursion is possible only through aliased conditional types, so we only increment the tail

‎tsc/internal/checker/types.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,8 @@ const (
737737
ObjectFlagsIsNeverIntersectionComputed = 1 << 25 // IsNeverLike flag has been computed
738738
ObjectFlagsIsNeverIntersection = 1 << 26 // Intersection reduces to never
739739
ObjectFlagsIsConstrainedTypeVariable = 1 << 27 // T & C, where T's constraint and C are primitives, object, or {}
740+
// Flags that require TypeFlags.Conditional
741+
ObjectFlagsConditionalForConstraint = 1 << 30 // Deferred conditional type made for a constraint
740742
)
741743

742744
// TypeAlias

‎tsc/internal/checker/utilities.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,9 @@ func CompareTypes(t1, t2 *Type) int {
726726
if c := compareTypeMappers(t1.AsConditionalType().mapper, t2.AsConditionalType().mapper); c != 0 {
727727
return c
728728
}
729+
if a, b := t1.objectFlags&ObjectFlagsConditionalForConstraint, t2.objectFlags&ObjectFlagsConditionalForConstraint; a != b {
730+
return cmp.Compare(a, b)
731+
}
729732
case t1.flags&TypeFlagsSubstitution != 0:
730733
if c := CompareTypes(t1.AsSubstitutionType().baseType, t2.AsSubstitutionType().baseType); c != 0 {
731734
return c

‎tsc/testdata/baselines/reference/compiler/boundedRecursiveTypeInferredThroughClosure.errors.txt‎

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)