1 // RUN: %clang_cc1 -std=c++14 -Wno-unused-value -fsyntax-only -verify -fblocks %s
2 
3 namespace std { class type_info; };
4 
5 namespace ExplicitCapture {
6   class C {
7     int Member;
8 
9     static void Overload(int);
10     void Overload();
11     virtual C& Overload(float);
12 
13     void ImplicitThisCapture() {
14       [](){(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}}
20       []{Overload(3);};
21       []{Overload();}; // expected-error {{'this' cannot be implicitly captured in this context}}
22       []{(void)typeid(Overload());};
23       []{(void)typeid(Overload(.5f));};// expected-error {{'this' cannot be implicitly captured in this context}}
24     }
25   };
26 
27   void f() {
28     [this] () {}; // expected-error {{'this' cannot be captured in this context}}
29   }
30 }
31 
32 namespace ReturnDeduction {
33   void test() {
34     [](){ 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; };
39     [](){ return 'c'; return 1; };  // expected-error {{must match previous return type}}
40     []() { return; return (void)0; };
41     [](){ return 1; return 1; };
42   }
43 }
44 
45 namespace ImplicitCapture {
46   void test() {
47     int a = 0; // expected-note 5 {{declared}}
48     []() { 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 *'}}
52     [=]() { return [&]() { return a; }; };
53     []() { 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; }; }; //
57 
58     const int b = 2;
59     []() { return b; };
60 
61     union { // expected-note {{declared}}
62       int c;
63       float d;
64     };
65     d = 3;
66     [=]() { return c; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}}
67 
68     __block int e; // expected-note 3 {{declared}}
69     [&]() { 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}}
71 
72     int f[10]; // expected-note {{declared}}
73     [&]() { return f[2]; };
74     (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}}
76 
77     struct G { G(); G(G&); int a; }; // expected-note 6 {{not viable}}
78     G g;
79     [=]() { const G* gg = &g; return gg->a; };
80     [=]() { 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'}}
82 
83     const int h = a; // expected-note {{declared}}
84     []() { return h; }; // expected-error {{variable 'h' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
85 
86     // 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}}
90     [] { 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}}
91 
92     static int j;
93     int &ref_j = j;
94     [] { return ref_j; }; // ok
95   }
96 }
97 
98 namespace 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 }
111 
112 namespace Array {
113   int &f(int *p);
114   char &f(...);
115   void g() {
116     int n = -1;
117     [=] {
118       int arr[n]; // VLA
119     } ();
120 
121     const int m = -1;
122     [] {
123       int arr[m]; // expected-error{{negative size}}
124     } ();
125 
126     [&] {
127       int arr[m]; // expected-error{{negative size}}
128     } ();
129 
130     [=] {
131       int arr[m]; // expected-error{{negative size}}
132     } ();
133 
134     [m] {
135       int arr[m]; // expected-error{{negative size}}
136     } ();
137   }
138 }
139 
140 void PR12248()
141 {
142   unsigned int result = 0;
143   auto l = [&]() { ++result; };
144 }
145 
146 namespace ModifyingCapture {
147   void test() {
148     int n = 0;
149     [=] {
150       n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
151     };
152   }
153 }
154 
155 namespace 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 }
230 
231 namespace 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 }
238 
239 namespace PR13854 {
240   auto l = [](void){};
241 }
242 
243 namespace PR14518 {
244   auto f = [](void) { return __func__; }; // no-warning
245 }
246 
247 namespace PR16708 {
248   auto L = []() {
249     auto ret = 0;
250     return ret;
251     return 0;
252   };
253 }
254 
255 namespace TypeDeduction {
256   struct S {};
257   void f() {
258     const S s {};
259     S &&t = [&] { return s; } ();
260 #if __cplusplus > 201103L
261     S &&u = [&] () -> auto { return s; } ();
262 #endif
263   }
264 }
265 
266 
267 namespace lambdas_in_NSDMIs {
268   template<class T>
269   struct L {
270       T t{};
271       T t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
272   };
273   L<int> l;
274 
275   namespace non_template {
276     struct L {
277       int t = 0;
278       int t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
279     };
280     L l;
281   }
282 }
283 
284 // PR18477: don't try to capture 'this' from an NSDMI encountered while parsing
285 // a lambda.
286 namespace NSDMIs_in_lambdas {
287   template<typename T> struct S { int a = 0; int b = a; };
288   void f() { []() { S<int> s; }; }
289 
290   auto x = []{ struct S { int n, m = n; }; };
291   auto y = [&]{ struct S { int n, m = n; }; }; // expected-error {{non-local lambda expression cannot have a capture-default}}
292   void g() { auto z = [&]{ struct S { int n, m = n; }; }; }
293 }
294 
295 namespace CaptureIncomplete {
296   struct Incomplete; // expected-note 2{{forward decl}}
297   void g(const Incomplete &a);
298   void f(Incomplete &a) {
299     (void) [a] {}; // expected-error {{incomplete}}
300     (void) [&a] {};
301 
302     (void) [=] { g(a); }; // expected-error {{incomplete}}
303     (void) [&] { f(a); };
304   }
305 }
306 
307 namespace CaptureAbstract {
308   struct S {
309     virtual void f() = 0; // expected-note {{unimplemented}}
310     int n = 0;
311   };
312   struct T : S {
313     constexpr T() {}
314     void f();
315   };
316   void f() {
317     constexpr T t = T();
318     S &s = const_cast<T&>(t);
319     // FIXME: Once we properly compute odr-use per DR712, this should be
320     // accepted (and should not capture 's').
321     [=] { return s.n; }; // expected-error {{abstract}}
322   }
323 }
324 
325 namespace PR18128 {
326   auto l = [=]{}; // expected-error {{non-local lambda expression cannot have a capture-default}}
327 
328   struct S {
329     int n;
330     int (*f())[true ? 1 : ([=]{ return n; }(), 0)];
331     // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
332     // expected-error@-2 {{invalid use of non-static data member 'n'}}
333     // expected-error@-3 {{a lambda expression may not appear inside of a constant expression}}
334     int g(int k = ([=]{ return n; }(), 0));
335     // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
336     // expected-error@-2 {{invalid use of non-static data member 'n'}}
337 
338     int a = [=]{ return n; }(); // ok
339     int b = [=]{ return [=]{ return n; }(); }(); // ok
340     int c = []{ int k = 0; return [=]{ return k; }(); }(); // ok
341     int d = []{ return [=]{ return n; }(); }(); // expected-error {{'this' cannot be implicitly captured in this context}}
342   };
343 }
344 
345 namespace PR18473 {
346   template<typename T> void f() {
347     T t(0);
348     (void) [=]{ int n = t; }; // expected-error {{deleted}}
349   }
350 
351   template void f<int>();
352   struct NoCopy {
353     NoCopy(int);
354     NoCopy(const NoCopy &) = delete; // expected-note {{deleted}}
355     operator int() const;
356   };
357   template void f<NoCopy>(); // expected-note {{instantiation}}
358 }
359 
360 void PR19249() {
361   auto x = [&x]{}; // expected-error {{cannot appear in its own init}}
362 }
363 
364 namespace PR20731 {
365 template <class L, int X = sizeof(L)>
366 void Job(L l);
367 
368 template <typename... Args>
369 void Logger(Args &&... args) {
370   auto len = Invalid_Function((args)...);
371   // expected-error@-1 {{use of undeclared identifier 'Invalid_Function'}}
372   Job([len]() {});
373 }
374 
375 void GetMethod() {
376   Logger();
377   // expected-note@-1 {{in instantiation of function template specialization 'PR20731::Logger<>' requested here}}
378 }
379 
380 template <typename T>
381 struct A {
382   T t;
383   // expected-error@-1 {{field has incomplete type 'void'}}
384 };
385 
386 template <typename F>
387 void g(F f) {
388   auto a = A<decltype(f())>{};
389   // expected-note@-1 {{in instantiation of template class 'PR20731::A<void>' requested here}}
390   auto xf = [a, f]() {};
391   int x = sizeof(xf);
392 };
393 void f() {
394   g([] {});
395   // expected-note-re@-1 {{in instantiation of function template specialization 'PR20731::g<(lambda at {{.*}}>' requested here}}
396 }
397 
398 template <class _Rp> struct function {
399   template <class _Fp>
400   function(_Fp) {
401     static_assert(sizeof(_Fp) > 0, "Type must be complete.");
402   }
403 };
404 
405 template <typename T> void p(T t) {
406   auto l = some_undefined_function(t);
407   // expected-error@-1 {{use of undeclared identifier 'some_undefined_function'}}
408   function<void()>(([l]() {}));
409 }
410 void q() { p(0); }
411 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::p<int>' requested here}}
412 }
413 
414 namespace lambda_in_default_mem_init {
415   template<typename T> void f() {
416     struct S { int n = []{ return 0; }(); };
417   }
418   template void f<int>();
419 
420   template<typename T> void g() {
421     struct S { int n = [](int n){ return n; }(0); };
422   }
423   template void g<int>();
424 }
425 
426 namespace error_in_transform_prototype {
427   template<class T>
428   void f(T t) {
429     // expected-error@+2 {{type 'int' cannot be used prior to '::' because it has no members}}
430     // expected-error@+1 {{no member named 'ns' in 'error_in_transform_prototype::S'}}
431     auto x = [](typename T::ns::type &k) {};
432   }
433   class S {};
434   void foo() {
435     f(5); // expected-note {{requested here}}
436     f(S()); // expected-note {{requested here}}
437   }
438 }
439 
440 namespace PR21857 {
441   template<typename Fn> struct fun : Fn {
442     fun() = default;
443     using Fn::operator();
444   };
445   template<typename Fn> fun<Fn> wrap(Fn fn);
446   auto x = wrap([](){});
447 }
448 
449 namespace PR13987 {
450 class Enclosing {
451   void Method(char c = []()->char {
452     int d = []()->int {
453         struct LocalClass {
454           int Method() { return 0; }
455         };
456       return 0;
457     }();
458     return d; }()
459   );
460 };
461 }
462 
463 namespace PR23860 {
464 template <class> struct A {
465   void f(int x = []() {
466     struct B {
467       void g() {}
468     };
469     return 0;
470   }());
471 };
472 
473 int main() {
474 }
475 
476 A<int> a;
477 }
478 
479 // rdar://22032373
480 namespace rdar22032373 {
481 void foo() {
482   auto blk = [](bool b) {
483     if (b)
484       return undeclared_error; // expected-error {{use of undeclared identifier}}
485     return 0;
486   };
487 }
488 }
489 
490 namespace nested_lambda {
491 template <int N>
492 class S {};
493 
494 void foo() {
495   const int num = 18;  // expected-note {{'num' declared here}}
496   auto outer = []() {
497     auto inner = [](S<num> &X) {};  // expected-error {{variable 'num' cannot be implicitly captured in a lambda with no capture-default specified}}
498   };
499 }
500 }
501 
502 namespace PR27994 {
503 struct A { template <class T> A(T); };
504 
505 template <class T>
506 struct B {
507   int x;
508   A a = [&] { int y = x; };
509   A b = [&] { [&] { [&] { int y = x; }; }; };
510   A d = [&](auto param) { int y = x; };
511   A e = [&](auto param) { [&] { [&](auto param2) { int y = x; }; }; };
512 };
513 
514 B<int> b;
515 
516 template <class T> struct C {
517   struct D {
518     int x;
519     A f = [&] { int y = x; };
520   };
521 };
522 
523 int func() {
524   C<int> a;
525   decltype(a)::D b;
526 }
527 }
528