arc-type-conversion.mm revision 31862ba5ea70b1f2c81d03f8a0100b61cd6f06f6
1// RUN: %clang_cc1 -fobjc-nonfragile-abi -fsyntax-only -fobjc-arc -verify -fblocks %s
2// rdar://8843600
3
4void * cvt(id arg) // expected-note{{candidate function not viable: cannot convert argument of incomplete type 'void *' to '__strong id'}}
5{
6  void* voidp_val;
7  (void)(int*)arg; // expected-error {{cast of an Objective-C pointer to 'int *' is disallowed with ARC}}
8  (void)(id)arg;
9  (void)(__autoreleasing id*)arg; // expected-error{{C-style cast from 'id' to '__autoreleasing id *' casts away qualifiers}}
10  (void)(id*)arg; // expected-error {{pointer to non-const type 'id' with no explicit ownership}} \
11  // expected-error{{C-style cast from 'id' to '__autoreleasing id *' casts away qualifiers}}
12
13  (void)(__autoreleasing id**)voidp_val;
14  (void)(void*)voidp_val;
15  (void)(void**)arg; // expected-error {{cast of an Objective-C pointer to 'void **' is disallowed}}
16  cvt((void*)arg); // expected-error {{cast of Objective-C pointer type 'id' to C pointer type 'void *' requires a bridged cast}} \
17                   // expected-error {{no matching function for call to 'cvt'}} \
18  // expected-note{{use __bridge to convert directly (no change in ownership)}} \
19  // expected-note{{use __bridge_retained to make an ARC object available as a +1 'void *'}}
20  cvt(0);
21  (void)(__strong id**)(0);
22
23  // FIXME: Diagnostic could be better here.
24  return arg; // expected-error{{cannot initialize return object of type 'void *' with an lvalue of type '__strong id'}}
25}
26
27// rdar://8898937
28namespace rdar8898937 {
29
30typedef void (^dispatch_block_t)(void);
31
32void dispatch_once(dispatch_block_t block);
33static void _dispatch_once(dispatch_block_t block)
34{
35  dispatch_once(block);
36}
37
38}
39
40void static_casts(id arg) {
41  void* voidp_val;
42  (void)static_cast<int*>(arg); // expected-error {{cannot cast from type 'id' to pointer type 'int *'}}
43  (void)static_cast<id>(arg);
44  (void)static_cast<__autoreleasing id*>(arg); // expected-error{{cannot cast from type 'id' to pointer type '__autoreleasing id *'}}
45  (void)static_cast<id*>(arg); // expected-error {{cannot cast from type 'id' to pointer type '__strong id *'}}
46
47  (void)static_cast<__autoreleasing id**>(voidp_val);
48  (void)static_cast<void*>(voidp_val);
49  (void)static_cast<void**>(arg); // expected-error {{cannot cast from type 'id' to pointer type 'void **'}}
50  (void)static_cast<__strong id**>(0);  
51
52  __strong id *idp;
53  (void)static_cast<__autoreleasing id*>(idp); // expected-error{{static_cast from '__strong id *' to '__autoreleasing id *' is not allowed}}
54  (void)static_cast<__weak id*>(idp); // expected-error{{static_cast from '__strong id *' to '__weak id *' is not allowed}}
55}
56
57void test_const_cast(__strong id *sip, __weak id *wip, 
58                     const __strong id *csip, __weak const id *cwip) {
59  // Cannot use const_cast to cast between ownership qualifications or
60  // add/remove ownership qualifications.
61  (void)const_cast<__strong id *>(wip); // expected-error{{is not allowed}}
62  (void)const_cast<__weak id *>(sip); // expected-error{{is not allowed}}
63
64  // It's acceptable to cast away constness.
65  (void)const_cast<__strong id *>(csip);
66  (void)const_cast<__weak id *>(cwip);
67}
68
69void test_reinterpret_cast(__strong id *sip, __weak id *wip, 
70                           const __strong id *csip, __weak const id *cwip) {
71  // Okay to reinterpret_cast to add/remove/change ownership
72  // qualifications.
73  (void)reinterpret_cast<__strong id *>(wip);
74  (void)reinterpret_cast<__weak id *>(sip);
75
76  // Not allowed to cast away constness
77  (void)reinterpret_cast<__strong id *>(csip); // expected-error{{reinterpret_cast from '__strong id const *' to '__strong id *' casts away qualifiers}}
78  (void)reinterpret_cast<__weak id *>(cwip); // expected-error{{reinterpret_cast from '__weak id const *' to '__weak id *' casts away qualifiers}}
79  (void)reinterpret_cast<__weak id *>(csip); // expected-error{{reinterpret_cast from '__strong id const *' to '__weak id *' casts away qualifiers}}
80  (void)reinterpret_cast<__strong id *>(cwip); // expected-error{{reinterpret_cast from '__weak id const *' to '__strong id *' casts away qualifiers}}
81}
82
83void test_cstyle_cast(__strong id *sip, __weak id *wip, 
84                      const __strong id *csip, __weak const id *cwip) {
85  // C-style casts aren't allowed to change Objective-C ownership
86  // qualifiers (beyond what the normal implicit conversion allows).
87
88  (void)(__strong id *)wip; // expected-error{{C-style cast from '__weak id *' to '__strong id *' casts away qualifiers}}
89  (void)(__strong id *)cwip; // expected-error{{C-style cast from '__weak id const *' to '__strong id *' casts away qualifiers}}
90  (void)(__weak id *)sip; // expected-error{{C-style cast from '__strong id *' to '__weak id *' casts away qualifiers}}
91  (void)(__weak id *)csip; // expected-error{{C-style cast from '__strong id const *' to '__weak id *' casts away qualifiers}}
92
93  (void)(__strong const id *)wip; // expected-error{{C-style cast from '__weak id *' to '__strong id const *' casts away qualifiers}}
94  (void)(__strong const id *)cwip; // expected-error{{C-style cast from '__weak id const *' to '__strong id const *' casts away qualifiers}}
95  (void)(__weak const id *)sip; // expected-error{{C-style cast from '__strong id *' to '__weak id const *' casts away qualifiers}}
96  (void)(__weak const id *)csip; // expected-error{{C-style cast from '__strong id const *' to '__weak id const *' casts away qualifiers}}
97  (void)(__autoreleasing const id *)wip; // expected-error{{C-style cast from '__weak id *' to '__autoreleasing id const *' casts away qualifiers}}
98  (void)(__autoreleasing const id *)cwip; // expected-error{{C-style cast from '__weak id const *' to '__autoreleasing id const *' casts away qualifiers}}
99  (void)(__autoreleasing const id *)sip;
100  (void)(__autoreleasing const id *)csip;
101}
102
103void test_functional_cast(__strong id *sip, __weak id *wip,
104                          __autoreleasing id *aip) {
105  // Functional casts aren't allowed to change Objective-C ownership
106  // qualifiers (beyond what the normal implicit conversion allows).
107
108  typedef __strong id *strong_id_pointer;
109  typedef __weak id *weak_id_pointer;
110  typedef __autoreleasing id *autoreleasing_id_pointer;
111
112  typedef const __strong id *const_strong_id_pointer;
113  typedef const __weak id *const_weak_id_pointer;
114  typedef const __autoreleasing id *const_autoreleasing_id_pointer;
115
116  (void)strong_id_pointer(wip); // expected-error{{functional-style cast from '__weak id *' to 'strong_id_pointer' (aka '__strong id *') casts away qualifiers}}
117  (void)weak_id_pointer(sip); // expected-error{{functional-style cast from '__strong id *' to 'weak_id_pointer' (aka '__weak id *') casts away qualifiers}}
118  (void)autoreleasing_id_pointer(sip); // expected-error{{functional-style cast from '__strong id *' to 'autoreleasing_id_pointer' (aka '__autoreleasing id *') casts away qualifiers}}
119  (void)autoreleasing_id_pointer(wip); // expected-error{{functional-style cast from '__weak id *' to 'autoreleasing_id_pointer' (aka '__autoreleasing id *') casts away qualifiers}}
120  (void)const_strong_id_pointer(wip); // expected-error{{functional-style cast from '__weak id *' to 'const_strong_id_pointer' (aka 'const __strong id *') casts away qualifiers}}
121  (void)const_weak_id_pointer(sip); // expected-error{{functional-style cast from '__strong id *' to 'const_weak_id_pointer' (aka 'const __weak id *') casts away qualifiers}}
122  (void)const_autoreleasing_id_pointer(sip);
123  (void)const_autoreleasing_id_pointer(aip);
124  (void)const_autoreleasing_id_pointer(wip); // expected-error{{functional-style cast from '__weak id *' to 'const_autoreleasing_id_pointer' (aka 'const __autoreleasing id *') casts away qualifiers}}
125}
126
127void test_unsafe_unretained(__strong id *sip, __weak id *wip,
128                            __autoreleasing id *aip,
129                            __unsafe_unretained id *uip,
130                            const __unsafe_unretained id *cuip) {
131  uip = sip; // expected-error{{assigning to '__unsafe_unretained id *' from incompatible type '__strong id *'}}
132  uip = wip; // expected-error{{assigning to '__unsafe_unretained id *' from incompatible type '__weak id *'}}
133  uip = aip; // expected-error{{assigning to '__unsafe_unretained id *' from incompatible type '__autoreleasing id *'}}
134
135  cuip = sip;
136  cuip = wip; // expected-error{{assigning to '__unsafe_unretained id const *' from incompatible type '__weak id *'}}
137  cuip = aip;
138}
139
140void to_void(__strong id *sip, __weak id *wip,
141             __autoreleasing id *aip,
142             __unsafe_unretained id *uip) {
143  void *vp1 = sip;
144  void *vp2 = wip;
145  void *vp3 = aip;
146  void *vp4 = uip;
147  (void)(void*)sip;
148  (void)(void*)wip;
149  (void)(void*)aip;
150  (void)(void*)uip;
151  (void)static_cast<void*>(sip);
152  (void)static_cast<void*>(wip);
153  (void)static_cast<void*>(aip);
154  (void)static_cast<void*>(uip);
155  (void)reinterpret_cast<void*>(sip);
156  (void)reinterpret_cast<void*>(wip);
157  (void)reinterpret_cast<void*>(aip);
158  (void)reinterpret_cast<void*>(uip);
159
160  (void)(void*)&sip;
161  (void)(void*)&wip;
162  (void)(void*)&aip;
163  (void)(void*)&uip;
164  (void)static_cast<void*>(&sip);
165  (void)static_cast<void*>(&wip);
166  (void)static_cast<void*>(&aip);
167  (void)static_cast<void*>(&uip);
168  (void)reinterpret_cast<void*>(&sip);
169  (void)reinterpret_cast<void*>(&wip);
170  (void)reinterpret_cast<void*>(&aip);
171  (void)reinterpret_cast<void*>(&uip);
172}
173
174void from_void(void *vp) {
175  __strong id *sip = (__strong id *)vp;
176  __weak id *wip = (__weak id *)vp;
177  __autoreleasing id *aip = (__autoreleasing id *)vp;
178  __unsafe_unretained id *uip = (__unsafe_unretained id *)vp;
179  __strong id *sip2 = static_cast<__strong id *>(vp);
180  __weak id *wip2 = static_cast<__weak id *>(vp);
181  __autoreleasing id *aip2 = static_cast<__autoreleasing id *>(vp);
182  __unsafe_unretained id *uip2 = static_cast<__unsafe_unretained id *>(vp);
183  __strong id *sip3 = reinterpret_cast<__strong id *>(vp);
184  __weak id *wip3 = reinterpret_cast<__weak id *>(vp);
185  __autoreleasing id *aip3 = reinterpret_cast<__autoreleasing id *>(vp);
186  __unsafe_unretained id *uip3 = reinterpret_cast<__unsafe_unretained id *>(vp);
187
188  __strong id **sipp = (__strong id **)vp;
189  __weak id **wipp = (__weak id **)vp;
190  __autoreleasing id **aipp = (__autoreleasing id **)vp;
191  __unsafe_unretained id **uipp = (__unsafe_unretained id **)vp;
192
193  sip = vp; // expected-error{{assigning to '__strong id *' from incompatible type 'void *'}}
194  wip = vp; // expected-error{{assigning to '__weak id *' from incompatible type 'void *'}}
195  aip = vp; // expected-error{{assigning to '__autoreleasing id *' from incompatible type 'void *'}}
196  uip = vp; // expected-error{{assigning to '__unsafe_unretained id *' from incompatible type 'void *'}}
197}
198
199typedef void (^Block)();
200typedef void (^Block_strong)() __strong;
201typedef void (^Block_autoreleasing)() __autoreleasing;
202
203@class NSString;
204
205void ownership_transfer_in_cast(void *vp, Block *pblk) {
206  __strong NSString **sip2 = static_cast<NSString **>(static_cast<__strong id *>(vp));
207  __weak NSString **wip2 = static_cast<NSString **>(static_cast<__weak id *>(vp));
208  __autoreleasing id *aip2 = static_cast<id *>(static_cast<__autoreleasing id *>(vp));
209  __unsafe_unretained id *uip2 = static_cast<id *>(static_cast<__unsafe_unretained id *>(vp));
210  __strong id *sip3 = reinterpret_cast<id *>(reinterpret_cast<__strong id *>(vp));
211  __weak id *wip3 = reinterpret_cast<id *>(reinterpret_cast<__weak id *>(vp));
212  __autoreleasing id *aip3 = reinterpret_cast<id *>(reinterpret_cast<__autoreleasing id *>(vp));
213  __unsafe_unretained id *uip3 = reinterpret_cast<id *>(reinterpret_cast<__unsafe_unretained id *>(vp));
214
215  Block_strong blk_strong1;
216  Block_strong blk_strong2 = static_cast<Block>(blk_strong1);
217  Block_autoreleasing *blk_auto = static_cast<Block*>(pblk);
218}
219