blob: 87209581534c5598299be55303df10c98939260d [file] [log] [blame]
Steve Naroffc715e782009-07-29 15:09:39 +00001// RUN: clang-cc -fsyntax-only -verify %s
Daniel Dunbar5e155f02008-09-11 23:12:46 +00002// <rdar://problem/6212771>
3
4#define nil ((void*) 0)
5
6@interface A
7@property int x;
8@end
9
10@interface B : A
11@end
12
13// Basic checks...
14id f0(int cond, id a, void *b) {
15 return cond ? a : b;
16}
17A *f0_a(int cond, A *a, void *b) {
18 return cond ? a : b;
19}
20
21id f1(int cond, id a) {
22 return cond ? a : nil;
23}
24A *f1_a(int cond, A *a) {
25 return cond ? a : nil;
26}
27
Steve Naroffc715e782009-07-29 15:09:39 +000028void *f1_const_a(int x, void *p, const A * q) {
29 void *r = x ? p : q; // expected-warning{{initializing 'void const *' discards qualifiers, expected 'void *'}}
30 return r;
31}
32
Daniel Dunbar5e155f02008-09-11 23:12:46 +000033// Check interaction with qualified id
34
35@protocol P0 @end
36
37id f2(int cond, id<P0> a, void *b) {
38 return cond ? a : b;
39}
40
41id f3(int cond, id<P0> a) {
42 return cond ? a : nil;
43}
44
45// Check that result actually has correct type.
46
47// Using properties is one way to find the compiler internal type of a
48// conditional expression. Simple assignment doesn't work because if
49// the type is id then it can be implicitly promoted.
50@protocol P1
51@property int x;
52@end
53
54int f5(int cond, id<P1> a, id<P1> b) {
Steve Naroffc715e782009-07-29 15:09:39 +000055 return (cond ? a : b).x;
Daniel Dunbar5e155f02008-09-11 23:12:46 +000056}
57int f5_a(int cond, A *a, A *b) {
58 return (cond ? a : b).x;
59}
60int f5_b(int cond, A *a, B *b) {
61 return (cond ? a : b).x;
62}
63
64int f6(int cond, id<P1> a, void *b) {
65 // This should result in something with id type, currently.
Steve Naroffc715e782009-07-29 15:09:39 +000066 return (cond ? a : b).x; // expected-error {{member reference base type 'void *' is not a structure or union}}
Daniel Dunbar5e155f02008-09-11 23:12:46 +000067}
68
69int f7(int cond, id<P1> a) {
70 return (cond ? a : nil).x;
71}
72
73int f8(int cond, id<P1> a, A *b) {
Steve Naroffc715e782009-07-29 15:09:39 +000074 return a == b; // expected-warning {{comparison of distinct pointer types ('id<P1>' and 'A *')}}
Daniel Dunbar5e155f02008-09-11 23:12:46 +000075}
76
77int f9(int cond, id<P1> a, A *b) {
Steve Naroffc715e782009-07-29 15:09:39 +000078 return (cond ? a : b).x; // expected-warning {{incompatible operand types ('id<P1>' and 'A *')}} \
79 expected-error {{property 'x' not found on object of type 'id'}}
Daniel Dunbar5e155f02008-09-11 23:12:46 +000080}