blob: c91fd103e1333cf039f9e70ce46c1939566a5218 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify -fblocks %s
Douglas Gregor2a7e58d2008-12-23 00:53:59 +00002@protocol NSObject;
3
4void bar(id(^)(void));
5void foo(id <NSObject>(^objectCreationBlock)(void)) {
Fariborz Jahaniane3c8c642011-02-12 19:07:46 +00006 return bar(objectCreationBlock); // OK
Douglas Gregor2a7e58d2008-12-23 00:53:59 +00007}
8
9void bar2(id(*)(void));
10void foo2(id <NSObject>(*objectCreationBlock)(void)) {
Douglas Gregora3998bd2010-12-02 21:47:04 +000011 return bar2(objectCreationBlock); // expected-warning{{incompatible pointer types passing 'id<NSObject> (*)()' to parameter of type 'id (*)()'}}
Douglas Gregor2a7e58d2008-12-23 00:53:59 +000012}
13
Douglas Gregorfa047642009-02-04 00:32:51 +000014void bar3(id(*)()); // expected-note{{candidate function}}
Douglas Gregor2a7e58d2008-12-23 00:53:59 +000015void foo3(id (*objectCreationBlock)(int)) {
Douglas Gregorfa047642009-02-04 00:32:51 +000016 return bar3(objectCreationBlock); // expected-error{{no matching}}
Douglas Gregor2a7e58d2008-12-23 00:53:59 +000017}
18
Douglas Gregorfa047642009-02-04 00:32:51 +000019void bar4(id(^)()); // expected-note{{candidate function}}
Douglas Gregor2a7e58d2008-12-23 00:53:59 +000020void foo4(id (^objectCreationBlock)(int)) {
Douglas Gregorfa047642009-02-04 00:32:51 +000021 return bar4(objectCreationBlock); // expected-error{{no matching}}
Douglas Gregor2a7e58d2008-12-23 00:53:59 +000022}
23
24void foo5(id (^x)(int)) {
25 if (x) { }
26}
Douglas Gregorc71e28c2009-02-16 19:28:42 +000027
28// <rdar://problem/6590445>
29@interface Foo {
30 @private
31 void (^_block)(void);
32}
33- (void)bar;
34@end
35
36namespace 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 Jahanian3b27f1a2009-12-11 22:40:48 +000047
48typedef signed char BOOL;
49void 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 Gregor82aa7132010-11-01 18:37:59 +000053
54// <rdar://problem/8600419>: Require that the types of block
55// parameters are complete.
56namespace 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 McCall2998d6b2011-01-19 11:48:09 +000077
78// Make sure we successfully instantiate the copy constructor of a
79// __block variable's type.
80namespace 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 Gregord967e312011-01-19 21:52:31 +000097
98// Handle value-dependent block declaration references.
99namespace 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 Jahaniane3c8c642011-02-12 19:07:46 +0000107
108// rdar://8979379
109
110@interface A
111@end
112
113@interface B : A
114@end
115
116void f(int (^bl)(A* a)); // expected-note {{candidate function not viable: no known conversion from 'int (^)(B *)' to 'int (^)(A *)' for 1st argument}}
117
118void g() {
119 f(^(B* b) { return 0; }); // expected-error {{no matching function for call to 'f'}}
120}
Douglas Gregorbc3e64f2011-06-05 05:14:41 +0000121
122namespace 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}