@@ -757,26 +757,49 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
757
757
return result;
758
758
}
759
759
760
+ // Given a possibly-Optional type, return the direct superclass of the
761
+ // (underlying) type wrapped in the same number of optional levels as
762
+ // type.
763
+ static Type getOptionalSuperclass (Type type) {
764
+ int optionalLevels = 0 ;
765
+ while (auto underlying = type->getOptionalObjectType ()) {
766
+ ++optionalLevels;
767
+ type = underlying;
768
+ }
769
+
770
+ if (!type->mayHaveSuperclass ())
771
+ return Type ();
772
+
773
+ auto superclass = type->getSuperclass ();
774
+ if (!superclass)
775
+ return Type ();
776
+
777
+ while (optionalLevels--)
778
+ superclass = OptionalType::get (superclass);
779
+
780
+ return superclass;
781
+ }
782
+
760
783
// / \brief Enumerates all of the 'direct' supertypes of the given type.
761
784
// /
762
785
// / The direct supertype S of a type T is a supertype of T (e.g., T < S)
763
786
// / such that there is no type U where T < U and U < S.
764
787
static SmallVector<Type, 4 > enumerateDirectSupertypes (Type type) {
765
788
SmallVector<Type, 4 > result;
766
789
767
- if (type->mayHaveSuperclass ()) {
790
+ if (type->is <InOutType>() || type->is <LValueType>()) {
791
+ type = type->getWithoutSpecifierType ();
792
+ result.push_back (type);
793
+ }
794
+
795
+ if (auto superclass = getOptionalSuperclass (type)) {
768
796
// FIXME: Can also weaken to the set of protocol constraints, but only
769
797
// if there are any protocols that the type conforms to but the superclass
770
798
// does not.
771
799
772
- // If there is a superclass, it is a direct supertype.
773
- if (auto superclass = type->getSuperclass ())
774
- result.push_back (superclass);
800
+ result.push_back (superclass);
775
801
}
776
802
777
- if (type->is <InOutType>() || type->is <LValueType>())
778
- result.push_back (type->getWithoutSpecifierType ());
779
-
780
803
// FIXME: lots of other cases to consider!
781
804
return result;
782
805
}
0 commit comments