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
ImplicitThisCapture()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
f()27 void f() {
28 [this] () {}; // expected-error {{'this' cannot be captured in this context}}
29 }
30 }
31
32 namespace ReturnDeduction {
test()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 {
test()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 SpecialMembers {
f()99 void f() {
100 auto a = []{}; // expected-note 2{{here}} expected-note 2{{candidate}}
101 decltype(a) b; // expected-error {{no matching constructor}}
102 decltype(a) c = a;
103 decltype(a) d = static_cast<decltype(a)&&>(a);
104 a = a; // expected-error {{copy assignment operator is implicitly deleted}}
105 a = static_cast<decltype(a)&&>(a); // expected-error {{copy assignment operator is implicitly deleted}}
106 }
107 struct P {
108 P(const P&) = delete; // expected-note {{deleted here}}
109 };
110 struct Q {
111 ~Q() = delete; // expected-note {{deleted here}}
112 };
113 struct R {
114 R(const R&) = default;
115 R(R&&) = delete;
116 R &operator=(const R&) = delete;
117 R &operator=(R&&) = delete;
118 };
g(P & p,Q & q,R & r)119 void g(P &p, Q &q, R &r) {
120 auto pp = [p]{}; // expected-error {{deleted constructor}}
121 auto qq = [q]{}; // expected-error {{deleted function}} expected-note {{because}}
122
123 auto a = [r]{}; // expected-note 2{{here}}
124 decltype(a) b = a;
125 decltype(a) c = static_cast<decltype(a)&&>(a); // ok, copies R
126 a = a; // expected-error {{copy assignment operator is implicitly deleted}}
127 a = static_cast<decltype(a)&&>(a); // expected-error {{copy assignment operator is implicitly deleted}}
128 }
129 }
130
131 namespace PR12031 {
132 struct X {
133 template<typename T>
134 X(const T&);
135 ~X();
136 };
137
138 void f(int i, X x);
g()139 void g() {
140 const int v = 10;
141 f(v, [](){});
142 }
143 }
144
145 namespace Array {
146 int &f(int *p);
147 char &f(...);
g()148 void g() {
149 int n = -1;
150 [=] {
151 int arr[n]; // VLA
152 } ();
153
154 const int m = -1;
155 [] {
156 int arr[m]; // expected-error{{negative size}}
157 } ();
158
159 [&] {
160 int arr[m]; // expected-error{{negative size}}
161 } ();
162
163 [=] {
164 int arr[m]; // expected-error{{negative size}}
165 } ();
166
167 [m] {
168 int arr[m]; // expected-error{{negative size}}
169 } ();
170 }
171 }
172
PR12248()173 void PR12248()
174 {
175 unsigned int result = 0;
176 auto l = [&]() { ++result; };
177 }
178
179 namespace ModifyingCapture {
test()180 void test() {
181 int n = 0;
182 [=] {
183 n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
184 };
185 }
186 }
187
188 namespace VariadicPackExpansion {
189 template<typename T, typename U> using Fst = T;
190 template<typename...Ts> bool g(Fst<bool, Ts> ...bools);
f(Ts &&...ts)191 template<typename...Ts> bool f(Ts &&...ts) {
192 return g<Ts...>([&ts] {
193 if (!ts)
194 return false;
195 --ts;
196 return true;
197 } () ...);
198 }
h()199 void h() {
200 int a = 5, b = 2, c = 3;
201 while (f(a, b, c)) {
202 }
203 }
204
205 struct sink {
sinkVariadicPackExpansion::sink206 template<typename...Ts> sink(Ts &&...) {}
207 };
208
local_class()209 template<typename...Ts> void local_class() {
210 sink {
211 [] (Ts t) {
212 struct S : Ts {
213 void f(Ts t) {
214 Ts &that = *this;
215 that = t;
216 }
217 Ts g() { return *this; };
218 };
219 S s;
220 s.f(t);
221 return s;
222 } (Ts()).g() ...
223 };
224 };
225 struct X {}; struct Y {};
226 template void local_class<X, Y>();
227
nested(Ts...ts)228 template<typename...Ts> void nested(Ts ...ts) {
229 f(
230 // Each expansion of this lambda implicitly captures all of 'ts', because
231 // the inner lambda also expands 'ts'.
232 [&] {
233 return ts + [&] { return f(ts...); } ();
234 } () ...
235 );
236 }
237 template void nested(int, int, int);
238
nested2(Ts...ts)239 template<typename...Ts> void nested2(Ts ...ts) { // expected-note 2{{here}}
240 // Capture all 'ts', use only one.
241 f([&ts...] { return ts; } ()...);
242 // Capture each 'ts', use it.
243 f([&ts] { return ts; } ()...);
244 // Capture all 'ts', use all of them.
245 f([&ts...] { return (int)f(ts...); } ());
246 // Capture each 'ts', use all of them. Ill-formed. In more detail:
247 //
248 // We instantiate two lambdas here; the first captures ts$0, the second
249 // captures ts$1. Both of them reference both ts parameters, so both are
250 // ill-formed because ts can't be implicitly captured.
251 //
252 // FIXME: This diagnostic does not explain what's happening. We should
253 // specify which 'ts' we're referring to in its diagnostic name. We should
254 // also say which slice of the pack expansion is being performed in the
255 // instantiation backtrace.
256 f([&ts] { return (int)f(ts...); } ()...); // \
257 // expected-error 2{{'ts' cannot be implicitly captured}} \
258 // expected-note 2{{lambda expression begins here}}
259 }
260 template void nested2(int); // ok
261 template void nested2(int, int); // expected-note {{in instantiation of}}
262 }
263
264 namespace PR13860 {
foo()265 void foo() {
266 auto x = PR13860UndeclaredIdentifier(); // expected-error {{use of undeclared identifier 'PR13860UndeclaredIdentifier'}}
267 auto y = [x]() { };
268 static_assert(sizeof(y), "");
269 }
270 }
271
272 namespace PR13854 {
__anone8efe6a84502(void)273 auto l = [](void){};
274 }
275
276 namespace PR14518 {
__anone8efe6a84602(void) 277 auto f = [](void) { return __func__; }; // no-warning
278 }
279
280 namespace PR16708 {
__anone8efe6a84702() 281 auto L = []() {
282 auto ret = 0;
283 return ret;
284 return 0;
285 };
286 }
287
288 namespace TypeDeduction {
289 struct S {};
f()290 void f() {
291 const S s {};
292 S &&t = [&] { return s; } ();
293 #if __cplusplus > 201103L
294 S &&u = [&] () -> auto { return s; } ();
295 #endif
296 }
297 }
298
299
300 namespace lambdas_in_NSDMIs {
301 template<class T>
302 struct L {
303 T t{};
__anone8efe6a84a02lambdas_in_NSDMIs::L304 T t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
305 };
306 L<int> l;
307
308 namespace non_template {
309 struct L {
310 int t = 0;
__anone8efe6a84d02(int b) 311 int t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
312 };
313 L l;
314 }
315 }
316
317 // PR18477: don't try to capture 'this' from an NSDMI encountered while parsing
318 // a lambda.
319 namespace NSDMIs_in_lambdas {
320 template<typename T> struct S { int a = 0; int b = a; };
f()321 void f() { []() { S<int> s; }; }
322
__anone8efe6a84f02null323 auto x = []{ struct S { int n, m = n; }; };
__anone8efe6a85002null324 auto y = [&]{ struct S { int n, m = n; }; }; // expected-error {{non-local lambda expression cannot have a capture-default}}
__anone8efe6a85102null325 void g() { auto z = [&]{ struct S { int n, m = n; }; }; }
326 }
327
328 namespace CaptureIncomplete {
329 struct Incomplete; // expected-note 2{{forward decl}}
330 void g(const Incomplete &a);
f(Incomplete & a)331 void f(Incomplete &a) {
332 (void) [a] {}; // expected-error {{incomplete}}
333 (void) [&a] {};
334
335 (void) [=] { g(a); }; // expected-error {{incomplete}}
336 (void) [&] { f(a); };
337 }
338 }
339
340 namespace CaptureAbstract {
341 struct S {
342 virtual void f() = 0; // expected-note {{unimplemented}}
343 int n = 0;
344 };
345 struct T : S {
TCaptureAbstract::T346 constexpr T() {}
347 void f();
348 };
f()349 void f() {
350 constexpr T t = T();
351 S &s = const_cast<T&>(t);
352 // FIXME: Once we properly compute odr-use per DR712, this should be
353 // accepted (and should not capture 's').
354 [=] { return s.n; }; // expected-error {{abstract}}
355 }
356 }
357
358 namespace PR18128 {
__anone8efe6a85702null359 auto l = [=]{}; // expected-error {{non-local lambda expression cannot have a capture-default}}
360
361 struct S {
362 int n;
__anone8efe6a85802PR18128::S363 int (*f())[true ? 1 : ([=]{ return n; }(), 0)];
364 // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
365 // expected-error@-2 {{invalid use of non-static data member 'n'}}
366 // expected-error@-3 {{a lambda expression may not appear inside of a constant expression}}
__anone8efe6a85902PR18128::S367 int g(int k = ([=]{ return n; }(), 0));
368 // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
369 // expected-error@-2 {{invalid use of non-static data member 'n'}}
370
__anone8efe6a85a02PR18128::S371 int a = [=]{ return n; }(); // ok
__anone8efe6a85b02PR18128::S372 int b = [=]{ return [=]{ return n; }(); }(); // ok
__anone8efe6a85e02null373 int c = []{ int k = 0; return [=]{ return k; }(); }(); // ok
__anone8efe6a86002null374 int d = []{ return [=]{ return n; }(); }(); // expected-error {{'this' cannot be implicitly captured in this context}}
375 };
376 }
377
378 namespace PR18473 {
f()379 template<typename T> void f() {
380 T t(0);
381 (void) [=]{ int n = t; }; // expected-error {{deleted}}
382 }
383
384 template void f<int>();
385 struct NoCopy {
386 NoCopy(int);
387 NoCopy(const NoCopy &) = delete; // expected-note {{deleted}}
388 operator int() const;
389 };
390 template void f<NoCopy>(); // expected-note {{instantiation}}
391 }
392
PR19249()393 void PR19249() {
394 auto x = [&x]{}; // expected-error {{cannot appear in its own init}}
395 }
396
397 namespace PR20731 {
398 template <class L, int X = sizeof(L)>
399 void Job(L l);
400
401 template <typename... Args>
Logger(Args &&...args)402 void Logger(Args &&... args) {
403 auto len = Invalid_Function((args)...);
404 // expected-error@-1 {{use of undeclared identifier 'Invalid_Function'}}
405 Job([len]() {});
406 }
407
GetMethod()408 void GetMethod() {
409 Logger();
410 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::Logger<>' requested here}}
411 }
412
413 template <typename T>
414 struct A {
415 T t;
416 // expected-error@-1 {{field has incomplete type 'void'}}
417 };
418
419 template <typename F>
g(F f)420 void g(F f) {
421 auto a = A<decltype(f())>{};
422 // expected-note@-1 {{in instantiation of template class 'PR20731::A<void>' requested here}}
423 auto xf = [a, f]() {};
424 int x = sizeof(xf);
425 };
f()426 void f() {
427 g([] {});
428 // expected-note-re@-1 {{in instantiation of function template specialization 'PR20731::g<(lambda at {{.*}}>' requested here}}
429 }
430
431 template <class _Rp> struct function {
432 template <class _Fp>
functionPR20731::function433 function(_Fp) {
434 static_assert(sizeof(_Fp) > 0, "Type must be complete.");
435 }
436 };
437
p(T t)438 template <typename T> void p(T t) {
439 auto l = some_undefined_function(t);
440 // expected-error@-1 {{use of undeclared identifier 'some_undefined_function'}}
441 function<void()>(([l]() {}));
442 }
q()443 void q() { p(0); }
444 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::p<int>' requested here}}
445 }
446
447 namespace lambda_in_default_mem_init {
f()448 template<typename T> void f() {
449 struct S { int n = []{ return 0; }(); };
450 }
451 template void f<int>();
452
g()453 template<typename T> void g() {
454 struct S { int n = [](int n){ return n; }(0); };
455 }
456 template void g<int>();
457 }
458
459 namespace error_in_transform_prototype {
460 template<class T>
f(T t)461 void f(T t) {
462 // expected-error@+2 {{type 'int' cannot be used prior to '::' because it has no members}}
463 // expected-error@+1 {{no member named 'ns' in 'error_in_transform_prototype::S'}}
464 auto x = [](typename T::ns::type &k) {};
465 }
466 class S {};
foo()467 void foo() {
468 f(5); // expected-note {{requested here}}
469 f(S()); // expected-note {{requested here}}
470 }
471 }
472
473 namespace PR21857 {
474 template<typename Fn> struct fun : Fn {
475 fun() = default;
476 using Fn::operator();
477 };
478 template<typename Fn> fun<Fn> wrap(Fn fn);
__anone8efe6a86a02()479 auto x = wrap([](){});
480 }
481
482 namespace PR13987 {
483 class Enclosing {
__anone8efe6a86b02()484 void Method(char c = []()->char {
485 int d = []()->int {
486 struct LocalClass {
487 int Method() { return 0; }
488 };
489 return 0;
490 }();
491 return d; }()
492 );
493 };
494 }
495
496 namespace PR23860 {
497 template <class> struct A {
__anone8efe6a86d02PR23860::A498 void f(int x = []() {
499 struct B {
500 void g() {}
501 };
502 return 0;
503 }());
504 };
505
main()506 int main() {
507 }
508
509 A<int> a;
510 }
511
512 // rdar://22032373
513 namespace rdar22032373 {
foo()514 void foo() {
515 auto blk = [](bool b) {
516 if (b)
517 return undeclared_error; // expected-error {{use of undeclared identifier}}
518 return 0;
519 };
520 }
521 }
522
523 namespace nested_lambda {
524 template <int N>
525 class S {};
526
foo()527 void foo() {
528 const int num = 18;
529 auto outer = []() {
530 auto inner = [](S<num> &X) {};
531 };
532 }
533 }
534
535 namespace PR27994 {
536 struct A { template <class T> A(T); };
537
538 template <class T>
539 struct B {
540 int x;
__anone8efe6a87102PR27994::B541 A a = [&] { int y = x; };
__anone8efe6a87402null542 A b = [&] { [&] { [&] { int y = x; }; }; };
__anone8efe6a87502PR27994::B543 A d = [&](auto param) { int y = x; };
__anone8efe6a87802(auto param2) 544 A e = [&](auto param) { [&] { [&](auto param2) { int y = x; }; }; };
545 };
546
547 B<int> b;
548
549 template <class T> struct C {
550 struct D {
551 int x;
__anone8efe6a87902PR27994::C::D552 A f = [&] { int y = x; };
553 };
554 };
555
func()556 int func() {
557 C<int> a;
558 decltype(a)::D b;
559 }
560 }
561
562 namespace PR30566 {
563 int name1; // expected-note {{'name1' declared here}}
564
565 struct S1 {
566 template<class T>
S1PR30566::S1567 S1(T t) { s = sizeof(t); }
568 int s;
569 };
570
foo1()571 void foo1() {
572 auto s0 = S1{[name=]() {}}; // expected-error 2 {{expected expression}}
573 auto s1 = S1{[name=name]() {}}; // expected-error {{use of undeclared identifier 'name'; did you mean 'name1'?}}
574 }
575 }
576
577 namespace PR25627_dont_odr_use_local_consts {
578
579 template<int> struct X {};
580
foo()581 void foo() {
582 const int N = 10;
583 (void) [] { X<N> x; };
584 }
585 }
586
587 namespace ConversionOperatorDoesNotHaveDeducedReturnType {
__anone8efe6a87d02(int)588 auto x = [](int){};
__anone8efe6a87e02(auto) 589 auto y = [](auto) -> void {};
590 using T = decltype(x);
591 using U = decltype(y);
592 using ExpectedTypeT = void (*)(int);
593 template<typename T>
594 using ExpectedTypeU = void (*)(T);
595
596 struct X {
597 friend T::operator ExpectedTypeT() const;
598
599 // Formally, this is invalid, because the return type of the conversion
600 // function for a generic lambda expression is an unspecified decltype
601 // type, which this should not match. However, this declaration is
602 // functionally equivalent to that one, so we're permitted to choose to
603 // accept this.
604 template<typename T>
605 friend U::operator ExpectedTypeU<T>() const;
606 };
607
608 // This used to crash in return type deduction for the conversion opreator.
__anone8efe6a87f02(decltype(n)) 609 struct A { int n; void f() { +[](decltype(n)) {}; } };
610 }
611
612 namespace TypoCorrection {
613 template <typename T> struct X {};
614 // expected-note@-1 {{template parameter is declared here}}
615
616 template <typename T>
Run(const int & points)617 void Run(const int& points) {
618 // expected-note@-1 {{'points' declared here}}
619 auto outer_lambda = []() {
620 auto inner_lambda = [](const X<Points>&) {};
621 // expected-error@-1 {{use of undeclared identifier 'Points'; did you mean 'points'?}}
622 // expected-error@-2 {{template argument for template type parameter must be a type}}
623 };
624 }
625 }
626