Skip to content

Commit 941e494

Browse files
committed
Decide whether a type reference can recurse from its type arguments
Comparing types assumes a comparison holds once it is nested in three others of types with the same recursion identity. A type reference created while resolving a type node was tracked by itself rather than by its symbol, as it cannot be the source of generative recursion without being instantiated. Type references are interned, so this depended on how a reference was first created. In interface A<T> { slice(): B<T>; every<S extends T>(predicate: (value: T) => value is S): this is A<S>; flatMap<U>(callback: (value: T) => U | readonly U[] | A<U>): U[]; } interface B<T> extends A<T> {} const a: A<0> = []; const b: B<0> = []; B<0> was tracked by itself when the annotation of b was resolved before checking a instantiated slice, and by its symbol otherwise, so the comparison for b was cut short at different depths, changing the error it reported. Cutting a comparison short can also change whether the types are related. A type reference whose type arguments were not themselves instantiated is now tracked by itself however it was created. For 54921
1 parent 9bf09ef commit 941e494

14 files changed

Lines changed: 366 additions & 374 deletions

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export declare enum ObjectFlags {
3636
IsClassInstanceClone = 1 << 26,
3737
IdenticalBaseTypeCalculated = 1 << 27,
3838
IdenticalBaseTypeExists = 1 << 28,
39-
FromTypeNode = 1 << 29,
4039
IsGenericTypeComputed = 1 << 22,
4140
IsGenericObjectType = 1 << 23,
4241
IsGenericIndexType = 1 << 24,

‎packages/typescript/enums/objectFlags.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export var ObjectFlags;
3636
ObjectFlags[ObjectFlags["IsClassInstanceClone"] = 67108864] = "IsClassInstanceClone";
3737
ObjectFlags[ObjectFlags["IdenticalBaseTypeCalculated"] = 134217728] = "IdenticalBaseTypeCalculated";
3838
ObjectFlags[ObjectFlags["IdenticalBaseTypeExists"] = 268435456] = "IdenticalBaseTypeExists";
39-
ObjectFlags[ObjectFlags["FromTypeNode"] = 536870912] = "FromTypeNode";
4039
ObjectFlags[ObjectFlags["IsGenericTypeComputed"] = 4194304] = "IsGenericTypeComputed";
4140
ObjectFlags[ObjectFlags["IsGenericObjectType"] = 8388608] = "IsGenericObjectType";
4241
ObjectFlags[ObjectFlags["IsGenericIndexType"] = 16777216] = "IsGenericIndexType";

‎tsc/internal/api/enum_values_generated.go‎

Lines changed: 0 additions & 1 deletion
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: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24549,7 +24549,7 @@ func (c *Checker) getTypeFromClassOrInterfaceReference(node *ast.Node, symbol *a
2454924549
// of the class or interface.
2455024550
localTypeArguments := c.fillMissingTypeArguments(c.getTypeArgumentsFromNode(node), typeParameters, minTypeArgumentCount, isJs)
2455124551
typeArguments := append(d.OuterTypeParameters(), localTypeArguments...)
24552-
return c.createTypeReferenceEx(t, typeArguments, ObjectFlagsFromTypeNode)
24552+
return c.createTypeReference(t, typeArguments)
2455324553
}
2455424554
if c.checkNoTypeArguments(node, symbol) {
2455524555
return t
@@ -24644,11 +24644,11 @@ func (c *Checker) createNormalizedTypeReference(target *Type, typeArguments []*T
2464424644
return c.createTypeReference(target, typeArguments)
2464524645
}
2464624646

24647-
func (c *Checker) createNormalizedTupleTypeEx(target *Type, elementTypes []*Type, objectFlags ObjectFlags) *Type {
24647+
func (c *Checker) createNormalizedTupleType(target *Type, elementTypes []*Type) *Type {
2464824648
d := target.AsTupleType()
2464924649
if d.combinedFlags&ElementFlagsNonRequired == 0 {
2465024650
// No need to normalize when we only have regular required elements
24651-
return c.createTypeReferenceEx(target, elementTypes, objectFlags)
24651+
return c.createTypeReference(target, elementTypes)
2465224652
}
2465324653
if d.combinedFlags&ElementFlagsVariadic != 0 {
2465424654
for i, e := range elementTypes {
@@ -24662,7 +24662,7 @@ func (c *Checker) createNormalizedTupleTypeEx(target *Type, elementTypes []*Type
2466224662
})
2466324663
if c.checkCrossProductUnion(checkTypes) {
2466424664
return c.mapType(e, func(t *Type) *Type {
24665-
return c.createNormalizedTupleTypeEx(target, core.ReplaceElement(elementTypes, i, t), objectFlags)
24665+
return c.createNormalizedTupleType(target, core.ReplaceElement(elementTypes, i, t))
2466624666
})
2466724667
}
2466824668
}
@@ -24687,15 +24687,11 @@ func (c *Checker) createNormalizedTupleTypeEx(target *Type, elementTypes []*Type
2468724687
case tupleTarget == c.emptyGenericType:
2468824688
return c.emptyObjectType
2468924689
case len(n.types) != 0:
24690-
return c.createTypeReferenceEx(tupleTarget, n.types, objectFlags)
24690+
return c.createTypeReference(tupleTarget, n.types)
2469124691
}
2469224692
return tupleTarget
2469324693
}
2469424694

24695-
func (c *Checker) createNormalizedTupleType(target *Type, elementTypes []*Type) *Type {
24696-
return c.createNormalizedTupleTypeEx(target, elementTypes, ObjectFlagsNone)
24697-
}
24698-
2469924695
type TupleNormalizer struct {
2470024696
c *Checker
2470124697
types []*Type
@@ -25483,9 +25479,9 @@ func (c *Checker) getTypeFromArrayOrTupleTypeNode(node *ast.Node) *Type {
2548325479
elementTypes = core.Map(node.Elements(), c.getTypeFromTypeNode)
2548425480
}
2548525481
if target.objectFlags&ObjectFlagsTuple != 0 {
25486-
links.resolvedType = c.createNormalizedTupleTypeEx(target, elementTypes, ObjectFlagsFromTypeNode)
25482+
links.resolvedType = c.createNormalizedTupleType(target, elementTypes)
2548725483
} else {
25488-
links.resolvedType = c.createTypeReferenceEx(target, elementTypes, ObjectFlagsFromTypeNode)
25484+
links.resolvedType = c.createTypeReference(target, elementTypes)
2548925485
}
2549025486
}
2549125487
}
@@ -26477,16 +26473,12 @@ func (c *Checker) tryCreateTypeReference(target *Type, typeArguments []*Type) *T
2647726473
}
2647826474

2647926475
func (c *Checker) createTypeReference(target *Type, typeArguments []*Type) *Type {
26480-
return c.createTypeReferenceEx(target, typeArguments, ObjectFlagsNone)
26481-
}
26482-
26483-
func (c *Checker) createTypeReferenceEx(target *Type, typeArguments []*Type, objectFlags ObjectFlags) *Type {
2648426476
id := getTypeListKey(typeArguments)
2648526477
intf := target.AsInterfaceType()
2648626478
if t, ok := intf.instantiations[id]; ok {
2648726479
return t
2648826480
}
26489-
t := c.newObjectType(ObjectFlagsReference|objectFlags|c.getPropagatingFlagsOfTypes(typeArguments, TypeFlagsNone), target.symbol)
26481+
t := c.newObjectType(ObjectFlagsReference|c.getPropagatingFlagsOfTypes(typeArguments, TypeFlagsNone), target.symbol)
2649026482
d := t.AsTypeReference()
2649126483
d.target = target
2649226484
d.resolvedTypeArguments = typeArguments
@@ -28970,7 +28962,7 @@ func (c *Checker) walkBaseConstraint(t *Type, stack []RecursionId) (*Type, int32
2897028962
// levels of nesting, we are presumably exploring a repeating pattern with a long cycle that hasn't
2897128963
// yet triggered the deeply nested limiter. We have no test cases that actually get to 50 levels of
2897228964
// nesting, so it is effectively just a safety stop.
28973-
identity := getRecursionIdentity(t)
28965+
identity := c.getRecursionIdentity(t)
2897428966
if len(stack) < baseConstraintFreeDepth || len(stack) < baseConstraintMaxDepth && !slices.Contains(stack, identity) {
2897528967
constraint = c.computeBaseConstraint(c.getSimplifiedType(t /*writing*/, false), append(stack, identity))
2897628968
} else {

‎tsc/internal/checker/relater.go‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -776,11 +776,11 @@ func (c *Checker) isDeeplyNestedType(t *Type, stack []*Type, maxDepth int) bool
776776
}
777777
}
778778
} else {
779-
identity := getRecursionIdentityFromTarget(target)
779+
identity := c.getRecursionIdentityFromTarget(target)
780780
count := 0
781781
lastGeneration := 0
782782
for _, t := range stack {
783-
if hasMatchingRecursionIdentity(t, identity) {
783+
if c.hasMatchingRecursionIdentity(t, identity) {
784784
// We only count occurrences of a generation at least that of the previous occurrence, since
785785
// newer instantiations caused by recursion are built from the previous ones, whereas nested
786786
// type arguments are older than the types they are arguments of.
@@ -897,21 +897,21 @@ func (c *Checker) deeplyNestedStart(t *Type, stack []*Type, maxDepth int) int {
897897
return 0
898898
}
899899

900-
func hasMatchingRecursionIdentity(t *Type, identity RecursionId) bool {
900+
func (c *Checker) hasMatchingRecursionIdentity(t *Type, identity RecursionId) bool {
901901
target := getRecursionIdentityTarget(t)
902902
if target.flags&TypeFlagsIntersection != 0 {
903903
for _, t := range target.Types() {
904-
if hasMatchingRecursionIdentity(t, identity) {
904+
if c.hasMatchingRecursionIdentity(t, identity) {
905905
return true
906906
}
907907
}
908908
return false
909909
}
910-
return getRecursionIdentityFromTarget(target) == identity
910+
return c.getRecursionIdentityFromTarget(target) == identity
911911
}
912912

913-
func getRecursionIdentity(t *Type) RecursionId {
914-
return getRecursionIdentityFromTarget(getRecursionIdentityTarget(t))
913+
func (c *Checker) getRecursionIdentity(t *Type) RecursionId {
914+
return c.getRecursionIdentityFromTarget(getRecursionIdentityTarget(t))
915915
}
916916

917917
// Get the recursion identity target type from a type. Recursively (a) obtain the target object type of an
@@ -940,7 +940,7 @@ func getRecursionIdentityTarget(t *Type) *Type {
940940
// instantiations of that type have the same recursion identity. The default recursion identity is the object
941941
// identity of the type, meaning that every type is unique. Generally, types with constituents that could circularly
942942
// reference the type have a recursion identity that differs from the object identity.
943-
func getRecursionIdentityFromTarget(t *Type) RecursionId {
943+
func (c *Checker) getRecursionIdentityFromTarget(t *Type) RecursionId {
944944
// Object and array literals are known not to contain recursive references and don't need a recursion identity.
945945
if t.flags&TypeFlagsObject != 0 && !isObjectOrArrayLiteralType(t) {
946946
if t.objectFlags&ObjectFlagsReference != 0 && t.AsTypeReference().node != nil {
@@ -949,14 +949,18 @@ func getRecursionIdentityFromTarget(t *Type) RecursionId {
949949
// unique AST node.
950950
return asRecursionId(t.AsTypeReference().node)
951951
}
952-
if t.symbol != nil && !(t.objectFlags&ObjectFlagsAnonymous != 0 && t.symbol.Flags()&ast.SymbolFlagsClass != 0) && t.objectFlags&ObjectFlagsFromTypeNode == 0 {
952+
// A type reference whose type arguments were not themselves
953+
// instantiated, such as one written out in a type node, cannot be the
954+
// source of generative recursion, so it is tracked by itself. Type
955+
// references are interned, so this depends on the type arguments
956+
// rather than on how the reference was first created.
957+
generative := t.objectFlags&ObjectFlagsReference == 0 || c.typeGeneration(t) > 1
958+
if t.symbol != nil && !(t.objectFlags&ObjectFlagsAnonymous != 0 && t.symbol.Flags()&ast.SymbolFlagsClass != 0) && generative {
953959
// We track object types that have a symbol by that symbol (representing the origin of the type), but
954-
// exclude the static sides of classes (since they share their symbols with the instance sides) and type
955-
// references that originate in resolution of AST type nodes (since such type nodes cannot be the source
956-
// of generative recursion without first being instantiated).
960+
// exclude the static sides of classes (since they share their symbols with the instance sides).
957961
return asRecursionId(t.symbol)
958962
}
959-
if isTupleType(t) && t.objectFlags&ObjectFlagsFromTypeNode == 0 {
963+
if isTupleType(t) && generative {
960964
return asRecursionId(t.Target())
961965
}
962966
}

‎tsc/internal/checker/tracer.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func (a *tracedTypeAdapter) Pattern() *ast.Node {
323323
}
324324

325325
func (a *tracedTypeAdapter) RecursionIdentity() any {
326-
return getRecursionIdentity(a.t).value
326+
return a.checker.getRecursionIdentity(a.t).value
327327
}
328328

329329
func (a *tracedTypeAdapter) Display() string {

‎tsc/internal/checker/types.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,6 @@ const (
734734
// Flags that require TypeFlags.Object and ObjectFlags.Reference
735735
ObjectFlagsIdenticalBaseTypeCalculated = 1 << 27 // has had `getSingleBaseForNonAugmentingSubtype` invoked on it already
736736
ObjectFlagsIdenticalBaseTypeExists = 1 << 28 // has a defined cachedEquivalentBaseType member
737-
ObjectFlagsFromTypeNode = 1 << 29 // Originates in resolution of AST type node
738737
// Flags that require TypeFlags.UnionOrIntersection or TypeFlags.Substitution
739738
ObjectFlagsIsGenericTypeComputed = 1 << 22 // IsGenericObjectType flag has been computed
740739
ObjectFlagsIsGenericObjectType = 1 << 23 // Union or intersection contains generic object type

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ immutable.ts(289,22): error TS2320: Interface 'Indexed<T>' cannot simultaneously
66
immutable.ts(305,22): error TS2320: Interface 'Set<T>' cannot simultaneously extend types 'Seq<never, T>' and 'Set<T>'.
77
Named property 'size' of types 'Seq<never, T>' and 'Set<T>' are not identical.
88
immutable.ts(323,20): error TS2430: Interface 'Seq<K, V>' incorrectly extends interface 'Collection<K, V>'.
9-
The types returned by 'map(...).size' are incompatible between these types.
9+
The types returned by 'map(...).filter(...).size' are incompatible between these types.
1010
Type 'number | undefined' is not assignable to type 'number'.
1111
Type 'undefined' is not assignable to type 'number'.
1212
immutable.ts(341,22): error TS2430: Interface 'Keyed<K, V>' incorrectly extends interface 'Collection<K, V>'.
@@ -382,7 +382,7 @@ immutable.ts(391,22): error TS2430: Interface 'Set<T>' incorrectly extends inter
382382
export interface Seq<K, V> extends Collection<K, V> {
383383
~~~
384384
!!! error TS2430: Interface 'Seq<K, V>' incorrectly extends interface 'Collection<K, V>'.
385-
!!! error TS2430: The types returned by 'map(...).size' are incompatible between these types.
385+
!!! error TS2430: The types returned by 'map(...).filter(...).size' are incompatible between these types.
386386
!!! error TS2430: Type 'number | undefined' is not assignable to type 'number'.
387387
!!! error TS2430: Type 'undefined' is not assignable to type 'number'.
388388
readonly size: number | undefined;

‎tsc/testdata/baselines/reference/compiler/typeReferenceRecursionIdentityCheckOrder(checkorder=default).errors.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ typeReferenceRecursionIdentityCheckOrder.ts(12,7): error TS2322: Type 'never[]'
1212
Type '{ <S extends never>(predicate: (value: never, index: number, array: never[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: never, index: number, array: never[]) => unknown, thisArg?: any): boolean; }' is not assignable to type '<S extends 0>(predicate: (value: 0) => value is S) => this is A<S>'.
1313
Type predicate 'this is any[]' is not assignable to 'this is A<any>'.
1414
Type 'any[]' is not assignable to type 'A<any>'.
15-
The types returned by 'flatMap(...)' are incompatible between these types.
15+
The types returned by 'slice(...).flatMap(...)' are incompatible between these types.
1616
Type '(U | A<U>)[]' is not assignable to type 'U[]'.
1717
Type 'U | A<U>' is not assignable to type 'U'.
1818
'U' could be instantiated with an arbitrary type which could be unrelated to 'U | A<U>'.
@@ -47,7 +47,7 @@ typeReferenceRecursionIdentityCheckOrder.ts(12,7): error TS2322: Type 'never[]'
4747
!!! error TS2322: Type '{ <S extends never>(predicate: (value: never, index: number, array: never[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: never, index: number, array: never[]) => unknown, thisArg?: any): boolean; }' is not assignable to type '<S extends 0>(predicate: (value: 0) => value is S) => this is A<S>'.
4848
!!! error TS2322: Type predicate 'this is any[]' is not assignable to 'this is A<any>'.
4949
!!! error TS2322: Type 'any[]' is not assignable to type 'A<any>'.
50-
!!! error TS2322: The types returned by 'flatMap(...)' are incompatible between these types.
50+
!!! error TS2322: The types returned by 'slice(...).flatMap(...)' are incompatible between these types.
5151
!!! error TS2322: Type '(U | A<U>)[]' is not assignable to type 'U[]'.
5252
!!! error TS2322: Type 'U | A<U>' is not assignable to type 'U'.
5353
!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'U | A<U>'.

0 commit comments

Comments
 (0)