brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.9 KiB · 8509e6f Raw
235 lines · plain
1// RUN: %clang_cc1 -fsyntax-only -verify -fblocks -Wno-objc-root-class %s2// 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 %s4// test for block type safety.5 6@interface Super  @end7@interface Sub : Super @end8 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; });  // OK39    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; });    // OK44    r1(^id (void) { return 0; }); 45     46    r2(^id<NSObject>(void) { return 0; });47}48 49 50@interface A @end51@interface B @end52 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) { }); // OK66  f1(^(A* a) { });67   f1(^(id<NSObject> a) { });	// OK68}69 70@interface NSArray71   // Calls block() with every object in the array72   -enumerateObjectsWithBlock:(void (^)(id obj))block;73@end74 75@interface MyThing76-(void) printThing;77@end78 79@implementation MyThing80    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@end99 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 @end111 112@interface Baz @end113 114@interface Baz(FooConformance) <Foo>115@end116 117@implementation Baz @end118 119int test4 (void) {120    id <Foo> (^b)(void) = ^{ // Doesn't work121        return (Baz *)0;122    };123    return 0;124}125 126@protocol NSCopying @end127 128@interface NSAllArray <NSCopying>129@end130 131@interface NSAllArray (FooConformance) <Foo>132@end133 134#ifndef COMPATIBILITY_QUALIFIED_ID_TYPE_CHECKING135int test5(void) {136    // Returned value is used outside of a block, so error on changing137    // 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 type144    // 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#else152// In Apple SDK APIs using NSItemProviderCompletionHandler require to work with153// blocks that have parameters more specific than in method signatures. As154// explained in non-compatibility test above, it is not safe in general. But155// to keep existing code working we support a compatibility mode that uses156// 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#endif170 171typedef int NSInteger;172 173typedef enum : NSInteger {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending} NSComparisonResult;174 175typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);176 177@interface radar10798770178- (void)sortUsingComparator:(NSComparator)c;179@end180 181void f(void) {182   radar10798770 *f;183   [f sortUsingComparator:^(id a, id b) {184        return NSOrderedSame;185   }];186}187 188@protocol P1 @end189@protocol P2 @end190 191void Test(void) {192void (^aBlock)(void);193id anId = aBlock;  // OK194 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; // OK200 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; // Ok211  id<NSObject, NSCopying> anQualId2 = aBlock; // Ok212  id<NSObject, NSCopying, NSObject, NSCopying> anQualId3 = aBlock; // Ok213  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; // Ok216}217 218void Test3(void) {219  void (^aBlock)(void);220  NSObject *NSO = aBlock; // Ok221  NSObject<NSObject> *NSO1 = aBlock; // Ok222  NSObject<NSObject, NSCopying> *NSO2 = aBlock; // Ok223  NSObject<NSObject, NSCopying, NSObject, NSCopying> *NSO3 = aBlock; // Ok224  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; // Ok227}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