Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -fblocks %s |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 2 | @protocol NSObject; |
| 3 | |
| 4 | void bar(id(^)(void)); |
| 5 | void foo(id <NSObject>(^objectCreationBlock)(void)) { |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 6 | return bar(objectCreationBlock); // OK |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 7 | } |
| 8 | |
| 9 | void bar2(id(*)(void)); |
| 10 | void foo2(id <NSObject>(*objectCreationBlock)(void)) { |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 11 | return bar2(objectCreationBlock); // expected-warning{{incompatible pointer types passing 'id<NSObject> (*)()' to parameter of type 'id (*)()'}} |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 12 | } |
| 13 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 14 | void bar3(id(*)()); // expected-note{{candidate function}} |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 15 | void foo3(id (*objectCreationBlock)(int)) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 16 | return bar3(objectCreationBlock); // expected-error{{no matching}} |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 17 | } |
| 18 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 19 | void bar4(id(^)()); // expected-note{{candidate function}} |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 20 | void foo4(id (^objectCreationBlock)(int)) { |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 21 | return bar4(objectCreationBlock); // expected-error{{no matching}} |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | void foo5(id (^x)(int)) { |
| 25 | if (x) { } |
| 26 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 27 | |
| 28 | // <rdar://problem/6590445> |
| 29 | @interface Foo { |
| 30 | @private |
| 31 | void (^_block)(void); |
| 32 | } |
| 33 | - (void)bar; |
| 34 | @end |
| 35 | |
| 36 | namespace N { |
| 37 | class X { }; |
| 38 | void foo(X); |
| 39 | } |
| 40 | |
| 41 | @implementation Foo |
| 42 | - (void)bar { |
| 43 | _block(); |
| 44 | foo(N::X()); // okay |
| 45 | } |
| 46 | @end |
Fariborz Jahanian | 3b27f1a | 2009-12-11 22:40:48 +0000 | [diff] [blame] | 47 | |
| 48 | typedef signed char BOOL; |
| 49 | void foo6(void *block) { |
| 50 | void (^vb)(id obj, int idx, BOOL *stop) = (void (^)(id, int, BOOL *))block; |
| 51 | BOOL (^bb)(id obj, int idx, BOOL *stop) = (BOOL (^)(id, int, BOOL *))block; |
| 52 | } |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 53 | |
| 54 | // <rdar://problem/8600419>: Require that the types of block |
| 55 | // parameters are complete. |
| 56 | namespace N1 { |
| 57 | template<class _T> class ptr; // expected-note{{template is declared here}} |
| 58 | |
| 59 | template<class _T> |
| 60 | class foo { |
| 61 | public: |
| 62 | void bar(void (^)(ptr<_T>)); |
| 63 | }; |
| 64 | |
| 65 | class X; |
| 66 | |
| 67 | void test2(); |
| 68 | |
| 69 | void test() |
| 70 | { |
| 71 | foo<X> f; |
| 72 | f.bar(^(ptr<X> _f) { // expected-error{{implicit instantiation of undefined template 'N1::ptr<N1::X>'}} |
| 73 | test2(); |
| 74 | }); |
| 75 | } |
| 76 | } |
John McCall | 2998d6b | 2011-01-19 11:48:09 +0000 | [diff] [blame] | 77 | |
| 78 | // Make sure we successfully instantiate the copy constructor of a |
| 79 | // __block variable's type. |
| 80 | namespace N2 { |
| 81 | template <int n> struct A { |
| 82 | A() {} |
| 83 | A(const A &other) { |
| 84 | int invalid[-n]; // expected-error 2 {{array with a negative size}} |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | void test1() { |
| 89 | __block A<1> x; // expected-note {{requested here}} |
| 90 | } |
| 91 | |
| 92 | template <int n> void test2() { |
| 93 | __block A<n> x; // expected-note {{requested here}} |
| 94 | } |
| 95 | template void test2<2>(); |
| 96 | } |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 97 | |
| 98 | // Handle value-dependent block declaration references. |
| 99 | namespace N3 { |
| 100 | template<int N> struct X { }; |
| 101 | |
| 102 | template<int N> |
| 103 | void f() { |
| 104 | X<N> xN = ^() { return X<N>(); }(); |
| 105 | } |
| 106 | } |
Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 107 | |
| 108 | // rdar://8979379 |
| 109 | |
| 110 | @interface A |
| 111 | @end |
| 112 | |
| 113 | @interface B : A |
| 114 | @end |
| 115 | |
| 116 | void f(int (^bl)(A* a)); // expected-note {{candidate function not viable: no known conversion from 'int (^)(B *)' to 'int (^)(A *)' for 1st argument}} |
| 117 | |
| 118 | void g() { |
| 119 | f(^(B* b) { return 0; }); // expected-error {{no matching function for call to 'f'}} |
| 120 | } |
Douglas Gregor | bc3e64f | 2011-06-05 05:14:41 +0000 | [diff] [blame] | 121 | |
| 122 | namespace DependentReturn { |
| 123 | template<typename T> |
| 124 | void f(T t) { |
| 125 | (void)^(T u) { |
| 126 | if (t != u) |
| 127 | return t + u; |
| 128 | else |
| 129 | return; |
| 130 | }; |
| 131 | |
| 132 | (void)^(T u) { |
| 133 | if (t == u) |
| 134 | return; |
| 135 | else |
| 136 | return t + u; |
| 137 | }; |
| 138 | } |
| 139 | |
| 140 | struct X { }; |
| 141 | void operator+(X, X); |
| 142 | bool operator==(X, X); |
| 143 | bool operator!=(X, X); |
| 144 | |
| 145 | template void f<X>(X); |
| 146 | } |