1 // RUN: %clang_cc1 -fsyntax-only -verify -fblocks -Wno-objc-root-class %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -fblocks -Wno-objc-root-class \
3 // RUN: -fcompatibility-qualified-id-block-type-checking -DCOMPATIBILITY_QUALIFIED_ID_TYPE_CHECKING=1 %s
4 // test for block type safety.
7 @interface Sub : Super @end
9 void f2(void(^f)(Super *)) { // expected-note{{passing argument to parameter 'f' here}}
14 void f3(void(^f)(Sub *)) {
19 void r0(Super* (^f)(void)) {
23 void r1(Sub* (^f)(void)) { // expected-note{{passing argument to parameter 'f' here}}
30 void r2 (id<NSObject> (^f) (void)) {
35 f2(^(Sub *o) { }); // expected-error {{incompatible block pointer types passing}}
36 f3(^(Super *o) { }); // OK, block taking Super* may be called with a Sub*
38 r0(^Super* (void) { return 0; }); // OK
39 r0(^Sub* (void) { return 0; }); // OK, variable of type Super* gets return value of type Sub*
40 r0(^id (void) { return 0; });
42 r1(^Super* (void) { return 0; }); // expected-error {{incompatible block pointer types passing}}
43 r1(^Sub* (void) { return 0; }); // OK
44 r1(^id (void) { return 0; });
46 r2(^id<NSObject>(void) { return 0; });
53 void f0(void (^f)(A* x)) {
58 void f1(void (^f)(id x)) {
65 f0(^(id a) { }); // OK
67 f1(^(id<NSObject> a) { }); // OK
71 // Calls block() with every object in the array
72 -enumerateObjectsWithBlock:(void (^)(id obj))block;
79 @implementation MyThing
80 static NSArray* myThings; // array of MyThing*
82 -(void) printThing { }
84 // programmer wants to write this:
86 [myThings enumerateObjectsWithBlock: ^(MyThing *obj) {
91 // strict type safety requires this:
93 [myThings enumerateObjectsWithBlock: ^(id obj) {
94 MyThing *obj2 = (MyThing *)obj;
101 void f4(void (^f)(id<P> x)) { // expected-note{{passing argument to parameter 'f' here}}
103 f(b); // expected-warning {{passing 'NSArray<P2> *' to parameter of incompatible type 'id<P>'}}
107 f4(^(NSArray<P2>* a) { }); // expected-error {{incompatible block pointer types passing 'void (^)(NSArray<P2> *)' to parameter of type 'void (^)(id<P>)'}}
114 @interface Baz(FooConformance) <Foo>
117 @implementation Baz @end
120 id <Foo> (^b)(void) = ^{ // Doesn't work
126 @protocol NSCopying @end
128 @interface NSAllArray <NSCopying>
131 @interface NSAllArray (FooConformance) <Foo>
134 #ifndef COMPATIBILITY_QUALIFIED_ID_TYPE_CHECKING
136 // Returned value is used outside of a block, so error on changing
137 // a return type to a more general than expected.
138 NSAllArray *(^block)(id);
139 id <Foo> (^genericBlock)(id);
140 genericBlock = block;
141 block = genericBlock; // expected-error {{incompatible block pointer types assigning to 'NSAllArray *(^)(id)' from 'id<Foo> (^)(id)'}}
143 // A parameter is used inside a block, so error on changing a parameter type
144 // to a more specific than an argument type it will be called with.
145 void (^blockWithParam)(NSAllArray *);
146 void (^genericBlockWithParam)(id<Foo>);
147 genericBlockWithParam = blockWithParam; // expected-error {{incompatible block pointer types assigning to 'void (^)(id<Foo>)' from 'void (^)(NSAllArray *)'}}
148 blockWithParam = genericBlockWithParam;
152 // In Apple SDK APIs using NSItemProviderCompletionHandler require to work with
153 // blocks that have parameters more specific than in method signatures. As
154 // explained in non-compatibility test above, it is not safe in general. But
155 // to keep existing code working we support a compatibility mode that uses
156 // previous type checking.
158 NSAllArray *(^block)(id);
159 id <Foo> (^genericBlock)(id);
160 genericBlock = block;
161 block = genericBlock; // expected-error {{incompatible block pointer types assigning to 'NSAllArray *(^)(id)' from 'id<Foo> (^)(id)'}}
163 void (^blockWithParam)(NSAllArray *);
164 void (^genericBlockWithParam)(id<Foo>);
165 genericBlockWithParam = blockWithParam;
166 blockWithParam = genericBlockWithParam;
171 typedef int NSInteger;
173 typedef enum : NSInteger {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending} NSComparisonResult;
175 typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
177 @interface radar10798770
178 - (void)sortUsingComparator:(NSComparator)c;
183 [f sortUsingComparator:^(id a, id b) {
184 return NSOrderedSame;
192 void (^aBlock)(void);
193 id anId = aBlock; // OK
195 id<P1,P2> anQualId = aBlock; // expected-error {{initializing 'id<P1,P2>' with an expression of incompatible type 'void (^)(void)'}}
197 NSArray* anArray = aBlock; // expected-error {{initializing 'NSArray *' with an expression of incompatible type 'void (^)(void)'}}
202 aBlock = anQualId1; // expected-error {{assigning to 'void (^)(void)' from incompatible type 'id<P1,P2>'}}
205 aBlock = anArray1; // expected-error {{assigning to 'void (^)(void)' from incompatible type 'NSArray *'}}
209 void (^aBlock)(void);
210 id<NSObject> anQualId1 = aBlock; // Ok
211 id<NSObject, NSCopying> anQualId2 = aBlock; // Ok
212 id<NSObject, NSCopying, NSObject, NSCopying> anQualId3 = aBlock; // Ok
213 id <P1> anQualId4 = aBlock; // expected-error {{initializing 'id<P1>' with an expression of incompatible type 'void (^)(void)'}}
214 id<NSObject, P1, NSCopying> anQualId5 = aBlock; // expected-error {{initializing 'id<NSObject,P1,NSCopying>' with an expression of incompatible type 'void (^)(void)'}}
215 id<NSCopying> anQualId6 = aBlock; // Ok
219 void (^aBlock)(void);
220 NSObject *NSO = aBlock; // Ok
221 NSObject<NSObject> *NSO1 = aBlock; // Ok
222 NSObject<NSObject, NSCopying> *NSO2 = aBlock; // Ok
223 NSObject<NSObject, NSCopying, NSObject, NSCopying> *NSO3 = aBlock; // Ok
224 NSObject <P1> *NSO4 = aBlock; // expected-error {{initializing 'NSObject<P1> *' with an expression of incompatible type 'void (^)(void)'}}
225 NSObject<NSObject, P1, NSCopying> *NSO5 = aBlock; // expected-error {{initializing 'NSObject<NSObject,P1,NSCopying> *' with an expression of incompatible type 'void (^)(void)'}}
226 NSObject<NSCopying> *NSO6 = aBlock; // Ok
229 typedef NSObject<P1> NSObject_P1;
230 typedef NSObject_P1<P2> NSObject_P1_P2;
232 void Test4(void (^handler)(NSObject_P1_P2 *p)) {
233 Test4(^(NSObject<P2> *p) { });