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.
5
6@interface Super  @end
7@interface Sub : Super @end
8
9void f2(void(^f)(Super *)) { // expected-note{{passing argument to parameter 'f' here}}
10    Super *o;
11    f(o);
12}
13
14void f3(void(^f)(Sub *)) {
15    Sub *o;
16    f(o);
17}
18
19void r0(Super* (^f)()) {
20     Super *o = f();
21}
22
23void r1(Sub* (^f)()) { // expected-note{{passing argument to parameter 'f' here}}
24    Sub *o = f();
25}
26
27@protocol NSObject;
28@class NSObject;
29
30void r2 (id<NSObject> (^f) (void)) {
31  id o = f();
32}
33
34void test1() {
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*
37
38    r0(^Super* () { return 0; });  // OK
39    r0(^Sub* () { return 0; });    // OK, variable of type Super* gets return value of type Sub*
40    r0(^id () { return 0; });
41
42    r1(^Super* () { return 0; });  // expected-error {{incompatible block pointer types passing}}
43    r1(^Sub* () { return 0; });    // OK
44    r1(^id () { return 0; });
45
46    r2(^id<NSObject>() { return 0; });
47}
48
49
50@interface A @end
51@interface B @end
52
53void f0(void (^f)(A* x)) {
54  A* a;
55  f(a);
56}
57
58void f1(void (^f)(id x)) {
59  B *b;
60  f(b);
61}
62
63void test2(void)
64{
65  f0(^(id a) { }); // OK
66  f1(^(A* a) { });
67   f1(^(id<NSObject> a) { });	// OK
68}
69
70@interface NSArray
71   // Calls block() with every object in the array
72   -enumerateObjectsWithBlock:(void (^)(id obj))block;
73@end
74
75@interface MyThing
76-(void) printThing;
77@end
78
79@implementation MyThing
80    static NSArray* myThings;  // array of MyThing*
81
82   -(void) printThing {  }
83
84// programmer wants to write this:
85   -printMyThings1 {
86       [myThings enumerateObjectsWithBlock: ^(MyThing *obj) {
87           [obj printThing];
88       }];
89   }
90
91// strict type safety requires this:
92   -printMyThings {
93       [myThings enumerateObjectsWithBlock: ^(id obj) {
94           MyThing *obj2 = (MyThing *)obj;
95           [obj2 printThing];
96       }];
97   }
98@end
99
100@protocol P, P2;
101void f4(void (^f)(id<P> x)) { // expected-note{{passing argument to parameter 'f' here}}
102    NSArray<P2> *b;
103    f(b);	// expected-warning {{passing 'NSArray<P2> *' to parameter of incompatible type 'id<P>'}}
104}
105
106void test3() {
107  f4(^(NSArray<P2>* a) { });  // expected-error {{incompatible block pointer types passing 'void (^)(NSArray<P2> *)' to parameter of type 'void (^)(id<P>)'}}
108}
109
110// rdar : //8302845
111@protocol Foo @end
112
113@interface Baz @end
114
115@interface Baz(FooConformance) <Foo>
116@end
117
118@implementation Baz @end
119
120int test4 () {
121    id <Foo> (^b)() = ^{ // Doesn't work
122        return (Baz *)0;
123    };
124    return 0;
125}
126
127// rdar:// 9118343
128
129@protocol NSCopying @end
130
131@interface NSAllArray <NSCopying>
132@end
133
134@interface NSAllArray (FooConformance) <Foo>
135@end
136
137#ifndef COMPATIBILITY_QUALIFIED_ID_TYPE_CHECKING
138int test5() {
139    // Returned value is used outside of a block, so error on changing
140    // a return type to a more general than expected.
141    NSAllArray *(^block)(id);
142    id <Foo> (^genericBlock)(id);
143    genericBlock = block;
144    block = genericBlock; // expected-error {{incompatible block pointer types assigning to 'NSAllArray *(^)(id)' from 'id<Foo> (^)(id)'}}
145
146    // A parameter is used inside a block, so error on changing a parameter type
147    // to a more specific than an argument type it will be called with.
148    // rdar://problem/52788423
149    void (^blockWithParam)(NSAllArray *);
150    void (^genericBlockWithParam)(id<Foo>);
151    genericBlockWithParam = blockWithParam; // expected-error {{incompatible block pointer types assigning to 'void (^)(id<Foo>)' from 'void (^)(NSAllArray *)'}}
152    blockWithParam = genericBlockWithParam;
153    return 0;
154}
155#else
156// In Apple SDK APIs using NSItemProviderCompletionHandler require to work with
157// blocks that have parameters more specific than in method signatures. As
158// explained in non-compatibility test above, it is not safe in general. But
159// to keep existing code working we support a compatibility mode that uses
160// previous type checking.
161int test5() {
162    NSAllArray *(^block)(id);
163    id <Foo> (^genericBlock)(id);
164    genericBlock = block;
165    block = genericBlock; // expected-error {{incompatible block pointer types assigning to 'NSAllArray *(^)(id)' from 'id<Foo> (^)(id)'}}
166
167    void (^blockWithParam)(NSAllArray *);
168    void (^genericBlockWithParam)(id<Foo>);
169    genericBlockWithParam = blockWithParam;
170    blockWithParam = genericBlockWithParam;
171    return 0;
172}
173#endif
174
175// rdar://10798770
176typedef int NSInteger;
177
178typedef enum : NSInteger {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending} NSComparisonResult;
179
180typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
181
182@interface radar10798770
183- (void)sortUsingComparator:(NSComparator)c;
184@end
185
186void f() {
187   radar10798770 *f;
188   [f sortUsingComparator:^(id a, id b) {
189        return NSOrderedSame;
190   }];
191}
192
193// rdar://16739120
194@protocol P1 @end
195@protocol P2 @end
196
197void Test() {
198void (^aBlock)();
199id anId = aBlock;  // OK
200
201id<P1,P2> anQualId = aBlock;  // expected-error {{initializing 'id<P1,P2>' with an expression of incompatible type 'void (^)()'}}
202
203NSArray* anArray = aBlock; // expected-error {{initializing 'NSArray *' with an expression of incompatible type 'void (^)()'}}
204
205aBlock = anId; // OK
206
207id<P1,P2> anQualId1;
208aBlock = anQualId1; // expected-error {{assigning to 'void (^)()' from incompatible type 'id<P1,P2>'}}
209
210NSArray* anArray1;
211aBlock = anArray1; // expected-error {{assigning to 'void (^)()' from incompatible type 'NSArray *'}}
212}
213
214void Test2() {
215  void (^aBlock)();
216  id<NSObject> anQualId1 = aBlock; // Ok
217  id<NSObject, NSCopying> anQualId2 = aBlock; // Ok
218  id<NSObject, NSCopying, NSObject, NSCopying> anQualId3 = aBlock; // Ok
219  id <P1>  anQualId4  = aBlock; // expected-error {{initializing 'id<P1>' with an expression of incompatible type 'void (^)()'}}
220  id<NSObject, P1, NSCopying> anQualId5 = aBlock; // expected-error {{initializing 'id<NSObject,P1,NSCopying>' with an expression of incompatible type 'void (^)()'}}
221  id<NSCopying> anQualId6 = aBlock; // Ok
222}
223
224void Test3() {
225  void (^aBlock)();
226  NSObject *NSO = aBlock; // Ok
227  NSObject<NSObject> *NSO1 = aBlock; // Ok
228  NSObject<NSObject, NSCopying> *NSO2 = aBlock; // Ok
229  NSObject<NSObject, NSCopying, NSObject, NSCopying> *NSO3 = aBlock; // Ok
230  NSObject <P1>  *NSO4  = aBlock; // expected-error {{initializing 'NSObject<P1> *' with an expression of incompatible type 'void (^)()'}}
231  NSObject<NSObject, P1, NSCopying> *NSO5 = aBlock; // expected-error {{initializing 'NSObject<NSObject,P1,NSCopying> *' with an expression of incompatible type 'void (^)()'}}
232  NSObject<NSCopying> *NSO6 = aBlock; // Ok
233}
234
235// rdar://problem/19420731
236typedef NSObject<P1> NSObject_P1;
237typedef NSObject_P1<P2> NSObject_P1_P2;
238
239void Test4(void (^handler)(NSObject_P1_P2 *p)) {
240  Test4(^(NSObject<P2> *p) { });
241}
242