@@ -889,16 +889,20 @@ Type TypeBase::getRValueInstanceType() {
889
889
890
890
// / \brief Collect the protocols in the existential type T into the given
891
891
// / vector.
892
- static void addProtocols (Type T, SmallVectorImpl<ProtocolDecl *> &Protocols,
893
- Type &Superclass) {
892
+ static void addProtocols (Type T,
893
+ SmallVectorImpl<ProtocolDecl *> &Protocols,
894
+ Type &Superclass,
895
+ bool &HasExplicitAnyObject) {
894
896
if (auto Proto = T->getAs <ProtocolType>()) {
895
897
Protocols.push_back (Proto->getDecl ());
896
898
return ;
897
899
}
898
900
899
901
if (auto PC = T->getAs <ProtocolCompositionType>()) {
902
+ if (PC->hasExplicitAnyObject ())
903
+ HasExplicitAnyObject = true ;
900
904
for (auto P : PC->getMembers ())
901
- addProtocols (P, Protocols, Superclass);
905
+ addProtocols (P, Protocols, Superclass, HasExplicitAnyObject );
902
906
return ;
903
907
}
904
908
@@ -2780,12 +2784,16 @@ Type ProtocolCompositionType::get(const ASTContext &C,
2780
2784
Type Superclass;
2781
2785
SmallVector<ProtocolDecl *, 4 > Protocols;
2782
2786
for (Type t : Members) {
2783
- addProtocols (t, Protocols, Superclass);
2787
+ addProtocols (t, Protocols, Superclass, HasExplicitAnyObject );
2784
2788
}
2785
2789
2786
2790
// Minimize the set of protocols composed together.
2787
2791
ProtocolType::canonicalizeProtocols (Protocols);
2788
2792
2793
+ // The presence of a superclass constraint makes AnyObject redundant.
2794
+ if (Superclass)
2795
+ HasExplicitAnyObject = false ;
2796
+
2789
2797
// If one protocol remains with no further constraints, its nominal
2790
2798
// type is the canonical type.
2791
2799
if (Protocols.size () == 1 && !Superclass && !HasExplicitAnyObject)
@@ -2803,8 +2811,7 @@ Type ProtocolCompositionType::get(const ASTContext &C,
2803
2811
});
2804
2812
2805
2813
// TODO: Canonicalize away HasExplicitAnyObject if it is implied
2806
- // by one of our member protocols or the presence of a superclass
2807
- // constraint.
2814
+ // by one of our member protocols.
2808
2815
return build (C, CanTypes, HasExplicitAnyObject);
2809
2816
}
2810
2817
0 commit comments