1 |
149 |
jeremybenn |
/* Test various ObjC types assignments and comparisons. */
|
2 |
|
|
/* Author: Nicola Pero <nicola@brainstorm.co.uk>. */
|
3 |
|
|
/* { dg-do compile } */
|
4 |
|
|
#include <objc/objc.h>
|
5 |
|
|
|
6 |
|
|
@protocol MyProtocol
|
7 |
|
|
- (void) foo;
|
8 |
|
|
@end
|
9 |
|
|
|
10 |
|
|
@interface MyClass
|
11 |
|
|
@end
|
12 |
|
|
|
13 |
|
|
@interface MyOtherClass <MyProtocol>
|
14 |
|
|
- (void) foo;
|
15 |
|
|
@end
|
16 |
|
|
|
17 |
|
|
int main()
|
18 |
|
|
{
|
19 |
|
|
id obj = nil;
|
20 |
|
|
id<MyProtocol> obj_p = nil;
|
21 |
|
|
MyClass *obj_c = nil;
|
22 |
|
|
MyOtherClass *obj_cp = nil;
|
23 |
|
|
Class obj_C = Nil;
|
24 |
|
|
|
25 |
|
|
/* Assigning to an 'id' variable should never
|
26 |
|
|
generate a warning. */
|
27 |
|
|
obj = obj_p; /* Ok */
|
28 |
|
|
obj = obj_c; /* Ok */
|
29 |
|
|
obj = obj_cp; /* Ok */
|
30 |
|
|
obj = obj_C; /* Ok */
|
31 |
|
|
|
32 |
|
|
/* Assigning to a 'MyClass *' variable should always generate a
|
33 |
|
|
warning, unless done from an 'id'. */
|
34 |
|
|
obj_c = obj; /* Ok */
|
35 |
|
|
obj_c = obj_p; /* { dg-warning "distinct Objective\\-C type" } */
|
36 |
|
|
obj_c = obj_cp; /* { dg-warning "distinct Objective\\-C type" } */
|
37 |
|
|
obj_c = obj_C; /* { dg-warning "distinct Objective\\-C type" } */
|
38 |
|
|
|
39 |
|
|
/* Assigning to an 'id<MyProtocol>' variable should generate a
|
40 |
|
|
warning if done from a 'MyClass *' (which doesn't implement
|
41 |
|
|
MyProtocol), but not from an 'id' or from a 'MyOtherClass *'
|
42 |
|
|
(which implements MyProtocol). */
|
43 |
|
|
obj_p = obj; /* Ok */
|
44 |
|
|
obj_p = obj_c; /* { dg-warning "does not implement" } */
|
45 |
|
|
obj_p = obj_cp; /* Ok */
|
46 |
|
|
obj_p = obj_C; /* { dg-warning "distinct Objective\\-C type" } */
|
47 |
|
|
|
48 |
|
|
/* Assigning to a 'MyOtherClass *' variable should always generate
|
49 |
|
|
a warning, unless done from an 'id' or an 'id<MyProtocol>' (since
|
50 |
|
|
MyOtherClass implements MyProtocol). */
|
51 |
|
|
obj_cp = obj; /* Ok */
|
52 |
|
|
obj_cp = obj_c; /* { dg-warning "distinct Objective\\-C type" } */
|
53 |
|
|
obj_cp = obj_p; /* Ok */
|
54 |
|
|
obj_cp = obj_C; /* { dg-warning "distinct Objective\\-C type" } */
|
55 |
|
|
|
56 |
|
|
/* Any comparison involving an 'id' must be without warnings. */
|
57 |
|
|
if (obj == obj_p) ; /* Ok */ /*Bogus warning here in 2.95.4*/
|
58 |
|
|
if (obj_p == obj) ; /* Ok */
|
59 |
|
|
if (obj == obj_c) ; /* Ok */
|
60 |
|
|
if (obj_c == obj) ; /* Ok */
|
61 |
|
|
if (obj == obj_cp) ; /* Ok */
|
62 |
|
|
if (obj_cp == obj) ; /* Ok */
|
63 |
|
|
if (obj == obj_C) ; /* Ok */
|
64 |
|
|
if (obj_C == obj) ; /* Ok */
|
65 |
|
|
|
66 |
|
|
/* Any comparison between 'MyClass *' and anything which is not an 'id'
|
67 |
|
|
must generate a warning. */
|
68 |
|
|
if (obj_c == obj_p) ; /* { dg-warning "lacks a cast" } */
|
69 |
|
|
if (obj_p == obj_c) ; /* { dg-warning "lacks a cast" } */
|
70 |
|
|
if (obj_c == obj_cp) ; /* { dg-warning "lacks a cast" } */
|
71 |
|
|
if (obj_cp == obj_c) ; /* { dg-warning "lacks a cast" } */
|
72 |
|
|
if (obj_c == obj_C) ; /* { dg-warning "lacks a cast" } */
|
73 |
|
|
if (obj_C == obj_c) ; /* { dg-warning "lacks a cast" } */
|
74 |
|
|
|
75 |
|
|
/* Any comparison between 'MyOtherClass *' (which implements
|
76 |
|
|
MyProtocol) and an 'id' implementing MyProtocol are Ok. */
|
77 |
|
|
if (obj_cp == obj_p) ; /* Ok */
|
78 |
|
|
if (obj_p == obj_cp) ; /* Ok */
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
if (obj_p == obj_C) ; /* { dg-warning "lacks a cast" } */
|
82 |
|
|
if (obj_C == obj_p) ; /* { dg-warning "lacks a cast" } */
|
83 |
|
|
if (obj_cp == obj_C) ; /* { dg-warning "lacks a cast" } */
|
84 |
|
|
if (obj_C == obj_cp) ; /* { dg-warning "lacks a cast" } */
|
85 |
|
|
|
86 |
|
|
return 0;
|
87 |
|
|
}
|