Debian Codegraph @

clang/test/SemaObjCXX/related-result-type-inference.mm

raw
1 // RUN: %clang_cc1 -verify %s
2
3 @interface Unrelated
4 @end
5
6 @interface NSObject
7 + (id)new;
8 + (id)alloc;
9 - (NSObject *)init;
10
11 - (id)retain; // expected-note 2{{instance method 'retain' is assumed to return an instance of its receiver type ('NSArray *')}}
12 - autorelease;
13
14 - (id)self;
15
16 - (id)copy;
17 - (id)mutableCopy;
18
19 // Do not infer when instance/class mismatches
20 - (id)newNotInferred;
21 - (id)alloc;
22 + (id)initWithBlarg;
23 + (id)self;
24
25 // Do not infer when the return types mismatch.
26 - (Unrelated *)initAsUnrelated;
27 @end
28
29 @interface NSString : NSObject
30 - (id)init;
31 - (id)initWithCString:(const char*)string;
32 @end
33
34 @interface NSArray : NSObject
35 - (unsigned)count;
36 @end
37
38 @interface NSBlah
39 @end
40
41 @interface NSMutableArray : NSArray
42 @end
43
44 @interface NSBlah ()
45 + (Unrelated *)newUnrelated;
46 @end
47
48 void test_inference() {
49 // Inference based on method family
50 __typeof__(([[NSString alloc] init])) *str = (NSString**)0;
51 __typeof__(([[[[NSString new] self] retain] autorelease])) *str2 = (NSString **)0;
52 __typeof__(([[NSString alloc] initWithCString:"blah"])) *str3 = (NSString**)0;
53
54 // Not inferred
55 __typeof__(([[NSString new] copy])) *id1 = (id*)0;
56
57 // Not inferred due to instance/class mismatches
58 __typeof__(([[NSString new] newNotInferred])) *id2 = (id*)0;
59 __typeof__(([[NSString new] alloc])) *id3 = (id*)0;
60 __typeof__(([NSString self])) *id4 = (id*)0;
61 __typeof__(([NSString initWithBlarg])) *id5 = (id*)0;
62
63 // Not inferred due to return type mismatch
64 __typeof__(([[NSString alloc] initAsUnrelated])) *unrelated = (Unrelated**)0;
65 __typeof__(([NSBlah newUnrelated])) *unrelated2 = (Unrelated**)0;
66
67 NSArray *arr = [[NSMutableArray alloc] init];
68 NSMutableArray *marr = [arr retain]; // expected-error{{incompatible pointer types initializing 'NSMutableArray *' with an expression of type 'NSArray *'}}
69 marr = [arr retain]; // expected-error{{incompatible pointer types assigning to 'NSMutableArray *' from 'NSArray *'}}
70 arr = [marr retain];
71 }