You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[ObjC] Diagnose implicit type coercion from ObjC 'Class' to object
pointer types.
For example, in Objective-C mode, the initialization of 'x' in:
```
@implementation MyType
+ (void)someClassMethod {
MyType *x = self;
}
@EnD
```
is correctly diagnosed with an incompatible-pointer-types warning, but
in Objective-C++ mode, it is not diagnosed at all -- even though
incompatible pointer conversions generally become an error in C++.
This patch fixes that oversight, allowing implicit conversions
involving Class only to/from unqualified-id, and between qualified and
unqualified Class, where the protocols are compatible.
Note that this does change some behaviors in Objective-C, as well, as
shown by the modified tests.
Of particular note is that assignment from from 'Class<MyProtocol>' to
'id<MyProtocol>' now warns. (Despite appearances, those are not
compatible types. 'Class<MyProtocol>' is not expected to have instance
methods defined by 'MyProtocol', while 'id<MyProtocol>' is.)
Differential Revision: https://reviews.llvm.org/D67983
llvm-svn: 375125
Copy file name to clipboardExpand all lines: clang/test/SemaObjCXX/comptypes-1.mm
+16-16Lines changed: 16 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,7 @@ int main()
38
38
obj_c = obj; /* Ok */
39
39
obj_c = obj_p; // expected-error {{assigning to 'MyClass *' from incompatible type 'id<MyProtocol>'}}
40
40
obj_c = obj_cp; // expected-error {{assigning to 'MyClass *' from incompatible type 'MyOtherClass *'}}
41
-
obj_c = obj_C; //FIXME -- should error {{assigning to 'MyClass *' from incompatible type 'Class'}}
41
+
obj_c = obj_C; //expected-error {{assigning to 'MyClass *' from incompatible type 'Class'}}
42
42
obj_c = obj_CP; // expected-error {{assigning to 'MyClass *' from incompatible type 'Class<MyProtocol>'}}
43
43
44
44
/* Assigning to an 'id<MyProtocol>' variable should generate a
@@ -48,26 +48,26 @@ int main()
48
48
obj_p = obj; /* Ok */
49
49
obj_p = obj_c; // expected-error {{assigning to 'id<MyProtocol>' from incompatible type 'MyClass *'}}
50
50
obj_p = obj_cp; /* Ok */
51
-
obj_p = obj_C; //FIXME -- should error {{assigning to 'id<MyProtocol>' from incompatible type 'Class'}}
52
-
obj_p = obj_CP; //FIXME -- should error {{assigning to 'id<MyProtocol>' from incompatible type 'Class<MyProtocol>'}}
51
+
obj_p = obj_C; //expected-error {{assigning to 'id<MyProtocol>' from incompatible type 'Class'}}
52
+
obj_p = obj_CP; //expected-error {{assigning to 'id<MyProtocol>' from incompatible type 'Class<MyProtocol>'}}
53
53
54
54
/* Assigning to a 'MyOtherClass *' variable should always generate
55
55
a warning, unless done from an 'id' or an 'id<MyProtocol>' (since
56
56
MyOtherClass implements MyProtocol). */
57
57
obj_cp = obj; /* Ok */
58
58
obj_cp = obj_c; // expected-error {{assigning to 'MyOtherClass *' from incompatible type 'MyClass *'}}
59
59
obj_cp = obj_p; /* Ok */
60
-
obj_cp = obj_C; //FIXME -- should error {{assigning to 'MyOtherClass *' from incompatible type 'Class'}}
60
+
obj_cp = obj_C; //expected-error {{assigning to 'MyOtherClass *' from incompatible type 'Class'}}
61
61
obj_cp = obj_CP; // expected-error {{assigning to 'MyOtherClass *' from incompatible type 'Class<MyProtocol>'}}
62
62
63
63
obj_C = obj; // Ok
64
-
obj_C = obj_p; //FIXME -- should error {{assigning to 'Class' from incompatible type 'id<MyProtocol>'}}
65
-
obj_C = obj_c; //FIXME -- should error {{assigning to 'Class' from incompatible type 'MyClass *'}}
66
-
obj_C = obj_cp; //FIXME -- should error {{assigning to 'Class' from incompatible type 'MyOtherClass *'}}
64
+
obj_C = obj_p; //expected-error {{assigning to 'Class' from incompatible type 'id<MyProtocol>'}}
65
+
obj_C = obj_c; //expected-error {{assigning to 'Class' from incompatible type 'MyClass *'}}
66
+
obj_C = obj_cp; //expected-error {{assigning to 'Class' from incompatible type 'MyOtherClass *'}}
67
67
obj_C = obj_CP; // Ok
68
68
69
69
obj_CP = obj; // Ok
70
-
obj_CP = obj_p; // expected-warning {{incompatible pointer types assigning to 'Class<MyProtocol>' from 'id<MyProtocol>'}} FIXME -- should error {{assigning to 'Class<MyProtocol>' from incompatible type 'id<MyProtocol>'}}
70
+
obj_CP = obj_p; // expected-error {{assigning to 'Class<MyProtocol>' from incompatible type 'id<MyProtocol>'}}
71
71
obj_CP = obj_c; // expected-error {{assigning to 'Class<MyProtocol>' from incompatible type 'MyClass *}}
72
72
obj_CP = obj_cp; // expected-error {{assigning to 'Class<MyProtocol>' from incompatible type 'MyOtherClass *'}}
73
73
obj_CP = obj_C; // Ok
@@ -92,8 +92,8 @@ int main()
92
92
if (obj_c == obj_cp) foo(); // expected-warning {{comparison of distinct pointer types ('MyClass *' and 'MyOtherClass *')}}
93
93
if (obj_cp == obj_c) foo(); // expected-warning {{comparison of distinct pointer types ('MyOtherClass *' and 'MyClass *')}}
94
94
95
-
if (obj_c == obj_C) foo(); //FIXME -- should warn {{comparison of distinct pointer types ('MyClass *' and 'Class')}}
96
-
if (obj_C == obj_c) foo(); //FIXME -- should warn {{comparison of distinct pointer types ('Class' and 'MyClass *')}}
95
+
if (obj_c == obj_C) foo(); //expected-warning {{comparison of distinct pointer types ('MyClass *' and 'Class')}}
96
+
if (obj_C == obj_c) foo(); //expected-warning {{comparison of distinct pointer types ('Class' and 'MyClass *')}}
97
97
98
98
if (obj_c == obj_CP) foo(); // expected-warning {{comparison of distinct pointer types ('MyClass *' and 'Class<MyProtocol>')}}
99
99
if (obj_CP == obj_c) foo(); // expected-warning {{comparison of distinct pointer types ('Class<MyProtocol>' and 'MyClass *')}}
@@ -103,15 +103,15 @@ int main()
103
103
if (obj_p == obj_cp) foo(); /* Ok */
104
104
if (obj_cp == obj_p) foo(); /* Ok */
105
105
106
-
if (obj_p == obj_C) foo(); //FIXME -- should warn {{comparison of distinct pointer types ('id<MyProtocol>' and 'Class')}}
107
-
if (obj_C == obj_p) foo(); //FIXME -- should warn {{comparison of distinct pointer types ('Class' and 'id<MyProtocol>')}}
106
+
if (obj_p == obj_C) foo(); //expected-warning {{comparison of distinct pointer types ('id<MyProtocol>' and 'Class')}}
107
+
if (obj_C == obj_p) foo(); //expected-warning {{comparison of distinct pointer types ('Class' and 'id<MyProtocol>')}}
108
108
109
-
if (obj_p == obj_CP) foo(); //FIXME -- should warn {{comparison of distinct pointer types ('id<MyProtocol>' and 'Class<MyProtocol>')}}
110
-
if (obj_CP == obj_p) foo(); //FIXME -- should warn {{comparison of distinct pointer types ('Class<MyProtocol>' and 'id<MyProtocol>')}}
109
+
if (obj_p == obj_CP) foo(); //expected-warning {{comparison of distinct pointer types ('id<MyProtocol>' and 'Class<MyProtocol>')}}
110
+
if (obj_CP == obj_p) foo(); //expected-warning {{comparison of distinct pointer types ('Class<MyProtocol>' and 'id<MyProtocol>')}}
111
111
112
112
/* Comparisons between MyOtherClass * and Class types is a warning */
113
-
if (obj_cp == obj_C) foo(); //FIXME -- should warn {{comparison of distinct pointer types ('MyOtherClass *' and 'Class')}}
114
-
if (obj_C == obj_cp) foo(); //FIXME -- should warn {{comparison of distinct pointer types ('Class' and 'MyOtherClass *')}}
113
+
if (obj_cp == obj_C) foo(); //expected-warning {{comparison of distinct pointer types ('MyOtherClass *' and 'Class')}}
114
+
if (obj_C == obj_cp) foo(); //expected-warning {{comparison of distinct pointer types ('Class' and 'MyOtherClass *')}}
115
115
116
116
if (obj_cp == obj_CP) foo(); // expected-warning {{comparison of distinct pointer types ('MyOtherClass *' and 'Class<MyProtocol>')}}
117
117
if (obj_CP == obj_cp) foo(); // expected-warning {{comparison of distinct pointer types ('Class<MyProtocol>' and 'MyOtherClass *')}}
0 commit comments