@@ -70,9 +70,8 @@ const (
7070 RelationComparisonResultReportsUnmeasurable RelationComparisonResult = 1 << 3
7171 RelationComparisonResultReportsUnreliable RelationComparisonResult = 1 << 4
7272 RelationComparisonResultComplexityOverflow RelationComparisonResult = 1 << 5
73- RelationComparisonResultStackDepthOverflow RelationComparisonResult = 1 << 6
7473 RelationComparisonResultReportsMask = RelationComparisonResultReportsUnmeasurable | RelationComparisonResultReportsUnreliable
75- RelationComparisonResultOverflow = RelationComparisonResultComplexityOverflow | RelationComparisonResultStackDepthOverflow
74+ RelationComparisonResultOverflow = RelationComparisonResultComplexityOverflow
7675)
7776
7877type DiagnosticAndArguments struct {
@@ -372,15 +371,14 @@ func (c *Checker) checkTypeRelatedToEx(
372371 if r .overflow {
373372 // Record this relation as having failed such that we don't attempt the overflowing operation again.
374373 id , _ := getRelationKey (source , target , IntersectionStateNone , relation == c .identityRelation , false /*ignoreConstraints*/ )
375- relation .set (id , RelationComparisonResultFailed | core . IfElse ( r . relationCount <= 0 , RelationComparisonResultComplexityOverflow , RelationComparisonResultStackDepthOverflow ) )
374+ relation .set (id , RelationComparisonResultFailed | RelationComparisonResultComplexityOverflow )
376375 if tr := c .tracer ; tr != nil {
377376 tr .Instant (tracing .PhaseCheckTypes , "checkTypeRelatedTo_DepthLimit" , map [string ]any {"sourceId" : source .id , "targetId" : target .id , "depth" : len (r .sourceStack ), "targetDepth" : len (r .targetStack )})
378377 }
379- message := core .IfElse (r .relationCount <= 0 , diagnostics .Excessive_complexity_comparing_types_0_and_1 , diagnostics .Excessive_stack_depth_comparing_types_0_and_1 )
380378 if errorNode == nil {
381379 errorNode = c .currentNode
382380 }
383- c .reportDiagnostic (NewDiagnosticForNode (errorNode , message , c .TypeToString (source ), c .TypeToString (target )), diagnosticOutput )
381+ c .reportDiagnostic (NewDiagnosticForNode (errorNode , diagnostics . Excessive_complexity_comparing_types_0_and_1 , c .TypeToString (source ), c .TypeToString (target )), diagnosticOutput )
384382 } else if r .errorChain != nil {
385383 // Check if we should issue an extra diagnostic to produce a quickfix for a slightly incorrect import statement
386384 if headMessage != nil && errorNode != nil && result == TernaryFalse && source .symbol != nil && c .exportTypeLinks .Has (source .symbol ) {
@@ -767,67 +765,70 @@ func isExcessPropertyCheckTarget(t *Type) bool {
767765// structurally equal to at least maxDepth levels, but unequal at some level beyond that.
768766func (c * Checker ) isDeeplyNestedType (t * Type , stack []* Type , maxDepth int ) bool {
769767 if len (stack ) >= maxDepth {
770- if t .objectFlags & ObjectFlagsInstantiatedMapped == ObjectFlagsInstantiatedMapped {
771- t = c .getMappedTargetWithSymbol (t )
772- }
773- if t .flags & TypeFlagsIntersection != 0 {
774- for _ , t := range t .Types () {
768+ target := getRecursionIdentityTarget (t )
769+ if target .flags & TypeFlagsIntersection != 0 {
770+ for _ , t := range target .Types () {
775771 if c .isDeeplyNestedType (t , stack , maxDepth ) {
776772 return true
777773 }
778774 }
779- }
780- identity := getRecursionIdentity (t )
781- count := 0
782- lastTypeId := TypeId (0 )
783- for _ , t := range stack {
784- if c .hasMatchingRecursionIdentity (t , identity ) {
785- // We only count occurrences with a higher type id than the previous occurrence, since higher
786- // type ids are an indicator of newer instantiations caused by recursion.
787- if t .id >= lastTypeId {
788- count ++
789- if count >= maxDepth {
790- return true
775+ } else {
776+ identity := getRecursionIdentityFromTarget (target )
777+ count := 0
778+ lastTypeId := TypeId (0 )
779+ for _ , t := range stack {
780+ if hasMatchingRecursionIdentity (t , identity ) {
781+ // We only count occurrences with a higher type id than the previous occurrence, since higher
782+ // type ids are an indicator of newer instantiations caused by recursion.
783+ if t .id >= lastTypeId {
784+ count ++
785+ if count >= maxDepth {
786+ return true
787+ }
791788 }
789+ lastTypeId = t .id
792790 }
793- lastTypeId = t .id
794791 }
795792 }
796793 }
797794 return false
798795}
799796
800- // Unwrap nested homomorphic mapped types and return the deepest target type that has a symbol. This better
801- // preserves unique type identities for mapped types applied to explicitly written object literals. For example
802- // in `Mapped<{ x: Mapped<{ x: Mapped<{ x: string }>}>}>`, each of the mapped type applications will have a
803- // unique recursion identity (that of their target object type literal) and thus avoid appearing deeply nested.
804- func (c * Checker ) getMappedTargetWithSymbol (t * Type ) * Type {
805- for {
806- if t .objectFlags & ObjectFlagsInstantiatedMapped == ObjectFlagsInstantiatedMapped {
807- target := c .getModifiersTypeFromMappedType (t )
808- if target != nil && (target .symbol != nil || target .flags & TypeFlagsIntersection != 0 &&
809- core .Some (target .Types (), func (t * Type ) bool { return t .symbol != nil })) {
810- t = target
811- continue
797+ func hasMatchingRecursionIdentity (t * Type , identity RecursionId ) bool {
798+ target := getRecursionIdentityTarget (t )
799+ if target .flags & TypeFlagsIntersection != 0 {
800+ for _ , t := range target .Types () {
801+ if hasMatchingRecursionIdentity (t , identity ) {
802+ return true
812803 }
813804 }
814- return t
805+ return false
815806 }
807+ return getRecursionIdentityFromTarget (target ) == identity
816808}
817809
818- func (c * Checker ) hasMatchingRecursionIdentity (t * Type , identity RecursionId ) bool {
819- if t .objectFlags & ObjectFlagsInstantiatedMapped == ObjectFlagsInstantiatedMapped {
820- t = c .getMappedTargetWithSymbol (t )
810+ func getRecursionIdentity (t * Type ) RecursionId {
811+ return getRecursionIdentityFromTarget (getRecursionIdentityTarget (t ))
812+ }
813+
814+ // Get the recursion identity target type from a type. Recursively (a) obtain the target object type of an
815+ // indexed access (i.e. the T in T[K]), and (b) unwrap nested homomorphic mapped types and return the deepest
816+ // target type that has a symbol. The unwrapping better preserves unique type identities for mapped types applied
817+ // to explicitly written object literals. For example in `Mapped<{ x: Mapped<{ x: Mapped<{ x: string }>}>}>`,
818+ // each of the mapped type applications will have a unique recursion identity (that of their target object type
819+ // literal) and thus avoid appearing deeply nested.
820+ func getRecursionIdentityTarget (t * Type ) * Type {
821+ if t .flags & TypeFlagsIndexedAccess != 0 {
822+ return getRecursionIdentityTarget (t .AsIndexedAccessType ().objectType )
821823 }
822- if t .flags & TypeFlagsIntersection != 0 {
823- for _ , t := range t . Types () {
824- if c . hasMatchingRecursionIdentity ( t , identity ) {
825- return true
826- }
824+ if t .objectFlags & ObjectFlagsInstantiatedMapped == ObjectFlagsInstantiatedMapped {
825+ target := t . checker . getModifiersTypeFromMappedType ( t )
826+ if target != nil && ( target . symbol != nil || target . flags & TypeFlagsIntersection != 0 &&
827+ core . Some ( target . Types (), func ( t * Type ) bool { return t . symbol != nil })) {
828+ return getRecursionIdentityTarget ( target )
827829 }
828- return false
829830 }
830- return getRecursionIdentity ( t ) == identity
831+ return t
831832}
832833
833834// The recursion identity of a type is an object identity that is shared among multiple instantiations of the type.
@@ -836,7 +837,7 @@ func (c *Checker) hasMatchingRecursionIdentity(t *Type, identity RecursionId) bo
836837// instantiations of that type have the same recursion identity. The default recursion identity is the object
837838// identity of the type, meaning that every type is unique. Generally, types with constituents that could circularly
838839// reference the type have a recursion identity that differs from the object identity.
839- func getRecursionIdentity (t * Type ) RecursionId {
840+ func getRecursionIdentityFromTarget (t * Type ) RecursionId {
840841 // Object and array literals are known not to contain recursive references and don't need a recursion identity.
841842 if t .flags & TypeFlagsObject != 0 && ! isObjectOrArrayLiteralType (t ) {
842843 if t .objectFlags & ObjectFlagsReference != 0 && t .AsTypeReference ().node != nil {
@@ -861,14 +862,6 @@ func getRecursionIdentity(t *Type) RecursionId {
861862 // have the same recursion identity.
862863 return asRecursionId (t .symbol )
863864 }
864- if t .flags & TypeFlagsIndexedAccess != 0 {
865- // Identity is the leftmost object type in a chain of indexed accesses, eg, in A[P1][P2][P3] it is A.
866- t = t .AsIndexedAccessType ().objectType
867- for t .flags & TypeFlagsIndexedAccess != 0 {
868- t = t .AsIndexedAccessType ().objectType
869- }
870- return asRecursionId (t )
871- }
872865 if t .flags & TypeFlagsConditional != 0 {
873866 // The root object represents the origin of the conditional type
874867 return asRecursionId (t .AsConditionalType ().root .node .AsNode ())
@@ -3112,10 +3105,7 @@ func (r *Relater) recursiveTypeRelatedTo(source *Type, target *Type, reportError
31123105 } else {
31133106 r .c .reliabilityFlags |= entry & (RelationComparisonResultReportsUnmeasurable | RelationComparisonResultReportsUnreliable )
31143107 if reportErrors && entry & RelationComparisonResultOverflow != 0 {
3115- message := core .IfElse (entry & RelationComparisonResultComplexityOverflow != 0 ,
3116- diagnostics .Excessive_complexity_comparing_types_0_and_1 ,
3117- diagnostics .Excessive_stack_depth_comparing_types_0_and_1 )
3118- r .reportError (message , r .c .TypeToString (source ), r .c .TypeToString (target ))
3108+ r .reportError (diagnostics .Excessive_complexity_comparing_types_0_and_1 , r .c .TypeToString (source ), r .c .TypeToString (target ))
31193109 }
31203110 if entry & RelationComparisonResultSucceeded != 0 {
31213111 return TernaryTrue
@@ -3141,8 +3131,10 @@ func (r *Relater) recursiveTypeRelatedTo(source *Type, target *Type, reportError
31413131 }
31423132 }
31433133 if len (r .sourceStack ) == 100 || len (r .targetStack ) == 100 {
3144- r .overflow = true
3145- return TernaryFalse
3134+ // We stop relating if we reach 100 levels of nesting. This is a backstop to catch infinite recursion
3135+ // that wasn't caught by isDeeplyNestedType. It will also stop relating types that truly are over 100
3136+ // levels deep, but those are exceedingly rare.
3137+ return TernaryMaybe
31463138 }
31473139 maybeStart := len (r .maybeKeys )
31483140 r .maybeKeys = append (r .maybeKeys , id )
@@ -4846,7 +4838,6 @@ func (r *Relater) reportRelationError(message *diagnostics.Message, source *Type
48464838 // Suppress if next message is an excessive complexity/stack depth message for source and target or a readonly
48474839 // vs. mutable error for source and target
48484840 case diagnostics .Excessive_complexity_comparing_types_0_and_1 ,
4849- diagnostics .Excessive_stack_depth_comparing_types_0_and_1 ,
48504841 diagnostics .The_type_0_is_readonly_and_cannot_be_assigned_to_the_mutable_type_1 :
48514842 if r .chainArgsMatch (generalizedSourceType , targetType ) {
48524843 return
0 commit comments