@@ -1141,6 +1141,7 @@ Type TypeBase::stripConcurrency(bool recurse, bool dropGlobalActor) {
11411141 if (!newMembers.empty ()) {
11421142 return ProtocolCompositionType::get (
11431143 getASTContext (), newMembers,
1144+ protocolCompositionType->getInverses (),
11441145 protocolCompositionType->hasExplicitAnyObject ());
11451146 }
11461147
@@ -1606,18 +1607,20 @@ static void addProtocols(Type T,
16061607 SmallVectorImpl<ProtocolDecl *> &Protocols,
16071608 ParameterizedProtocolMap &Parameterized,
16081609 Type &Superclass,
1610+ InvertibleProtocolSet &Inverses,
16091611 bool &HasExplicitAnyObject) {
16101612 if (auto Proto = T->getAs <ProtocolType>()) {
16111613 Protocols.push_back (Proto->getDecl ());
16121614 return ;
16131615 }
16141616
16151617 if (auto PC = T->getAs <ProtocolCompositionType>()) {
1616- if (PC->hasExplicitAnyObject ())
1617- HasExplicitAnyObject = true ;
1618- for (auto P : PC->getMembers ())
1619- addProtocols (P, Protocols, Parameterized, Superclass,
1618+ Inverses. insertAll (PC->getInverses ());
1619+ HasExplicitAnyObject |= PC-> hasExplicitAnyObject () ;
1620+ for (auto P : PC->getMembers ()) {
1621+ addProtocols (P, Protocols, Parameterized, Superclass, Inverses,
16201622 HasExplicitAnyObject);
1623+ }
16211624 return ;
16221625 }
16231626
@@ -1918,6 +1921,7 @@ CanType TypeBase::computeCanonicalType() {
19181921 assert (!CanProtos.empty () && " Non-canonical empty composition?" );
19191922 const ASTContext &C = CanProtos[0 ]->getASTContext ();
19201923 Type Composition = ProtocolCompositionType::get (C, CanProtos,
1924+ PCT->getInverses (),
19211925 PCT->hasExplicitAnyObject ());
19221926 Result = Composition.getPointer ();
19231927 break ;
@@ -4009,27 +4013,22 @@ bool ProtocolCompositionType::requiresClass() {
40094013
40104014// / Constructs a protocol composition corresponding to the `Any` type.
40114015Type ProtocolCompositionType::theAnyType (const ASTContext &C) {
4012- return ProtocolCompositionType::get (C, {}, /* HasExplicitAnyObject=*/ false );
4016+ return ProtocolCompositionType::get (C, {}, /* Inverses=*/ {},
4017+ /* HasExplicitAnyObject=*/ false );
40134018}
40144019
40154020// / Constructs a protocol composition containing the `AnyObject` constraint.
40164021Type ProtocolCompositionType::theAnyObjectType (const ASTContext &C) {
4017- return ProtocolCompositionType::get (C, {}, /* HasExplicitAnyObject=*/ true );
4022+ return ProtocolCompositionType::get (C, {}, /* Inverses=*/ {},
4023+ /* HasExplicitAnyObject=*/ true );
40184024}
40194025
40204026Type ProtocolCompositionType::getInverseOf (const ASTContext &C,
40214027 InvertibleProtocolKind IP) {
4022- return ProtocolCompositionType::get (C, {}, {IP},
4028+ return ProtocolCompositionType::get (C, {}, /* Inverses= */ {IP},
40234029 /* HasExplicitAnyObject=*/ false );
40244030}
40254031
4026- Type ProtocolCompositionType::get (const ASTContext &C, ArrayRef<Type> Members,
4027- bool HasExplicitAnyObject) {
4028- return ProtocolCompositionType::get (C, Members,
4029- /* Inverses=*/ {},
4030- HasExplicitAnyObject);
4031- }
4032-
40334032Type ProtocolCompositionType::get (const ASTContext &C,
40344033 ArrayRef<Type> Members,
40354034 InvertibleProtocolSet Inverses,
@@ -4055,29 +4054,29 @@ Type ProtocolCompositionType::get(const ASTContext &C,
40554054 if (!t->isCanonical ())
40564055 return build (C, Members, Inverses, HasExplicitAnyObject);
40574056 }
4058-
4057+
40594058 Type Superclass;
40604059 SmallVector<ProtocolDecl *, 4 > Protocols;
40614060 ParameterizedProtocolMap Parameterized;
40624061 for (Type t : Members) {
4063- addProtocols (t, Protocols, Parameterized, Superclass, HasExplicitAnyObject);
4062+ addProtocols (t, Protocols, Parameterized, Superclass,
4063+ Inverses, HasExplicitAnyObject);
40644064 }
40654065
4066+ // Form the set of canonical component types.
4067+ SmallVector<Type, 4 > CanTypes;
4068+
40664069 // The presence of a superclass constraint makes AnyObject redundant.
4067- if (Superclass)
4070+ if (Superclass) {
40684071 HasExplicitAnyObject = false ;
4072+ CanTypes.push_back (Superclass->getCanonicalType ());
4073+ }
40694074
40704075 // If there are any parameterized protocols, the canonicalization
40714076 // algorithm gets more complex.
4072-
4073- // Form the set of canonical component types.
4074- SmallVector<Type, 4 > CanTypes;
4075- if (Superclass)
4076- CanTypes.push_back (Superclass->getCanonicalType ());
4077-
40784077 canonicalizeProtocols (Protocols, &Parameterized);
40794078
4080- for (auto proto: Protocols) {
4079+ for (auto proto : Protocols) {
40814080 // If we have a parameterized type for this protocol, use the
40824081 // canonical type of that. Sema should prevent us from building
40834082 // compositions with the same protocol and conflicting constraints.
@@ -5346,6 +5345,7 @@ case TypeKind::Id:
53465345
53475346 return ProtocolCompositionType::get (Ptr->getASTContext (),
53485347 substMembers,
5348+ pc->getInverses (),
53495349 pc->hasExplicitAnyObject ());
53505350 }
53515351
0 commit comments