blob: 96e8fcd8d37179bf43afcaf4f8cc2b16d2bbba46 [file] [log] [blame]
Rafael Espindola34970692013-12-12 16:07:11 +00001// RUN: %clang_cc1 -triple i686-pc-linux -std=c++11 -fblocks %s -verify
Douglas Gregor0e8ff392012-02-01 00:09:55 +00002
3void block_capture_errors() {
4 __block int var; // expected-note 2{{'var' declared here}}
Douglas Gregor656bc622012-02-09 08:26:42 +00005 (void)[var] { }; // expected-error{{__block variable 'var' cannot be captured in a lambda}}
Douglas Gregor0e8ff392012-02-01 00:09:55 +00006
Douglas Gregor656bc622012-02-09 08:26:42 +00007 (void)[=] { var = 17; }; // expected-error{{__block variable 'var' cannot be captured in a lambda}}
Douglas Gregor0e8ff392012-02-01 00:09:55 +00008}
Douglas Gregor33e863c2012-02-15 22:08:38 +00009
10void conversion_to_block(int captured) {
11 int (^b1)(int) = [=](int x) { return x + captured; };
12
13 const auto lambda = [=](int x) { return x + captured; };
14 int (^b2)(int) = lambda;
15}
Douglas Gregord3b672c2012-02-16 01:06:16 +000016
17template<typename T>
18class ConstCopyConstructorBoom {
19public:
20 ConstCopyConstructorBoom(ConstCopyConstructorBoom&);
21
22 ConstCopyConstructorBoom(const ConstCopyConstructorBoom&) {
23 T *ptr = 1; // expected-error{{cannot initialize a variable of type 'float *' with an rvalue of type 'int'}}
24 }
25
26 void foo() const;
27};
28
29void conversion_to_block_init(ConstCopyConstructorBoom<int> boom,
30 ConstCopyConstructorBoom<float> boom2) {
31 const auto& lambda1([=] { boom.foo(); }); // okay
32
33 const auto& lambda2([=] { boom2.foo(); }); // expected-note{{in instantiation of member function}}
34 void (^block)(void) = lambda2;
35}
Douglas Gregorfdf598e2012-02-18 09:37:24 +000036
37
38void nesting() {
39 int array[7]; // expected-note 2{{'array' declared here}}
40 [=] () mutable {
41 [&] {
42 ^ {
43 int i = array[2];
44 i += array[3];
45 }();
46 }();
47 }();
48
49 [&] {
50 [=] () mutable {
51 ^ {
52 int i = array[2]; // expected-error{{cannot refer to declaration with an array type inside block}}
53 i += array[3]; // expected-error{{cannot refer to declaration with an array type inside block}}
54 }();
55 }();
56 }();
57}
Douglas Gregor2837aa22012-02-22 17:32:19 +000058
59namespace overloading {
60 void bool_conversion() {
61 if ([](){}) {
62 }
63
64 bool b = []{};
65 b = (bool)[]{};
66 }
67
68 void conversions() {
69 int (*fp)(int) = [](int x) { return x + 1; };
70 fp = [](int x) { return x + 1; };
71
72 typedef int (*func_ptr)(int);
73 fp = (func_ptr)[](int x) { return x + 1; };
74
75 int (^bp)(int) = [](int x) { return x + 1; };
76 bp = [](int x) { return x + 1; };
77
78 typedef int (^block_ptr)(int);
79 bp = (block_ptr)[](int x) { return x + 1; };
80 }
81
82 int &accept_lambda_conv(int (*fp)(int));
83 float &accept_lambda_conv(int (^bp)(int));
84
85 void call_with_lambda() {
86 int &ir = accept_lambda_conv([](int x) { return x + 1; });
87 }
Richard Smithec2748a2014-05-17 04:36:39 +000088
89 template<typename T> using id = T;
90
91 auto a = [](){};
92 struct C : decltype(a) {
93 using decltype(a)::operator id<void(*)()>;
94 private:
95 using decltype(a)::operator id<void(^)()>;
96 } extern c;
97
98 struct D : decltype(a) {
99 using decltype(a)::operator id<void(^)()>;
100 private:
101 using decltype(a)::operator id<void(*)()>; // expected-note {{here}}
102 } extern d;
103
104 bool r1 = c;
105 bool r2 = d; // expected-error {{private}}
Douglas Gregor2837aa22012-02-22 17:32:19 +0000106}
Douglas Gregor7efd007c2012-06-15 16:59:29 +0000107
108namespace PR13117 {
109 struct A {
Richard Smith2589b9802012-07-25 03:56:55 +0000110 template<typename ... Args> static void f(Args...);
111
Douglas Gregor7efd007c2012-06-15 16:59:29 +0000112 template<typename ... Args> static void f1()
113 {
114 (void)^(Args args) { // expected-error{{block contains unexpanded parameter pack 'Args'}}
115 };
116 }
117
118 template<typename ... Args> static void f2()
119 {
Richard Smith2589b9802012-07-25 03:56:55 +0000120 // FIXME: Allow this.
121 f(
122 ^(Args args) // expected-error{{block contains unexpanded parameter pack 'Args'}}
123 { }
124 ... // expected-error{{pack expansion does not contain any unexpanded parameter packs}}
125 );
126 }
127
128 template<typename ... Args> static void f3()
129 {
130 (void)[](Args args) { // expected-error{{expression contains unexpanded parameter pack 'Args'}}
Douglas Gregor7efd007c2012-06-15 16:59:29 +0000131 };
132 }
Richard Smith2589b9802012-07-25 03:56:55 +0000133
134 template<typename ... Args> static void f4()
135 {
136 f([](Args args) { } ...);
137 }
Douglas Gregor7efd007c2012-06-15 16:59:29 +0000138 };
139
140 void g() {
141 A::f1<int, int>();
142 A::f2<int, int>();
143 }
144}