@@ -941,13 +941,58 @@ bool swift::isMemberDeclApplied(const DeclContext *DC, Type BaseTy,
941941 IsDeclApplicableRequest (DeclApplicabilityOwner (DC, BaseTy, VD)), false );
942942}
943943
944+ Type swift::tryMergeBaseTypeForCompletionLookup (Type ty1, Type ty2,
945+ DeclContext *dc) {
946+ // Easy case, equivalent so just pick one.
947+ if (ty1->isEqual (ty2))
948+ return ty1;
949+
950+ // Check to see if one is an optional of another. In that case, prefer the
951+ // optional since we can unwrap a single level when doing a lookup.
952+ {
953+ SmallVector<Type, 4 > ty1Optionals;
954+ SmallVector<Type, 4 > ty2Optionals;
955+ auto ty1Unwrapped = ty1->lookThroughAllOptionalTypes (ty1Optionals);
956+ auto ty2Unwrapped = ty2->lookThroughAllOptionalTypes (ty2Optionals);
957+
958+ if (ty1Unwrapped->isEqual (ty2Unwrapped)) {
959+ // We currently only unwrap a single level of optional, so if the
960+ // difference is greater, don't merge.
961+ if (ty1Optionals.size () == 1 && ty2Optionals.empty ())
962+ return ty1;
963+ if (ty2Optionals.size () == 1 && ty1Optionals.empty ())
964+ return ty2;
965+ }
966+ // We don't want to consider subtyping for optional mismatches since
967+ // optional promotion is modelled as a subtype, which isn't useful for us
968+ // (i.e if we have T? and U, preferring U would miss members on T?).
969+ if (ty1Optionals.size () != ty2Optionals.size ())
970+ return Type ();
971+ }
972+
973+ // In general we want to prefer a subtype over a supertype.
974+ if (isSubtypeOf (ty1, ty2, dc))
975+ return ty1;
976+ if (isSubtypeOf (ty2, ty1, dc))
977+ return ty2;
978+
979+ // Incomparable, return null.
980+ return Type ();
981+ }
982+
944983bool swift::isConvertibleTo (Type T1, Type T2, bool openArchetypes,
945984 DeclContext &DC) {
946985 return evaluateOrDefault (DC.getASTContext ().evaluator ,
947986 TypeRelationCheckRequest (TypeRelationCheckInput (&DC, T1, T2,
948987 TypeRelation::ConvertTo, openArchetypes)), false );
949988}
950989
990+ bool swift::isSubtypeOf (Type T1, Type T2, DeclContext *DC) {
991+ return evaluateOrDefault (DC->getASTContext ().evaluator ,
992+ TypeRelationCheckRequest (TypeRelationCheckInput (DC, T1, T2,
993+ TypeRelation::SubtypeOf, /* openArchetypes*/ false )), false );
994+ }
995+
951996Type swift::getRootTypeOfKeypathDynamicMember (SubscriptDecl *SD) {
952997 return evaluateOrDefault (SD->getASTContext ().evaluator ,
953998 RootTypeOfKeypathDynamicMemberRequest{SD}, Type ());
0 commit comments