summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Lorenz <arphaman@gmail.com>2017-04-06 13:06:34 +0000
committerAlex Lorenz <arphaman@gmail.com>2017-04-06 13:06:34 +0000
commita9832134597af6f5104806385445cdeb1f9901ed (patch)
tree60ceb090e1872161585d251c3aa1599bcdb5cb2a
parent45c936ef86d270ab85dad52dbfaf6dca319f71b0 (diff)
downloadbcm5719-llvm-a9832134597af6f5104806385445cdeb1f9901ed.tar.gz
bcm5719-llvm-a9832134597af6f5104806385445cdeb1f9901ed.zip
[ObjC++] Conversions from specialized to non-specialized Objective-C generic
object types should be preferred over conversions to other object pointers This change ensures that Clang will select the correct overload for the following code sample: void overload(Base *b); void overload(Derived *d); void test(Base<Base *> b) { overload(b); // Select overload(Base *), not overload(Derived *) } rdar://20124827 Differential Revision: https://reviews.llvm.org/D31597 llvm-svn: 299648
-rw-r--r--clang/lib/Sema/SemaOverload.cpp20
-rw-r--r--clang/test/SemaObjCXX/overload.mm27
2 files changed, 44 insertions, 3 deletions
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 477e04db396..c556fb7e43b 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -4047,7 +4047,7 @@ CompareDerivedToBaseConversions(Sema &S, SourceLocation Loc,
= S.Context.canAssignObjCInterfaces(ToPtr1, ToPtr2);
bool ToAssignRight
= S.Context.canAssignObjCInterfaces(ToPtr2, ToPtr1);
-
+
// A conversion to an a non-id object pointer type or qualified 'id'
// type is better than a conversion to 'id'.
if (ToPtr1->isObjCIdType() &&
@@ -4081,11 +4081,25 @@ CompareDerivedToBaseConversions(Sema &S, SourceLocation Loc,
return ImplicitConversionSequence::Better;
// -- "conversion of C* to B* is better than conversion of C* to A*,"
- if (S.Context.hasSameType(FromType1, FromType2) &&
+ if (S.Context.hasSameType(FromType1, FromType2) &&
!FromPtr1->isObjCIdType() && !FromPtr1->isObjCClassType() &&
- (ToAssignLeft != ToAssignRight))
+ (ToAssignLeft != ToAssignRight)) {
+ if (FromPtr1->isSpecialized()) {
+ // "conversion of B<A> * to B * is better than conversion of B * to
+ // C *.
+ bool IsFirstSame =
+ FromPtr1->getInterfaceDecl() == ToPtr1->getInterfaceDecl();
+ bool IsSecondSame =
+ FromPtr1->getInterfaceDecl() == ToPtr2->getInterfaceDecl();
+ if (IsFirstSame) {
+ if (!IsSecondSame)
+ return ImplicitConversionSequence::Better;
+ } else if (IsSecondSame)
+ return ImplicitConversionSequence::Worse;
+ }
return ToAssignLeft? ImplicitConversionSequence::Worse
: ImplicitConversionSequence::Better;
+ }
// -- "conversion of B* to A* is better than conversion of C* to A*,"
if (S.Context.hasSameUnqualifiedType(ToType1, ToType2) &&
diff --git a/clang/test/SemaObjCXX/overload.mm b/clang/test/SemaObjCXX/overload.mm
index bb94d9ed92c..018afc9b429 100644
--- a/clang/test/SemaObjCXX/overload.mm
+++ b/clang/test/SemaObjCXX/overload.mm
@@ -174,3 +174,30 @@ namespace class_id {
void f(Class) { }
void f(id) { }
}
+
+@interface NSDictionary<__covariant KeyType, __covariant ObjectType> : A
+@end
+
+@interface NSMutableDictionary<KeyType, ObjectType> : NSDictionary<KeyType, ObjectType>
+@end
+
+namespace rdar20124827 {
+
+int overload(NSDictionary *) { return 1; }
+
+__attribute__((deprecated)) // expected-note {{'overload' has been explicitly marked deprecated here}}
+int overload(NSMutableDictionary *) { return 0; }
+
+__attribute__((deprecated))
+void overload2(NSDictionary *); // expected-note {{candidate function}}
+void overload2(NSDictionary<A *, A *> *); // expected-note {{candidate function}}
+
+void test(NSDictionary *d1, NSDictionary<A *, A *> *d2, NSMutableDictionary<A *, A *> *m1) {
+ overload(d1);
+ overload(d2); // no warning
+ overload(m1); // expected-warning {{'overload' is deprecated}}
+ overload2(d2); // no warning
+ overload2(m1); // expected-error {{call to 'overload2' is ambiguous}}
+}
+
+}
OpenPOWER on IntegriCloud