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