blob: 83c5215d7453b2648960668a5a610cd77651e2e7 [file] [log] [blame]
Douglas Gregor503384f2012-02-09 00:47:04 +00001// RUN: %clang_cc1 -std=c++0x -Wno-unused-value -fsyntax-only -verify -fblocks %s
Eli Friedmane81d7e92012-01-07 01:08:17 +00002
Eli Friedman72899c32012-01-07 04:59:52 +00003namespace std { class type_info; };
4
Eli Friedmane81d7e92012-01-07 01:08:17 +00005namespace ExplicitCapture {
Eli Friedmane81d7e92012-01-07 01:08:17 +00006 class C {
Eli Friedman72899c32012-01-07 04:59:52 +00007 int Member;
Eli Friedmane81d7e92012-01-07 01:08:17 +00008
Eli Friedman72899c32012-01-07 04:59:52 +00009 static void Overload(int);
10 void Overload();
11 virtual C& Overload(float);
12
Eli Friedman72899c32012-01-07 04:59:52 +000013 void ImplicitThisCapture() {
Douglas Gregorb326ca82012-02-09 08:26:42 +000014 [](){(void)Member;}; // expected-error {{'this' cannot be implicitly captured in this context}}
15 [&](){(void)Member;};
16
17 [this](){(void)Member;};
18 [this]{[this]{};};
19 []{[this]{};};// expected-error {{'this' cannot be implicitly captured in this context}}
Aaron Ballman28769832012-06-04 20:07:46 +000020 []{Overload(3);};
21 []{Overload();}; // expected-error {{'this' cannot be implicitly captured in this context}}
Douglas Gregorb326ca82012-02-09 08:26:42 +000022 []{(void)typeid(Overload());};
Aaron Ballman28769832012-06-04 20:07:46 +000023 []{(void)typeid(Overload(.5f));};// expected-error {{'this' cannot be implicitly captured in this context}}
Eli Friedman72899c32012-01-07 04:59:52 +000024 }
Eli Friedmane81d7e92012-01-07 01:08:17 +000025 };
26
27 void f() {
Aaron Ballman28769832012-06-04 20:07:46 +000028 [this] () {}; // expected-error {{'this' cannot be captured in this context}}
Eli Friedmane81d7e92012-01-07 01:08:17 +000029 }
30}
Eli Friedman84b007f2012-01-26 03:00:14 +000031
32namespace ReturnDeduction {
33 void test() {
Aaron Ballman28769832012-06-04 20:07:46 +000034 [](){ return 1; };
35 [](){ return 1; };
36 [](){ return ({return 1; 1;}); };
37 [](){ return ({return 'c'; 1;}); }; // expected-error {{must match previous return type}}
38 []()->int{ return 'c'; return 1; };
Douglas Gregorb326ca82012-02-09 08:26:42 +000039 [](){ return 'c'; return 1; }; // expected-error {{must match previous return type}}
Aaron Ballman28769832012-06-04 20:07:46 +000040 []() { return; return (void)0; };
41 [](){ return 1; return 1; };
Eli Friedman84b007f2012-01-26 03:00:14 +000042 }
43}
Eli Friedmanb942cb22012-02-03 22:47:37 +000044
45namespace ImplicitCapture {
46 void test() {
Eli Friedmancefc7b22012-02-03 23:06:43 +000047 int a = 0; // expected-note 5 {{declared}}
Aaron Ballman28769832012-06-04 20:07:46 +000048 []() { return a; }; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{begins here}}
49 [&]() { return a; };
50 [=]() { return a; };
51 [=]() { int* b = &a; }; // expected-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const int *'}}
Douglas Gregorb326ca82012-02-09 08:26:42 +000052 [=]() { return [&]() { return a; }; };
Aaron Ballman28769832012-06-04 20:07:46 +000053 []() { return [&]() { return a; }; }; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
54 []() { return ^{ return a; }; };// expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
55 []() { return [&a] { return a; }; }; // expected-error 2 {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note 2 {{lambda expression begins here}}
56 [=]() { return [&a] { return a; }; }; //
Eli Friedmanb942cb22012-02-03 22:47:37 +000057
58 const int b = 2;
Aaron Ballman28769832012-06-04 20:07:46 +000059 []() { return b; };
Eli Friedmanb942cb22012-02-03 22:47:37 +000060
61 union { // expected-note {{declared}}
62 int c;
63 float d;
64 };
65 d = 3;
Aaron Ballman28769832012-06-04 20:07:46 +000066 [=]() { return c; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}}
Eli Friedmanb942cb22012-02-03 22:47:37 +000067
Eli Friedmancefc7b22012-02-03 23:06:43 +000068 __block int e; // expected-note 3 {{declared}}
Aaron Ballman28769832012-06-04 20:07:46 +000069 [&]() { return e; }; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}}
70 [&e]() { return e; }; // expected-error 2 {{__block variable 'e' cannot be captured in a lambda expression}}
Eli Friedmanb942cb22012-02-03 22:47:37 +000071
72 int f[10]; // expected-note {{declared}}
Aaron Ballman28769832012-06-04 20:07:46 +000073 [&]() { return f[2]; };
Douglas Gregorf8af9822012-02-12 18:42:33 +000074 (void) ^{ return []() { return f[2]; }; }; // expected-error {{variable 'f' cannot be implicitly captured in a lambda with no capture-default specified}} \
75 // expected-note{{lambda expression begins here}}
Eli Friedmanb942cb22012-02-03 22:47:37 +000076
77 struct G { G(); G(G&); int a; }; // expected-note 6 {{not viable}}
78 G g;
Aaron Ballman28769832012-06-04 20:07:46 +000079 [=]() { const G* gg = &g; return gg->a; };
Eli Friedman9dd686d2012-10-24 20:28:18 +000080 [=]() { return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error {{no matching constructor for initialization of 'G'}}
81 (void)^{ return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error 2 {{no matching constructor for initialization of 'const G'}}
Eli Friedman210386e2012-02-06 21:50:18 +000082
83 const int h = a; // expected-note {{declared}}
Aaron Ballman28769832012-06-04 20:07:46 +000084 []() { return h; }; // expected-error {{variable 'h' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
Richard Smith16581332012-03-02 04:14:40 +000085
Richard Smith5016a702012-10-20 01:38:33 +000086 // References can appear in constant expressions if they are initialized by
87 // reference constant expressions.
88 int i;
89 int &ref_i = i; // expected-note {{declared}}
Richard Smith16581332012-03-02 04:14:40 +000090 [] { return ref_i; }; // expected-error {{variable 'ref_i' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
Richard Smith5016a702012-10-20 01:38:33 +000091
92 static int j;
93 int &ref_j = j;
94 [] { return ref_j; }; // ok
Eli Friedmanb942cb22012-02-03 22:47:37 +000095 }
96}
Douglas Gregorb09ab8c2012-02-21 20:05:31 +000097
98namespace PR12031 {
99 struct X {
100 template<typename T>
101 X(const T&);
102 ~X();
103 };
104
105 void f(int i, X x);
106 void g() {
107 const int v = 10;
108 f(v, [](){});
109 }
110}
Richard Smith359c89d2012-02-24 22:12:32 +0000111
Richard Smithf050d242013-06-13 02:46:14 +0000112namespace Array {
Richard Smith359c89d2012-02-24 22:12:32 +0000113 int &f(int *p);
114 char &f(...);
115 void g() {
Richard Smithf050d242013-06-13 02:46:14 +0000116 int n = -1;
Richard Smith359c89d2012-02-24 22:12:32 +0000117 [=] {
Richard Smithf050d242013-06-13 02:46:14 +0000118 int arr[n]; // VLA
Richard Smith359c89d2012-02-24 22:12:32 +0000119 } ();
120
Richard Smithf050d242013-06-13 02:46:14 +0000121 const int m = -1;
122 [] {
123 int arr[m]; // expected-error{{negative size}}
Richard Smith359c89d2012-02-24 22:12:32 +0000124 } ();
125
Richard Smithf050d242013-06-13 02:46:14 +0000126 [&] {
127 int arr[m]; // expected-error{{negative size}}
128 } ();
129
130 [=] {
131 int arr[m]; // expected-error{{negative size}}
Richard Smith359c89d2012-02-24 22:12:32 +0000132 } ();
133
134 [m] {
Richard Smithf050d242013-06-13 02:46:14 +0000135 int arr[m]; // expected-error{{negative size}}
Richard Smith359c89d2012-02-24 22:12:32 +0000136 } ();
137 }
138}
Eli Friedman71930e02012-03-12 20:57:19 +0000139
140void PR12248()
141{
142 unsigned int result = 0;
143 auto l = [&]() { ++result; };
144}
John McCall78dae242012-03-13 00:37:01 +0000145
146namespace ModifyingCapture {
147 void test() {
148 int n = 0;
149 [=] {
John McCall23dde822012-03-13 01:10:51 +0000150 n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
John McCall78dae242012-03-13 00:37:01 +0000151 };
152 }
153}
Richard Smith612409e2012-07-25 03:56:55 +0000154
155namespace VariadicPackExpansion {
156 template<typename T, typename U> using Fst = T;
157 template<typename...Ts> bool g(Fst<bool, Ts> ...bools);
158 template<typename...Ts> bool f(Ts &&...ts) {
159 return g<Ts...>([&ts] {
160 if (!ts)
161 return false;
162 --ts;
163 return true;
164 } () ...);
165 }
166 void h() {
167 int a = 5, b = 2, c = 3;
168 while (f(a, b, c)) {
169 }
170 }
171
172 struct sink {
173 template<typename...Ts> sink(Ts &&...) {}
174 };
175
176 template<typename...Ts> void local_class() {
177 sink {
178 [] (Ts t) {
179 struct S : Ts {
180 void f(Ts t) {
181 Ts &that = *this;
182 that = t;
183 }
184 Ts g() { return *this; };
185 };
186 S s;
187 s.f(t);
188 return s;
189 } (Ts()).g() ...
190 };
191 };
192 struct X {}; struct Y {};
193 template void local_class<X, Y>();
194
195 template<typename...Ts> void nested(Ts ...ts) {
196 f(
197 // Each expansion of this lambda implicitly captures all of 'ts', because
198 // the inner lambda also expands 'ts'.
199 [&] {
200 return ts + [&] { return f(ts...); } ();
201 } () ...
202 );
203 }
204 template void nested(int, int, int);
205
206 template<typename...Ts> void nested2(Ts ...ts) { // expected-note 2{{here}}
207 // Capture all 'ts', use only one.
208 f([&ts...] { return ts; } ()...);
209 // Capture each 'ts', use it.
210 f([&ts] { return ts; } ()...);
211 // Capture all 'ts', use all of them.
212 f([&ts...] { return (int)f(ts...); } ());
213 // Capture each 'ts', use all of them. Ill-formed. In more detail:
214 //
215 // We instantiate two lambdas here; the first captures ts$0, the second
216 // captures ts$1. Both of them reference both ts parameters, so both are
217 // ill-formed because ts can't be implicitly captured.
218 //
219 // FIXME: This diagnostic does not explain what's happening. We should
220 // specify which 'ts' we're referring to in its diagnostic name. We should
221 // also say which slice of the pack expansion is being performed in the
222 // instantiation backtrace.
223 f([&ts] { return (int)f(ts...); } ()...); // \
224 // expected-error 2{{'ts' cannot be implicitly captured}} \
225 // expected-note 2{{lambda expression begins here}}
226 }
227 template void nested2(int); // ok
228 template void nested2(int, int); // expected-note {{in instantiation of}}
229}
Eli Friedman9cd5b242012-09-18 21:11:30 +0000230
231namespace PR13860 {
232 void foo() {
233 auto x = PR13860UndeclaredIdentifier(); // expected-error {{use of undeclared identifier 'PR13860UndeclaredIdentifier'}}
234 auto y = [x]() { };
235 static_assert(sizeof(y), "");
236 }
237}
Eli Friedman7c3c6bc2012-09-20 01:40:23 +0000238
239namespace PR13854 {
240 auto l = [](void){};
241}
Benjamin Kramer42427402012-12-06 15:42:21 +0000242
243namespace PR14518 {
244 auto f = [](void) { return __func__; }; // no-warning
245}