1 | // RUN: %clang_cc1 -fsyntax-only -verify -fblocks -Wno-objc-root-class %s |
2 | // test for block type safety. |
3 | |
4 | @interface Super @end |
5 | @interface Sub : Super @end |
6 | |
7 | void f2(void(^f)(Super *)) { // expected-note{{passing argument to parameter 'f' here}} |
8 | Super *o; |
9 | f(o); |
10 | } |
11 | |
12 | void f3(void(^f)(Sub *)) { |
13 | Sub *o; |
14 | f(o); |
15 | } |
16 | |
17 | void r0(Super* (^f)()) { |
18 | Super *o = f(); |
19 | } |
20 | |
21 | void r1(Sub* (^f)()) { // expected-note{{passing argument to parameter 'f' here}} |
22 | Sub *o = f(); |
23 | } |
24 | |
25 | @protocol NSObject; |
26 | @class NSObject; |
27 | |
28 | void r2 (id<NSObject> (^f) (void)) { |
29 | id o = f(); |
30 | } |
31 | |
32 | void test1() { |
33 | f2(^(Sub *o) { }); // expected-error {{incompatible block pointer types passing}} |
34 | f3(^(Super *o) { }); // OK, block taking Super* may be called with a Sub* |
35 | |
36 | r0(^Super* () { return 0; }); // OK |
37 | r0(^Sub* () { return 0; }); // OK, variable of type Super* gets return value of type Sub* |
38 | r0(^id () { return 0; }); |
39 | |
40 | r1(^Super* () { return 0; }); // expected-error {{incompatible block pointer types passing}} |
41 | r1(^Sub* () { return 0; }); // OK |
42 | r1(^id () { return 0; }); |
43 | |
44 | r2(^id<NSObject>() { return 0; }); |
45 | } |
46 | |
47 | |
48 | @interface A @end |
49 | @interface B @end |
50 | |
51 | void f0(void (^f)(A* x)) { |
52 | A* a; |
53 | f(a); |
54 | } |
55 | |
56 | void f1(void (^f)(id x)) { |
57 | B *b; |
58 | f(b); |
59 | } |
60 | |
61 | void test2(void) |
62 | { |
63 | f0(^(id a) { }); // OK |
64 | f1(^(A* a) { }); |
65 | f1(^(id<NSObject> a) { }); // OK |
66 | } |
67 | |
68 | @interface NSArray |
69 | // Calls block() with every object in the array |
70 | -enumerateObjectsWithBlock:(void (^)(id obj))block; |
71 | @end |
72 | |
73 | @interface MyThing |
74 | -(void) printThing; |
75 | @end |
76 | |
77 | @implementation MyThing |
78 | static NSArray* myThings; // array of MyThing* |
79 | |
80 | -(void) printThing { } |
81 | |
82 | // programmer wants to write this: |
83 | -printMyThings1 { |
84 | [myThings enumerateObjectsWithBlock: ^(MyThing *obj) { |
85 | [obj printThing]; |
86 | }]; |
87 | } |
88 | |
89 | // strict type safety requires this: |
90 | -printMyThings { |
91 | [myThings enumerateObjectsWithBlock: ^(id obj) { |
92 | MyThing *obj2 = (MyThing *)obj; |
93 | [obj2 printThing]; |
94 | }]; |
95 | } |
96 | @end |
97 | |
98 | @protocol P, P2; |
99 | void f4(void (^f)(id<P> x)) { // expected-note{{passing argument to parameter 'f' here}} |
100 | NSArray<P2> *b; |
101 | f(b); // expected-warning {{passing 'NSArray<P2> *' to parameter of incompatible type 'id<P>'}} |
102 | } |
103 | |
104 | void test3() { |
105 | f4(^(NSArray<P2>* a) { }); // expected-error {{incompatible block pointer types passing 'void (^)(NSArray<P2> *)' to parameter of type 'void (^)(id<P>)'}} |
106 | } |
107 | |
108 | // rdar : //8302845 |
109 | @protocol Foo @end |
110 | |
111 | @interface Baz @end |
112 | |
113 | @interface Baz(FooConformance) <Foo> |
114 | @end |
115 | |
116 | @implementation Baz @end |
117 | |
118 | int test4 () { |
119 | id <Foo> (^b)() = ^{ // Doesn't work |
120 | return (Baz *)0; |
121 | }; |
122 | return 0; |
123 | } |
124 | |
125 | // rdar:// 9118343 |
126 | |
127 | @protocol NSCopying @end |
128 | |
129 | @interface NSAllArray <NSCopying> |
130 | @end |
131 | |
132 | @interface NSAllArray (FooConformance) <Foo> |
133 | @end |
134 | |
135 | int test5() { |
136 | NSAllArray *(^block)(id); |
137 | id <Foo> (^genericBlock)(id); |
138 | genericBlock = block; |
139 | return 0; |
140 | } |
141 | |
142 | // rdar://10798770 |
143 | typedef int NSInteger; |
144 | |
145 | typedef enum : NSInteger {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending} NSComparisonResult; |
146 | |
147 | typedef NSComparisonResult (^NSComparator)(id obj1, id obj2); |
148 | |
149 | @interface radar10798770 |
150 | - (void)sortUsingComparator:(NSComparator)c; |
151 | @end |
152 | |
153 | void f() { |
154 | radar10798770 *f; |
155 | [f sortUsingComparator:^(id a, id b) { |
156 | return NSOrderedSame; |
157 | }]; |
158 | } |
159 | |
160 | // rdar://16739120 |
161 | @protocol P1 @end |
162 | @protocol P2 @end |
163 | |
164 | void Test() { |
165 | void (^aBlock)(); |
166 | id anId = aBlock; // OK |
167 | |
168 | id<P1,P2> anQualId = aBlock; // expected-error {{initializing 'id<P1,P2>' with an expression of incompatible type 'void (^)()'}} |
169 | |
170 | NSArray* anArray = aBlock; // expected-error {{initializing 'NSArray *' with an expression of incompatible type 'void (^)()'}} |
171 | |
172 | aBlock = anId; // OK |
173 | |
174 | id<P1,P2> anQualId1; |
175 | aBlock = anQualId1; // expected-error {{assigning to 'void (^)()' from incompatible type 'id<P1,P2>'}} |
176 | |
177 | NSArray* anArray1; |
178 | aBlock = anArray1; // expected-error {{assigning to 'void (^)()' from incompatible type 'NSArray *'}} |
179 | } |
180 | |
181 | void Test2() { |
182 | void (^aBlock)(); |
183 | id<NSObject> anQualId1 = aBlock; // Ok |
184 | id<NSObject, NSCopying> anQualId2 = aBlock; // Ok |
185 | id<NSObject, NSCopying, NSObject, NSCopying> anQualId3 = aBlock; // Ok |
186 | id <P1> anQualId4 = aBlock; // expected-error {{initializing 'id<P1>' with an expression of incompatible type 'void (^)()'}} |
187 | id<NSObject, P1, NSCopying> anQualId5 = aBlock; // expected-error {{initializing 'id<NSObject,P1,NSCopying>' with an expression of incompatible type 'void (^)()'}} |
188 | id<NSCopying> anQualId6 = aBlock; // Ok |
189 | } |
190 | |
191 | void Test3() { |
192 | void (^aBlock)(); |
193 | NSObject *NSO = aBlock; // Ok |
194 | NSObject<NSObject> *NSO1 = aBlock; // Ok |
195 | NSObject<NSObject, NSCopying> *NSO2 = aBlock; // Ok |
196 | NSObject<NSObject, NSCopying, NSObject, NSCopying> *NSO3 = aBlock; // Ok |
197 | NSObject <P1> *NSO4 = aBlock; // expected-error {{initializing 'NSObject<P1> *' with an expression of incompatible type 'void (^)()'}} |
198 | NSObject<NSObject, P1, NSCopying> *NSO5 = aBlock; // expected-error {{initializing 'NSObject<NSObject,P1,NSCopying> *' with an expression of incompatible type 'void (^)()'}} |
199 | NSObject<NSCopying> *NSO6 = aBlock; // Ok |
200 | } |
201 | |
202 | // rdar://problem/19420731 |
203 | typedef NSObject<P1> NSObject_P1; |
204 | typedef NSObject_P1<P2> NSObject_P1_P2; |
205 | |
206 | void Test4(void (^handler)(NSObject_P1_P2 *p)) { |
207 | Test4(^(NSObject<P2> *p) { }); |
208 | } |
209 | |