@@ -2,6 +2,7 @@ package checker
22
33import (
44 "cmp"
5+ "iter"
56 "slices"
67 "strings"
78 "sync"
@@ -622,6 +623,9 @@ func CompareTypes(t1, t2 *Type) int {
622623 if c := int (t1 .objectFlags & objectLiteralCreationFlags ) - int (t2 .objectFlags & objectLiteralCreationFlags ); c != 0 {
623624 return c
624625 }
626+ if c := compareObjectMemberSymbols (t1 , t2 ); c != 0 {
627+ return c
628+ }
625629 } else if t1 .objectFlags & t2 .objectFlags & ObjectFlagsObjectRestType != 0 {
626630 // Rest types share the symbol of their declaration, so a rest from a union
627631 // yields one type per constituent, distinguished by the members they keep.
@@ -631,8 +635,13 @@ func CompareTypes(t1, t2 *Type) int {
631635 if c := CompareTypes (d1 , d2 ); c != 0 {
632636 return c
633637 }
634- } else if c := compareObjectMembers (t1 , t2 , false /*withTypes*/ ); c != 0 {
635- return c
638+ } else {
639+ if c := compareObjectMembers (t1 , t2 , false /*withTypes*/ ); c != 0 {
640+ return c
641+ }
642+ if c := compareObjectMemberSymbols (t1 , t2 ); c != 0 {
643+ return c
644+ }
636645 }
637646 }
638647 }
@@ -858,18 +867,19 @@ func hasObjectLiteralMembers(t *Type) bool {
858867 return false
859868 }
860869 if t .symbol == nil {
861- // Types implied by binding patterns have no symbol.
862- return t .objectFlags & ObjectFlagsObjectLiteral != 0
870+ // Types implied by binding patterns and their widened copies have no symbol.
871+ return t .objectFlags & ObjectFlagsAnonymous != 0
863872 }
864873 return t .symbol .Flags ()& ast .SymbolFlagsObjectLiteral != 0
865874}
866875
867876// compareObjectMembers orders resolved object types by what their members
868877// declare, and with withTypes, by the types of members of object literal
869878// types, which are set when the members are created. Members are created per
870- // type, so they are compared by what they declare rather than by identity,
871- // pairwise since getNamedMembers sorts the properties.
879+ // type, so they are compared by what they declare, pairwise since
880+ // getNamedMembers sorts the properties.
872881func compareObjectMembers (t1 , t2 * Type , withTypes bool ) int {
882+ debug .Assert (t1 .objectFlags & t2 .objectFlags & ObjectFlagsMembersResolved != 0 , "Members compared for order must be set when the type is created" )
873883 c := t1 .checker
874884 p1 := t1 .AsStructuredType ().properties
875885 p2 := t2 .AsStructuredType ().properties
@@ -881,13 +891,80 @@ func compareObjectMembers(t1, t2 *Type, withTypes bool) int {
881891 if r := c .compareSymbolDeclarationsAndNames (s1 , s2 ); r != 0 {
882892 return r
883893 }
884- if r := int (s1 .Flags ()& ast .SymbolFlagsOptional ) - int (s2 .Flags ()& ast .SymbolFlagsOptional ); r != 0 {
894+ if r := cmp .Compare (s1 .Flags (), s2 .Flags ()); r != 0 {
895+ return r
896+ }
897+ if r := cmp .Compare (s1 .CheckFlags (), s2 .CheckFlags ()); r != 0 {
885898 return r
886899 }
887900 if withTypes {
888901 if r := CompareTypes (c .peekTypeOfObjectLiteralMember (s1 ), c .peekTypeOfObjectLiteralMember (s2 )); r != 0 {
889902 return r
890903 }
904+ if r := CompareTypes (c .nameTypeOfObjectLiteralMember (s1 ), c .nameTypeOfObjectLiteralMember (s2 )); r != 0 {
905+ return r
906+ }
907+ }
908+ }
909+ return c .compareIndexInfos (t1 .AsStructuredType ().getIndexInfos (), t2 .AsStructuredType ().getIndexInfos ())
910+ }
911+
912+ // compareObjectMemberSymbols orders object types with members that declare the
913+ // same by the members themselves, in member table order. Types derived from
914+ // others, such as spreads, are interned by their member symbols and the order
915+ // of their member tables.
916+ func compareObjectMemberSymbols (t1 , t2 * Type ) int {
917+ c := t1 .checker
918+ n1 , members1 := membersInTableOrder (t1 )
919+ n2 , members2 := membersInTableOrder (t2 )
920+ if n := n1 - n2 ; n != 0 {
921+ return n
922+ }
923+ next2 , stop := iter .Pull (members2 )
924+ defer stop ()
925+ for s1 := range members1 {
926+ s2 , _ := next2 ()
927+ if r := c .compareSymbols (s1 , s2 ); r != 0 {
928+ return r
929+ }
930+ }
931+ return 0
932+ }
933+
934+ func membersInTableOrder (t * Type ) (int , iter.Seq [* ast.Symbol ]) {
935+ d := t .AsStructuredType ()
936+ if d .members != nil {
937+ return d .members .Len (), d .members .Values ()
938+ }
939+ properties := d .getProperties ()
940+ return len (properties ), slices .Values (properties )
941+ }
942+
943+ func (c * Checker ) compareIndexInfos (infos1 , infos2 []* IndexInfo ) int {
944+ if n := len (infos1 ) - len (infos2 ); n != 0 {
945+ return n
946+ }
947+ for i , info1 := range infos1 {
948+ info2 := infos2 [i ]
949+ if r := CompareTypes (info1 .keyType , info2 .keyType ); r != 0 {
950+ return r
951+ }
952+ if r := CompareTypes (info1 .valueType , info2 .valueType ); r != 0 {
953+ return r
954+ }
955+ if info1 .isReadonly != info2 .isReadonly {
956+ return core .IfElse (info1 .isReadonly , 1 , - 1 )
957+ }
958+ if r := c .compareNodes (info1 .declaration , info2 .declaration ); r != 0 {
959+ return r
960+ }
961+ if n := len (info1 .components ) - len (info2 .components ); n != 0 {
962+ return n
963+ }
964+ for j , n1 := range info1 .components {
965+ if r := c .compareNodes (n1 , info2 .components [j ]); r != 0 {
966+ return r
967+ }
891968 }
892969 }
893970 return 0
@@ -902,6 +979,15 @@ func (c *Checker) peekTypeOfObjectLiteralMember(s *ast.Symbol) *Type {
902979 return nil
903980}
904981
982+ // The name type of a member of an object literal type is set when the member
983+ // is created.
984+ func (c * Checker ) nameTypeOfObjectLiteralMember (s * ast.Symbol ) * Type {
985+ if links := c .valueSymbolLinks .TryGet (s ); links != nil {
986+ return links .getNameType ()
987+ }
988+ return nil
989+ }
990+
905991type typeParameterOrigin int
906992
907993const (
0 commit comments