blob: e8e2eb612a0576786deb278a142c4ae12236070b [file] [log] [blame]
Richard Smith14d937a2013-07-26 23:45:07 +00001// RUN: %clang_cc1 -std=c++11 -Wno-unused-value -fsyntax-only -verify -fblocks %s
2// RUN: %clang_cc1 -std=c++1y -Wno-unused-value -fsyntax-only -verify -fblocks %s
Eli Friedmane81d7e92012-01-07 01:08:17 +00003
Eli Friedman72899c32012-01-07 04:59:52 +00004namespace std { class type_info; };
5
Eli Friedmane81d7e92012-01-07 01:08:17 +00006namespace ExplicitCapture {
Eli Friedmane81d7e92012-01-07 01:08:17 +00007 class C {
Eli Friedman72899c32012-01-07 04:59:52 +00008 int Member;
Eli Friedmane81d7e92012-01-07 01:08:17 +00009
Eli Friedman72899c32012-01-07 04:59:52 +000010 static void Overload(int);
11 void Overload();
12 virtual C& Overload(float);
13
Eli Friedman72899c32012-01-07 04:59:52 +000014 void ImplicitThisCapture() {
Douglas Gregorb326ca82012-02-09 08:26:42 +000015 [](){(void)Member;}; // expected-error {{'this' cannot be implicitly captured in this context}}
16 [&](){(void)Member;};
17
18 [this](){(void)Member;};
19 [this]{[this]{};};
20 []{[this]{};};// expected-error {{'this' cannot be implicitly captured in this context}}
Aaron Ballman28769832012-06-04 20:07:46 +000021 []{Overload(3);};
22 []{Overload();}; // expected-error {{'this' cannot be implicitly captured in this context}}
Douglas Gregorb326ca82012-02-09 08:26:42 +000023 []{(void)typeid(Overload());};
Aaron Ballman28769832012-06-04 20:07:46 +000024 []{(void)typeid(Overload(.5f));};// expected-error {{'this' cannot be implicitly captured in this context}}
Eli Friedman72899c32012-01-07 04:59:52 +000025 }
Eli Friedmane81d7e92012-01-07 01:08:17 +000026 };
27
28 void f() {
Aaron Ballman28769832012-06-04 20:07:46 +000029 [this] () {}; // expected-error {{'this' cannot be captured in this context}}
Eli Friedmane81d7e92012-01-07 01:08:17 +000030 }
31}
Eli Friedman84b007f2012-01-26 03:00:14 +000032
33namespace ReturnDeduction {
34 void test() {
Aaron Ballman28769832012-06-04 20:07:46 +000035 [](){ return 1; };
36 [](){ return 1; };
37 [](){ return ({return 1; 1;}); };
38 [](){ return ({return 'c'; 1;}); }; // expected-error {{must match previous return type}}
39 []()->int{ return 'c'; return 1; };
Douglas Gregorb326ca82012-02-09 08:26:42 +000040 [](){ return 'c'; return 1; }; // expected-error {{must match previous return type}}
Aaron Ballman28769832012-06-04 20:07:46 +000041 []() { return; return (void)0; };
42 [](){ return 1; return 1; };
Eli Friedman84b007f2012-01-26 03:00:14 +000043 }
44}
Eli Friedmanb942cb22012-02-03 22:47:37 +000045
46namespace ImplicitCapture {
47 void test() {
Eli Friedmancefc7b22012-02-03 23:06:43 +000048 int a = 0; // expected-note 5 {{declared}}
Aaron Ballman28769832012-06-04 20:07:46 +000049 []() { return a; }; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{begins here}}
50 [&]() { return a; };
51 [=]() { return a; };
52 [=]() { 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 +000053 [=]() { return [&]() { return a; }; };
Aaron Ballman28769832012-06-04 20:07:46 +000054 []() { 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 ^{ return a; }; };// expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
56 []() { 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}}
57 [=]() { return [&a] { return a; }; }; //
Eli Friedmanb942cb22012-02-03 22:47:37 +000058
59 const int b = 2;
Aaron Ballman28769832012-06-04 20:07:46 +000060 []() { return b; };
Eli Friedmanb942cb22012-02-03 22:47:37 +000061
62 union { // expected-note {{declared}}
63 int c;
64 float d;
65 };
66 d = 3;
Aaron Ballman28769832012-06-04 20:07:46 +000067 [=]() { return c; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}}
Eli Friedmanb942cb22012-02-03 22:47:37 +000068
Eli Friedmancefc7b22012-02-03 23:06:43 +000069 __block int e; // expected-note 3 {{declared}}
Aaron Ballman28769832012-06-04 20:07:46 +000070 [&]() { return e; }; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}}
71 [&e]() { return e; }; // expected-error 2 {{__block variable 'e' cannot be captured in a lambda expression}}
Eli Friedmanb942cb22012-02-03 22:47:37 +000072
73 int f[10]; // expected-note {{declared}}
Aaron Ballman28769832012-06-04 20:07:46 +000074 [&]() { return f[2]; };
Douglas Gregorf8af9822012-02-12 18:42:33 +000075 (void) ^{ return []() { return f[2]; }; }; // expected-error {{variable 'f' cannot be implicitly captured in a lambda with no capture-default specified}} \
76 // expected-note{{lambda expression begins here}}
Eli Friedmanb942cb22012-02-03 22:47:37 +000077
78 struct G { G(); G(G&); int a; }; // expected-note 6 {{not viable}}
79 G g;
Aaron Ballman28769832012-06-04 20:07:46 +000080 [=]() { const G* gg = &g; return gg->a; };
Eli Friedman9dd686d2012-10-24 20:28:18 +000081 [=]() { return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error {{no matching constructor for initialization of 'G'}}
82 (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 +000083
84 const int h = a; // expected-note {{declared}}
Aaron Ballman28769832012-06-04 20:07:46 +000085 []() { 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 +000086
Richard Smith5016a702012-10-20 01:38:33 +000087 // References can appear in constant expressions if they are initialized by
88 // reference constant expressions.
89 int i;
90 int &ref_i = i; // expected-note {{declared}}
Richard Smith16581332012-03-02 04:14:40 +000091 [] { 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 +000092
93 static int j;
94 int &ref_j = j;
95 [] { return ref_j; }; // ok
Eli Friedmanb942cb22012-02-03 22:47:37 +000096 }
97}
Douglas Gregorb09ab8c2012-02-21 20:05:31 +000098
99namespace PR12031 {
100 struct X {
101 template<typename T>
102 X(const T&);
103 ~X();
104 };
105
106 void f(int i, X x);
107 void g() {
108 const int v = 10;
109 f(v, [](){});
110 }
111}
Richard Smith359c89d2012-02-24 22:12:32 +0000112
Richard Smithf050d242013-06-13 02:46:14 +0000113namespace Array {
Richard Smith359c89d2012-02-24 22:12:32 +0000114 int &f(int *p);
115 char &f(...);
116 void g() {
Richard Smithf050d242013-06-13 02:46:14 +0000117 int n = -1;
Richard Smith359c89d2012-02-24 22:12:32 +0000118 [=] {
Richard Smithf050d242013-06-13 02:46:14 +0000119 int arr[n]; // VLA
Richard Smith359c89d2012-02-24 22:12:32 +0000120 } ();
121
Richard Smithf050d242013-06-13 02:46:14 +0000122 const int m = -1;
123 [] {
124 int arr[m]; // expected-error{{negative size}}
Richard Smith359c89d2012-02-24 22:12:32 +0000125 } ();
126
Richard Smithf050d242013-06-13 02:46:14 +0000127 [&] {
128 int arr[m]; // expected-error{{negative size}}
129 } ();
130
131 [=] {
132 int arr[m]; // expected-error{{negative size}}
Richard Smith359c89d2012-02-24 22:12:32 +0000133 } ();
134
135 [m] {
Richard Smithf050d242013-06-13 02:46:14 +0000136 int arr[m]; // expected-error{{negative size}}
Richard Smith359c89d2012-02-24 22:12:32 +0000137 } ();
138 }
139}
Eli Friedman71930e02012-03-12 20:57:19 +0000140
141void PR12248()
142{
143 unsigned int result = 0;
144 auto l = [&]() { ++result; };
145}
John McCall78dae242012-03-13 00:37:01 +0000146
147namespace ModifyingCapture {
148 void test() {
149 int n = 0;
150 [=] {
John McCall23dde822012-03-13 01:10:51 +0000151 n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
John McCall78dae242012-03-13 00:37:01 +0000152 };
153 }
154}
Richard Smith612409e2012-07-25 03:56:55 +0000155
156namespace VariadicPackExpansion {
157 template<typename T, typename U> using Fst = T;
158 template<typename...Ts> bool g(Fst<bool, Ts> ...bools);
159 template<typename...Ts> bool f(Ts &&...ts) {
160 return g<Ts...>([&ts] {
161 if (!ts)
162 return false;
163 --ts;
164 return true;
165 } () ...);
166 }
167 void h() {
168 int a = 5, b = 2, c = 3;
169 while (f(a, b, c)) {
170 }
171 }
172
173 struct sink {
174 template<typename...Ts> sink(Ts &&...) {}
175 };
176
177 template<typename...Ts> void local_class() {
178 sink {
179 [] (Ts t) {
180 struct S : Ts {
181 void f(Ts t) {
182 Ts &that = *this;
183 that = t;
184 }
185 Ts g() { return *this; };
186 };
187 S s;
188 s.f(t);
189 return s;
190 } (Ts()).g() ...
191 };
192 };
193 struct X {}; struct Y {};
194 template void local_class<X, Y>();
195
196 template<typename...Ts> void nested(Ts ...ts) {
197 f(
198 // Each expansion of this lambda implicitly captures all of 'ts', because
199 // the inner lambda also expands 'ts'.
200 [&] {
201 return ts + [&] { return f(ts...); } ();
202 } () ...
203 );
204 }
205 template void nested(int, int, int);
206
207 template<typename...Ts> void nested2(Ts ...ts) { // expected-note 2{{here}}
208 // Capture all 'ts', use only one.
209 f([&ts...] { return ts; } ()...);
210 // Capture each 'ts', use it.
211 f([&ts] { return ts; } ()...);
212 // Capture all 'ts', use all of them.
213 f([&ts...] { return (int)f(ts...); } ());
214 // Capture each 'ts', use all of them. Ill-formed. In more detail:
215 //
216 // We instantiate two lambdas here; the first captures ts$0, the second
217 // captures ts$1. Both of them reference both ts parameters, so both are
218 // ill-formed because ts can't be implicitly captured.
219 //
220 // FIXME: This diagnostic does not explain what's happening. We should
221 // specify which 'ts' we're referring to in its diagnostic name. We should
222 // also say which slice of the pack expansion is being performed in the
223 // instantiation backtrace.
224 f([&ts] { return (int)f(ts...); } ()...); // \
225 // expected-error 2{{'ts' cannot be implicitly captured}} \
226 // expected-note 2{{lambda expression begins here}}
227 }
228 template void nested2(int); // ok
229 template void nested2(int, int); // expected-note {{in instantiation of}}
230}
Eli Friedman9cd5b242012-09-18 21:11:30 +0000231
232namespace PR13860 {
233 void foo() {
234 auto x = PR13860UndeclaredIdentifier(); // expected-error {{use of undeclared identifier 'PR13860UndeclaredIdentifier'}}
235 auto y = [x]() { };
236 static_assert(sizeof(y), "");
237 }
238}
Eli Friedman7c3c6bc2012-09-20 01:40:23 +0000239
240namespace PR13854 {
241 auto l = [](void){};
242}
Benjamin Kramer42427402012-12-06 15:42:21 +0000243
244namespace PR14518 {
245 auto f = [](void) { return __func__; }; // no-warning
246}
Richard Smithec0808d2013-07-26 22:53:54 +0000247
248namespace PR16708 {
249 auto L = []() {
250 auto ret = 0;
251 return ret;
252 return 0;
253 };
254}
Richard Smith14d937a2013-07-26 23:45:07 +0000255
256namespace TypeDeduction {
257 struct S {};
258 void f() {
259 const S s {};
260 S &&t = [&] { return s; } ();
261#if __cplusplus <= 201103L
262 // expected-error@-2 {{drops qualifiers}}
263#else
264 S &&u = [&] () -> auto { return s; } ();
265#endif
266 }
267}