xref: /llvm-project/clang/test/SemaObjC/block-type-safety.m (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
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)(void)) {
20     Super *o = f();
21}
22
23void r1(Sub* (^f)(void)) { // 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(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*
37
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; });
41
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; });
45
46    r2(^id<NSObject>(void) { 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(void) {
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@protocol Foo @end
111
112@interface Baz @end
113
114@interface Baz(FooConformance) <Foo>
115@end
116
117@implementation Baz @end
118
119int test4 (void) {
120    id <Foo> (^b)(void) = ^{ // Doesn't work
121        return (Baz *)0;
122    };
123    return 0;
124}
125
126@protocol NSCopying @end
127
128@interface NSAllArray <NSCopying>
129@end
130
131@interface NSAllArray (FooConformance) <Foo>
132@end
133
134#ifndef COMPATIBILITY_QUALIFIED_ID_TYPE_CHECKING
135int test5(void) {
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)'}}
142
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;
149    return 0;
150}
151#else
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.
157int test5(void) {
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)'}}
162
163    void (^blockWithParam)(NSAllArray *);
164    void (^genericBlockWithParam)(id<Foo>);
165    genericBlockWithParam = blockWithParam;
166    blockWithParam = genericBlockWithParam;
167    return 0;
168}
169#endif
170
171typedef int NSInteger;
172
173typedef enum : NSInteger {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending} NSComparisonResult;
174
175typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
176
177@interface radar10798770
178- (void)sortUsingComparator:(NSComparator)c;
179@end
180
181void f(void) {
182   radar10798770 *f;
183   [f sortUsingComparator:^(id a, id b) {
184        return NSOrderedSame;
185   }];
186}
187
188@protocol P1 @end
189@protocol P2 @end
190
191void Test(void) {
192void (^aBlock)(void);
193id anId = aBlock;  // OK
194
195id<P1,P2> anQualId = aBlock;  // expected-error {{initializing 'id<P1,P2>' with an expression of incompatible type 'void (^)(void)'}}
196
197NSArray* anArray = aBlock; // expected-error {{initializing 'NSArray *' with an expression of incompatible type 'void (^)(void)'}}
198
199aBlock = anId; // OK
200
201id<P1,P2> anQualId1;
202aBlock = anQualId1; // expected-error {{assigning to 'void (^)(void)' from incompatible type 'id<P1,P2>'}}
203
204NSArray* anArray1;
205aBlock = anArray1; // expected-error {{assigning to 'void (^)(void)' from incompatible type 'NSArray *'}}
206}
207
208void Test2(void) {
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
216}
217
218void Test3(void) {
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
227}
228
229typedef NSObject<P1> NSObject_P1;
230typedef NSObject_P1<P2> NSObject_P1_P2;
231
232void Test4(void (^handler)(NSObject_P1_P2 *p)) {
233  Test4(^(NSObject<P2> *p) { });
234}
235