blob: 4c2ae236ac6c9891275c260850883273394e8e5e [file] [log] [blame]
John McCallf85e1932011-06-15 23:02:42 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-nonfragile-abi -fsyntax-only -fobjc-arc -fblocks -verify %s
2
3typedef unsigned long NSUInteger;
4
5void test0(void (*fn)(int), int val) {
6 fn(val);
7}
8
9@interface A
10- (id)retain;
11- (id)autorelease;
12- (oneway void)release;
13- (void)dealloc;
14- (NSUInteger)retainCount;
15@end
16
17void test1(A *a) {
18 SEL s = @selector(retain); // expected-error {{ARC forbids use of 'retain' in a @selector}}
19 s = @selector(release); // expected-error {{ARC forbids use of 'release' in a @selector}}
20 s = @selector(autorelease); // expected-error {{ARC forbids use of 'autorelease' in a @selector}}
21 s = @selector(dealloc); // expected-error {{ARC forbids use of 'dealloc' in a @selector}}
22 [a dealloc]; // expected-error {{ARC forbids explicit message send of 'dealloc'}}
23 [a retain]; // expected-error {{ARC forbids explicit message send of 'retain'}}
24 [a retainCount]; // expected-error {{ARC forbids explicit message send of 'retainCount'}}
25 [a release]; // expected-error {{ARC forbids explicit message send of 'release'}}
26 [a autorelease]; // expected-error {{ARC forbids explicit message send of 'autorelease'}}
27}
28
29@interface Test2 : A
30- (void) dealloc;
31@end
32@implementation Test2
33- (void) dealloc {
34 // This should maybe just be ignored. We're just going to warn about it for now.
35 [super dealloc]; // expected-error {{ARC forbids explicit message send of 'dealloc'}}
36}
37@end
38
39__weak __strong id x; // expected-error {{the type '__strong id' already has retainment attributes}}
40
41// rdar://8843638
42
43@interface I
44- (id)retain;
45- (id)autorelease;
46- (oneway void)release;
47- (NSUInteger)retainCount;
48@end
49
50@implementation I
51- (id)retain{return 0;} // expected-error {{ARC forbids implementation of 'retain'}}
52- (id)autorelease{return 0;} // expected-error {{ARC forbids implementation of 'autorelease'}}
53- (oneway void)release{} // expected-error {{ARC forbids implementation of 'release'}}
54- (NSUInteger)retainCount{ return 0; } // expected-error {{ARC forbids implementation of 'retainCount'}}
55@end
56
57@implementation I(CAT)
58- (id)retain{return 0;} // expected-error {{ARC forbids implementation of 'retain'}}
59- (id)autorelease{return 0;} // expected-error {{ARC forbids implementation of 'autorelease'}}
60- (oneway void)release{} // expected-error {{ARC forbids implementation of 'release'}}
61- (NSUInteger)retainCount{ return 0; } // expected-error {{ARC forbids implementation of 'retainCount'}}
62@end
63
64// rdar://8861761
65
66@interface B
67-(id)alloc;
68- (id)initWithInt: (int) i;
69@end
70
71void rdar8861761() {
72 B *o1 = [[B alloc] initWithInt:0];
73 B *o2 = [B alloc];
74 [o2 initWithInt:0]; // expected-warning {{expression result unused}}
75}
76
77// rdar://8925835
78@interface rdar8925835
79- (void)foo:(void (^)(unsigned captureCount, I * const capturedStrings[captureCount]))block;
80@end
81
82void test5() {
83 extern void test5_helper(__autoreleasing id *);
84 id x;
85
86 // Okay because of magic temporaries.
87 test5_helper(&x);
88
89 __autoreleasing id *a = &x; // expected-error {{initializing '__autoreleasing id *' with an expression of type '__strong id *' changes retain/release properties of pointer}}
90
91 a = &x; // expected-error {{assigning '__strong id *' to '__autoreleasing id *' changes retain/release properties of pointer}}
92
93 extern void test5_helper2(id const *);
94 test5_helper2(&x);
95
96 extern void test5_helper3(__weak id *); // expected-note {{passing argument to parameter here}}
97 test5_helper3(&x); // expected-error {{passing '__strong id *' to parameter of type '__weak id *' changes retain/release properties of pointer}}
98}
99
100// rdar://problem/8937869
101void test6(unsigned cond) {
102 switch (cond) {
103 case 0:
104 ;
105 id x; // expected-note {{jump bypasses initialization of retaining variable}}
106
107 case 1: // expected-error {{switch case is in protected scope}}
108 break;
109 }
110}
111
112@class NSError;
113void test7(void) {
114 extern void test7_helper(NSError **);
115 NSError *err;
116 test7_helper(&err);
117}
118void test7_weak(void) {
119 extern void test7_helper(NSError **);
120 __weak NSError *err;
121 test7_helper(&err);
122}
123void test7_unsafe(void) {
124 extern void test7_helper(NSError **); // expected-note {{passing argument to parameter here}}
125 __unsafe_unretained NSError *err;
126 test7_helper(&err); // expected-error {{passing 'NSError *__unsafe_unretained *' to parameter of type 'NSError *__autoreleasing *' changes retain/release properties of pointer}}
127}
128
129@class Test8_incomplete;
130@interface Test8_complete @end;
131@interface Test8_super @end;
132@interface Test8 : Test8_super
133- (id) init00;
134- (id) init01; // expected-note {{declaration in interface}} \
135 // expected-note{{overridden method}}
136- (id) init02; // expected-note{{overridden method}}
137- (id) init03; // covariance
138- (id) init04; // covariance
139- (id) init05; // expected-note{{overridden method}}
140
141- (void) init10; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
142- (void) init11;
143- (void) init12;
144- (void) init13; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
145- (void) init14; // expected-note {{declaration in interface is not in the 'init' family because its result type is not an object pointer}}
146- (void) init15;
147
148// These should be invalid to actually call.
149- (Test8_incomplete*) init20;
150- (Test8_incomplete*) init21; // expected-note {{declaration in interface}}
151- (Test8_incomplete*) init22;
152- (Test8_incomplete*) init23;
153- (Test8_incomplete*) init24;
154- (Test8_incomplete*) init25;
155
156- (Test8_super*) init30; // id exception to covariance
157- (Test8_super*) init31; // expected-note {{declaration in interface}} \
158 // expected-note{{overridden method}}
159- (Test8_super*) init32; // expected-note{{overridden method}}
160- (Test8_super*) init33;
161- (Test8_super*) init34; // covariance
162- (Test8_super*) init35; // expected-note{{overridden method}}
163
164- (Test8*) init40; // id exception to covariance
165- (Test8*) init41; // expected-note {{declaration in interface}} \
166 // expected-note{{overridden method}}
167- (Test8*) init42; // expected-note{{overridden method}}
168- (Test8*) init43; // this should be a warning, but that's a general language thing, not an ARC thing
169- (Test8*) init44;
170- (Test8*) init45; // expected-note{{overridden method}}
171
172- (Test8_complete*) init50; // expected-error {{init methods must return a type related to the receiver type}}
173- (Test8_complete*) init51; // expected-error {{init methods must return a type related to the receiver type}}
174- (Test8_complete*) init52; // expected-error {{init methods must return a type related to the receiver type}}
175- (Test8_complete*) init53; // expected-error {{init methods must return a type related to the receiver type}}
176- (Test8_complete*) init54; // expected-error {{init methods must return a type related to the receiver type}}
177- (Test8_complete*) init55; // expected-error {{init methods must return a type related to the receiver type}}
178@end
179@implementation Test8
180- (id) init00 { return 0; }
181- (id) init10 { return 0; } // expected-error {{method implementation does not match its declaration}}
182- (id) init20 { return 0; }
183- (id) init30 { return 0; }
184- (id) init40 { return 0; }
185- (id) init50 { return 0; }
186
187- (void) init01 {} // expected-error {{method was declared as an 'init' method, but its implementation doesn't match because its result type is not an object pointer}} \
188 // expected-warning{{ method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
189- (void) init11 {}
190- (void) init21 {} // expected-error {{method was declared as an 'init' method, but its implementation doesn't match because its result type is not an object pointer}}
191- (void) init31 {} // expected-error {{method was declared as an 'init' method, but its implementation doesn't match because its result type is not an object pointer}} \
192 // expected-warning{{ method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
193- (void) init41 {} // expected-error {{method was declared as an 'init' method, but its implementation doesn't match because its result type is not an object pointer}} \
194 // expected-warning{{ method is expected to return an instance of its class type 'Test8', but is declared to return 'void'}}
195- (void) init51 {}
196
197- (Test8_incomplete*) init02 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
198 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
199- (Test8_incomplete*) init12 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
200- (Test8_incomplete*) init22 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
201- (Test8_incomplete*) init32 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
202 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
203- (Test8_incomplete*) init42 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
204 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_incomplete *'}}
205- (Test8_incomplete*) init52 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
206
207- (Test8_super*) init03 { return 0; }
208- (Test8_super*) init13 { return 0; } // expected-error {{method implementation does not match its declaration}}
209- (Test8_super*) init23 { return 0; }
210- (Test8_super*) init33 { return 0; }
211- (Test8_super*) init43 { return 0; }
212- (Test8_super*) init53 { return 0; }
213
214- (Test8*) init04 { return 0; }
215- (Test8*) init14 { return 0; } // expected-error {{method implementation does not match its declaration}}
216- (Test8*) init24 { return 0; }
217- (Test8*) init34 { return 0; }
218- (Test8*) init44 { return 0; }
219- (Test8*) init54 { return 0; }
220
221- (Test8_complete*) init05 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
222 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
223- (Test8_complete*) init15 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
224- (Test8_complete*) init25 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
225- (Test8_complete*) init35 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
226 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
227- (Test8_complete*) init45 { return 0; } // expected-error {{init methods must return a type related to the receiver type}} \
228 // expected-warning{{method is expected to return an instance of its class type 'Test8', but is declared to return 'Test8_complete *'}}
229- (Test8_complete*) init55 { return 0; } // expected-error {{init methods must return a type related to the receiver type}}
230@end
231
232@class Test9_incomplete;
233@interface Test9
234- (Test9_incomplete*) init1; // expected-error {{init methods must return a type related to the receiver type}}
235- (Test9_incomplete*) init2;
236@end
237id test9(Test9 *v) {
238 return [v init1];
239}
240
241// Test that the inference rules are different for fast enumeration variables.
242void test10(id collection) {
243 for (id x in collection) {
244 __strong id *ptr = &x; // expected-error {{initializing '__strong id *' with an expression of type 'const __unsafe_unretained id *' changes retain/release properties of pointer}}
245 }
246
247 for (__strong id x in collection) {
248 __weak id *ptr = &x; // expected-error {{initializing '__weak id *' with an expression of type '__strong id *' changes retain/release properties of pointer}}
249 }
250}
251
252// rdar://problem/9078626
253#define nil ((void*) 0)
254void test11(id op, void *vp) {
255 _Bool b;
256 b = (op == nil);
257 b = (nil == op);
258
259 b = (vp == nil);
260 b = (nil == vp);
261
262 b = (vp == op); // expected-error {{implicit conversion of an Objective-C pointer to 'void *'}}
263 b = (op == vp); // expected-error {{implicit conversion of a non-Objective-C pointer type 'void *' to 'id'}}
264}
265
266void test12(id collection) {
267 for (id x in collection) {
268 x = 0; // expected-error {{fast enumeration variables can't be modified in ARC by default; declare the variable __strong to allow this}}
269 }
270
271 for (const id x in collection) {
272 x = 0; // expected-error {{read-only variable is not assignable}}
273 }
274
275 for (__strong id x in collection) {
276 x = 0;
277 }
278}
279
280@interface Test13
281- (id) init0;
282- (void) noninit;
283@end
284@implementation Test13
285- (id) init0 {
286 self = 0;
287}
288- (void) noninit {
289 self = 0; // expected-error {{cannot assign to 'self' outside of a method in the init family}}
290}
291@end
292
293// rdar://problem/9172151
294@class Test14A, Test14B;
295void test14() {
296 extern void test14_consume(id *);
297 extern int test14_cond(void);
298 extern float test14_nowriteback(id __autoreleasing const *); // expected-note{{passing argument to parameter here}}
299
300 Test14A *a;
301 Test14B *b;
302 id i;
303 id cla[10];
304 id vla[test14_cond() + 10];
305
306 test14_consume((__strong id*) &a);
307 test14_consume((test14_cond() ? (__strong id*) &b : &i));
308 test14_consume(test14_cond() ? 0 : &a);
309 test14_consume(test14_cond() ? (void*) 0 : (&a));
310 test14_consume(cla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
311 test14_consume(vla); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
312 test14_consume(&cla[5]); // expected-error {{passing address of non-scalar object to __autoreleasing parameter for write-back}}
313
314 __strong id *test14_indirect(void);
315 test14_consume(test14_indirect()); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
316
317 extern id test14_global;
318 test14_consume(&test14_global); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
319
320 extern __strong id *test14_global_ptr;
321 test14_consume(test14_global_ptr); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
322
323 static id static_local;
324 test14_consume(&static_local); // expected-error {{passing address of non-local object to __autoreleasing parameter for write-back}}
325
326 __weak id* wip;
327 test14_nowriteback(&static_local); // okay, not a write-back.
328 test14_nowriteback(wip); // expected-error{{passing '__weak id *' to parameter of type '__autoreleasing id const *' changes retain/release properties of pointer}}
329}
330
331void test15() {
332 __block __autoreleasing id x; // expected-error {{__block variables cannot have __autoreleasing lifetime}}
333}
334
335struct Test16;
336@interface Test16a
337- (void) test16_0: (int) x;
338- (int) test16_1: (int) x; // expected-note {{one possibility}}
339- (int) test16_2: (int) x; // expected-note {{one possibility}}
340- (id) test16_3: (int) x __attribute__((ns_returns_retained)); // expected-note {{one possibility}}
341- (void) test16_4: (int) x __attribute__((ns_consumes_self)); // expected-note {{one possibility}}
342- (void) test16_5: (id) __attribute__((ns_consumed)) x; // expected-note {{one possibility}}
343- (void) test16_6: (id) x;
344@end
345
346@interface Test16b
347- (void) test16_0: (int) x;
348- (int) test16_1: (char*) x; // expected-note {{also found}}
349- (char*) test16_2: (int) x; // expected-note {{also found}}
350- (id) test16_3: (int) x; // expected-note {{also found}}
351- (void) test16_4: (int) x; // expected-note {{also found}}
352- (void) test16_5: (id) x; // expected-note {{also found}}
353- (void) test16_6: (struct Test16 *) x;
354@end
355
356void test16(void) {
357 id v;
358 [v test16_0: 0];
359 [v test16_1: 0]; // expected-error {{multiple methods named 'test16_1:' found with mismatched result, parameter type or attributes}}
360 [v test16_2: 0]; // expected-error {{multiple methods named}}
361 [v test16_3: 0]; // expected-error {{multiple methods named}}
362 [v test16_4: 0]; // expected-error {{multiple methods named}}
363 [v test16_5: 0]; // expected-error {{multiple methods named}}
364 [v test16_6: 0];
365}
366
367@class Test17;
368@protocol Test17p
369- (void) test17;
370+ (void) test17;
371@end
372void test17(void) {
373 Test17 *v0;
374 [v0 test17]; // expected-error {{receiver type 'Test17' for instance message is a forward declaration}}
375
376 Test17<Test17p> *v1;
377 [v1 test17]; // expected-error {{receiver type 'Test17<Test17p>' for instance message is a forward declaration}}
378
379 [Test17 test17]; // expected-error {{receiver 'Test17' for class message is a forward declaration}}
380}
381
382void test18(void) {
383 id x;
384 [x test18]; // expected-error {{no known instance method for selector 'test18'}}
385}
386
387extern struct Test19 *test19a;
388struct Test19 *const test19b = 0;
389void test19(void) {
390 id x;
391 x = (id) test19a; // expected-error {{bridged cast}} \
392 // expected-note{{use __bridge to convert directly (no change in ownership))}} \
393 // expected-note{{use __bridge_transfer to transfer ownership of a +1 'struct Test19 *' into ARC}}
394 x = (id) test19b; // expected-error {{bridged cast}} \
395 // expected-note{{use __bridge to convert directly (no change in ownership)}} \
396 // expected-note{{use __bridge_transfer to transfer ownership of a +1 'struct Test19 *' into ARC}}
397}
398
399// rdar://problem/8951453
400static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial lifetime: type is '__strong id'}}
401static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial lifetime: type is '__strong id'}}
402static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial lifetime: type is '__weak id'}}
403static __thread __autoreleasing id test20_autoreleasing; // expected-error {{thread-local variable has non-trivial lifetime: type is '__autoreleasing id'}} expected-error {{global variables cannot have __autoreleasing lifetime}}
404static __thread __unsafe_unretained id test20_unsafe;
405void test20(void) {
406 static __thread id test20_implicit; // expected-error {{thread-local variable has non-trivial lifetime: type is '__strong id'}}
407 static __thread __strong id test20_strong; // expected-error {{thread-local variable has non-trivial lifetime: type is '__strong id'}}
408 static __thread __weak id test20_weak; // expected-error {{thread-local variable has non-trivial lifetime: type is '__weak id'}}
409 static __thread __autoreleasing id test20_autoreleasing; // expected-error {{thread-local variable has non-trivial lifetime: type is '__autoreleasing id'}} expected-error {{global variables cannot have __autoreleasing lifetime}}
410 static __thread __unsafe_unretained id test20_unsafe;
411}
412
413// rdar://9310049
414_Bool fn(id obj) {
415 return (_Bool)obj;
416}
417
418// Check casting w/ lifetime qualifiers.
419void test21() {
420 __strong id *sip;
421 (void)(__weak id *)sip; // expected-error{{casting '__strong id *' to type '__weak id *' changes retain/release properties of pointer}}
422 (void)(__weak const id *)sip; // expected-error{{casting '__strong id *' to type '__weak id const *' changes retain/release properties of pointer}}
423 (void)(__autoreleasing id *)sip; // expected-error{{casting '__strong id *' to type '__autoreleasing id *' changes retain/release properties of pointer}}
424 (void)(__autoreleasing const id *)sip; // okay
425}
426
427// rdar://problem/9340462
428void test22(id x[]) { // expected-error {{must explicitly describe intended lifetime of an object array parameter}}
429}
430
431// rdar://problem/9400219
432void test23(void) {
433 void *ptr;
434 ptr = @"foo";
435 ptr = (ptr ? @"foo" : 0);
436 ptr = (ptr ? @"foo" : @"bar");
437}
438
439id test24(void) {
440 extern void test24_helper(void);
441 return test24_helper(), (void*) 0;
442}
443
444// rdar://9400841
445@interface Base
446@property (assign) id content;
447@end
448
449@interface Foo : Base
450-(void)test;
451@end
452
453@implementation Foo
454-(void)test {
455 super.content = 0;
456}
457@end
458
459// <rdar://problem/9398437>
460void test25(Class *classes) {
461 Class *other_classes;
462 test25(other_classes);
463}
464
465void test26(id y) {
466 extern id test26_var1;
467 __sync_swap(&test26_var1, 0, y); // expected-error {{cannot perform atomic operation on a pointer to type '__strong id': type has non-trivial lifetime}}
468
469 extern __unsafe_unretained id test26_var2;
470 __sync_swap(&test26_var2, 0, y);
471}
472
473@interface Test26
474- (id) init;
475- (id) initWithInt: (int) x;
476@end
477@implementation Test26
478- (id) init { return self; }
479- (id) initWithInt: (int) x {
480 [self init]; // expected-error {{the result of a delegate init call must be immediately returned or assigned to 'self'}}
481 return self;
482}
483@end
484
485// rdar://9525555
486@interface Test27
487@property id x; // expected-error {{ARC forbids properties of Objective-C objects with unspecified storage attribute}}
488@property int y;
489@end
490
491// rdar://9569264
492@interface Test28
493@property (nonatomic, assign) __strong id a; // expected-error {{unsafe_unretained property 'a' may not also be declared __strong}}
494@end
495
496@interface Test28 ()
497@property (nonatomic, assign) __strong id b; // expected-error {{unsafe_unretained property 'b' may not also be declared __strong}}
498@end
499
500@implementation Test28
501@synthesize a;
502@synthesize b;
503@end
504
505// rdar://9573962
506typedef struct Bark Bark;
507@interface Test29
508@property Bark* P;
509@end
510
511@implementation Test29
512@synthesize P;
513- (id)Meth {
514 Bark** f = &P;
515 return 0;
516}
517@end
518
519// rdar://9495837
520@interface Test30
521- (id) new;
522- (void)Meth;
523@end
524
525@implementation Test30
526- (id) new { return 0; }
527- (void) Meth {
528 __weak id x = [Test30 new]; // expected-warning {{cannot assign retained object to weak variable}}
529 id __unsafe_unretained u = [Test30 new]; // expected-warning {{cannot assign retained object to unsafe_unretained variable}}
530 id y = [Test30 new];
531 x = [Test30 new]; // expected-warning {{cannot assign retained object to weak variable}}
532 u = [Test30 new]; // expected-warning {{cannot assign retained object to unsafe_unretained variable}}
533 y = [Test30 new];
534}
535@end
536
537// rdar://9411838
538@protocol PTest31 @end
539
540int Test31() {
541 Class cls;
542 id ids;
543 id<PTest31> pids;
544 Class<PTest31> pcls;
545
546 int i = (ids->isa ? 1 : 0); // expected-error {{member reference base type 'id' is not a structure or union}}
547 int j = (pids->isa ? 1 : 0); // expected-error {{member reference base type 'id<PTest31>' is not a structure or union}}
548 int k = (pcls->isa ? i : j); // expected-error {{member reference base type 'Class<PTest31>' is not a structure or union}}
549 return cls->isa ? i : j; // expected-error {{member reference base type 'Class' is not a structure or union}}
550}
Fariborz Jahanianb1f7d242011-06-16 17:29:56 +0000551
552// rdar://9612030
553@interface ITest32 {
554@public
555 id ivar;
556}
557@end
558
559id Test32(__weak ITest32 *x) {
560 __weak ITest32 *y;
561 x->ivar = 0; // expected-error {{dereferencing a __weak pointer is not allowed}}
562 return y ? y->ivar // expected-error {{dereferencing a __weak pointer is not allowed}}
563 : (*x).ivar; // expected-error {{dereferencing a __weak pointer is not allowed}}
564}
565