1 // RUN: %clang_cc1 -std=c++14 -Wno-unused-value -fsyntax-only -verify -verify=expected-cxx14 -fblocks %s
2 // RUN: %clang_cc1 -std=c++17 -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 const int var = [](){(void)Member; return 0;}(); // expected-error {{'this' cannot be implicitly captured in this context}}
17 [&](){(void)Member;};
18
19 [this](){(void)Member;};
20 [this]{[this]{};};
21 []{[this]{};};// expected-error {{'this' cannot be implicitly captured in this context}}
22 []{Overload(3);};
23 []{Overload();}; // expected-error {{'this' cannot be implicitly captured in this context}}
24 []{(void)typeid(Overload());};
25 []{(void)typeid(Overload(.5f));};// expected-error {{'this' cannot be implicitly captured in this context}}
26 }
27 };
28
f()29 void f() {
30 [this] () {}; // expected-error {{'this' cannot be captured in this context}}
31 }
32 }
33
34 namespace ReturnDeduction {
test()35 void test() {
36 [](){ return 1; };
37 [](){ return 1; };
38 [](){ return ({return 1; 1;}); };
39 [](){ return ({return 'c'; 1;}); }; // expected-error {{must match previous return type}}
40 []()->int{ return 'c'; return 1; };
41 [](){ return 'c'; return 1; }; // expected-error {{must match previous return type}}
42 []() { return; return (void)0; };
43 [](){ return 1; return 1; };
44 }
45 }
46
47 namespace ImplicitCapture {
test()48 void test() {
49 int a = 0; // expected-note 5 {{declared}}
50 []() { return a; }; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{begins here}}
51 [&]() { return a; };
52 [=]() { return a; };
53 [=]() { int* b = &a; }; // expected-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const int *'}}
54 [=]() { return [&]() { return a; }; };
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 ^{ return a; }; };// expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
57 []() { 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}}
58 [=]() { return [&a] { return a; }; }; //
59
60 const int b = 2;
61 []() { return b; };
62
63 union { // expected-note {{declared}}
64 int c;
65 float d;
66 };
67 d = 3;
68 [=]() { return c; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}}
69
70 __block int e; // expected-note 2{{declared}}
71 [&]() { return e; }; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}}
72 [&e]() { return e; }; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}}
73
74 int f[10]; // expected-note {{declared}}
75 [&]() { return f[2]; };
76 (void) ^{ return []() { return f[2]; }; }; // expected-error {{variable 'f' cannot be implicitly captured in a lambda with no capture-default specified}} \
77 // expected-note{{lambda expression begins here}}
78
79 struct G { G(); G(G&); int a; }; // expected-note 6 {{not viable}}
80 G g;
81 [=]() { const G* gg = &g; return gg->a; };
82 [=]() { return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error {{no matching constructor for initialization of 'G'}}
83 (void)^{ return [=]{ const G* gg = &g; return gg->a; }(); }; // expected-error 2 {{no matching constructor for initialization of 'const G'}}
84
85 const int h = a; // expected-note {{declared}}
86 []() { return h; }; // expected-error {{variable 'h' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
87
88 // References can appear in constant expressions if they are initialized by
89 // reference constant expressions.
90 int i;
91 int &ref_i = i; // expected-note {{declared}}
92 [] { 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}}
93
94 static int j;
95 int &ref_j = j;
96 [] { return ref_j; }; // ok
97 }
98 }
99
100 namespace SpecialMembers {
f()101 void f() {
102 auto a = []{}; // expected-note 2{{here}} expected-note 2{{candidate}}
103 decltype(a) b; // expected-error {{no matching constructor}}
104 decltype(a) c = a;
105 decltype(a) d = static_cast<decltype(a)&&>(a);
106 a = a; // expected-error {{copy assignment operator is implicitly deleted}}
107 a = static_cast<decltype(a)&&>(a); // expected-error {{copy assignment operator is implicitly deleted}}
108 }
109 struct P {
110 P(const P&) = delete; //expected-note {{deleted here}} // expected-cxx14-note {{deleted here}}
111 };
112 struct Q {
113 ~Q() = delete; // expected-note {{deleted here}}
114 };
115 struct R {
116 R(const R&) = default;
117 R(R&&) = delete;
118 R &operator=(const R&) = delete;
119 R &operator=(R&&) = delete;
120 };
g(P & p,Q & q,R & r)121 void g(P &p, Q &q, R &r) {
122 // FIXME: The note attached to the second error here is just amazingly bad.
123 auto pp = [p]{}; // expected-error {{deleted constructor}} expected-cxx14-error {{deleted copy constructor of '(lambda}}
124 // expected-cxx14-note@-1 {{copy constructor of '' is implicitly deleted because field '' has a deleted copy constructor}}
125 auto qq = [q]{}; // expected-error {{deleted function}} expected-note {{because}}
126
127 auto a = [r]{}; // expected-note 2{{here}}
128 decltype(a) b = a;
129 decltype(a) c = static_cast<decltype(a)&&>(a); // ok, copies R
130 a = a; // expected-error {{copy assignment operator is implicitly deleted}}
131 a = static_cast<decltype(a)&&>(a); // expected-error {{copy assignment operator is implicitly deleted}}
132 }
133 }
134
135 namespace PR12031 {
136 struct X {
137 template<typename T>
138 X(const T&);
139 ~X();
140 };
141
142 void f(int i, X x);
g()143 void g() {
144 const int v = 10;
145 f(v, [](){});
146 }
147 }
148
149 namespace Array {
150 int &f(int *p);
151 char &f(...);
g()152 void g() {
153 int n = -1;
154 [=] {
155 int arr[n]; // VLA
156 } ();
157
158 const int m = -1;
159 [] {
160 int arr[m]; // expected-error{{negative size}}
161 } ();
162
163 [&] {
164 int arr[m]; // expected-error{{negative size}}
165 } ();
166
167 [=] {
168 int arr[m]; // expected-error{{negative size}}
169 } ();
170
171 [m] {
172 int arr[m]; // expected-error{{negative size}}
173 } ();
174 }
175 }
176
PR12248()177 void PR12248()
178 {
179 unsigned int result = 0;
180 auto l = [&]() { ++result; };
181 }
182
183 namespace ModifyingCapture {
test()184 void test() {
185 int n = 0;
186 [=] {
187 n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
188 };
189 }
190 }
191
192 namespace VariadicPackExpansion {
193 template<typename T, typename U> using Fst = T;
194 template<typename...Ts> bool g(Fst<bool, Ts> ...bools);
f(Ts &&...ts)195 template<typename...Ts> bool f(Ts &&...ts) {
196 return g<Ts...>([&ts] {
197 if (!ts)
198 return false;
199 --ts;
200 return true;
201 } () ...);
202 }
h()203 void h() {
204 int a = 5, b = 2, c = 3;
205 while (f(a, b, c)) {
206 }
207 }
208
209 struct sink {
sinkVariadicPackExpansion::sink210 template<typename...Ts> sink(Ts &&...) {}
211 };
212
local_class()213 template<typename...Ts> void local_class() {
214 sink {
215 [] (Ts t) {
216 struct S : Ts {
217 void f(Ts t) {
218 Ts &that = *this;
219 that = t;
220 }
221 Ts g() { return *this; };
222 };
223 S s;
224 s.f(t);
225 return s;
226 } (Ts()).g() ...
227 };
228 };
229 struct X {}; struct Y {};
230 template void local_class<X, Y>();
231
nested(Ts...ts)232 template<typename...Ts> void nested(Ts ...ts) {
233 f(
234 // Each expansion of this lambda implicitly captures all of 'ts', because
235 // the inner lambda also expands 'ts'.
236 [&] {
237 return ts + [&] { return f(ts...); } ();
238 } () ...
239 );
240 }
241 template void nested(int, int, int);
242
nested2(Ts...ts)243 template<typename...Ts> void nested2(Ts ...ts) { // expected-note 2{{here}}
244 // Capture all 'ts', use only one.
245 f([&ts...] { return ts; } ()...);
246 // Capture each 'ts', use it.
247 f([&ts] { return ts; } ()...);
248 // Capture all 'ts', use all of them.
249 f([&ts...] { return (int)f(ts...); } ());
250 // Capture each 'ts', use all of them. Ill-formed. In more detail:
251 //
252 // We instantiate two lambdas here; the first captures ts$0, the second
253 // captures ts$1. Both of them reference both ts parameters, so both are
254 // ill-formed because ts can't be implicitly captured.
255 //
256 // FIXME: This diagnostic does not explain what's happening. We should
257 // specify which 'ts' we're referring to in its diagnostic name. We should
258 // also say which slice of the pack expansion is being performed in the
259 // instantiation backtrace.
260 f([&ts] { return (int)f(ts...); } ()...); // \
261 // expected-error 2{{'ts' cannot be implicitly captured}} \
262 // expected-note 2{{lambda expression begins here}}
263 }
264 template void nested2(int); // ok
265 template void nested2(int, int); // expected-note {{in instantiation of}}
266 }
267
268 namespace PR13860 {
foo()269 void foo() {
270 auto x = PR13860UndeclaredIdentifier(); // expected-error {{use of undeclared identifier 'PR13860UndeclaredIdentifier'}}
271 auto y = [x]() { };
272 static_assert(sizeof(y), "");
273 }
274 }
275
276 namespace PR13854 {
__anon0846e4884602(void)277 auto l = [](void){};
278 }
279
280 namespace PR14518 {
__anon0846e4884702(void) 281 auto f = [](void) { return __func__; }; // no-warning
282 }
283
284 namespace PR16708 {
__anon0846e4884802() 285 auto L = []() {
286 auto ret = 0;
287 return ret;
288 return 0;
289 };
290 }
291
292 namespace TypeDeduction {
293 struct S {};
f()294 void f() {
295 const S s {};
296 S &&t = [&] { return s; } ();
297 #if __cplusplus > 201103L
298 S &&u = [&] () -> auto { return s; } ();
299 #endif
300 }
301 }
302
303
304 namespace lambdas_in_NSDMIs {
305 template<class T>
306 struct L {
307 T t{};
__anon0846e4884b02lambdas_in_NSDMIs::L308 T t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
309 };
310 L<int> l;
311
312 namespace non_template {
313 struct L {
314 int t = 0;
__anon0846e4884e02(int b) 315 int t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
316 };
317 L l;
318 }
319 }
320
321 // PR18477: don't try to capture 'this' from an NSDMI encountered while parsing
322 // a lambda.
323 namespace NSDMIs_in_lambdas {
324 template<typename T> struct S { int a = 0; int b = a; };
__anon0846e4884f02() 325 void f() { []() { S<int> s; }; }
326
__anon0846e4885002null327 auto x = []{ struct S { int n, m = n; }; };
__anon0846e4885102null328 auto y = [&]{ struct S { int n, m = n; }; }; // expected-error {{non-local lambda expression cannot have a capture-default}}
g()329 void g() { auto z = [&]{ struct S { int n, m = n; }; }; }
330 }
331
332 namespace CaptureIncomplete {
333 struct Incomplete; // expected-note 2{{forward decl}}
334 void g(const Incomplete &a);
f(Incomplete & a)335 void f(Incomplete &a) {
336 (void) [a] {}; // expected-error {{incomplete}}
337 (void) [&a] {};
338
339 (void) [=] { g(a); }; // expected-error {{incomplete}}
340 (void) [&] { f(a); };
341 }
342 }
343
344 namespace CaptureAbstract {
345 struct S {
346 virtual void f() = 0; // expected-note {{unimplemented}}
347 int n = 0;
348 };
349 struct T : S {
TCaptureAbstract::T350 constexpr T() {}
351 void f();
352 };
f()353 void f() {
354 constexpr T t = T();
355 S &s = const_cast<T&>(t);
356 // FIXME: Once we properly compute odr-use per DR712, this should be
357 // accepted (and should not capture 's').
358 [=] { return s.n; }; // expected-error {{abstract}}
359 }
360 }
361
362 namespace PR18128 {
__anon0846e4885802null363 auto l = [=]{}; // expected-error {{non-local lambda expression cannot have a capture-default}}
364
365 struct S {
366 int n;
__anon0846e4885902PR18128::S367 int (*f())[true ? 1 : ([=]{ 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 // expected-cxx14-error@-3 {{a lambda expression may not appear inside of a constant expression}}
__anon0846e4885a02PR18128::S371 int g(int k = ([=]{ return n; }(), 0));
372 // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
373 // expected-error@-2 {{invalid use of non-static data member 'n'}}
374
__anon0846e4885b02PR18128::S375 int a = [=]{ return n; }(); // ok
__anon0846e4885c02PR18128::S376 int b = [=]{ return [=]{ return n; }(); }(); // ok
__anon0846e4885f02null377 int c = []{ int k = 0; return [=]{ return k; }(); }(); // ok
__anon0846e4886002PR18128::S378 int d = []{ return [=]{ return n; }(); }(); // expected-error {{'this' cannot be implicitly captured in this context}}
379 };
380 }
381
382 namespace PR18473 {
f()383 template<typename T> void f() {
384 T t(0);
385 (void) [=]{ int n = t; }; // expected-error {{deleted}}
386 }
387
388 template void f<int>();
389 struct NoCopy {
390 NoCopy(int);
391 NoCopy(const NoCopy &) = delete; // expected-note {{deleted}}
392 operator int() const;
393 };
394 template void f<NoCopy>(); // expected-note {{instantiation}}
395 }
396
PR19249()397 void PR19249() {
398 auto x = [&x]{}; // expected-error {{cannot appear in its own init}}
399 }
400
401 namespace PR20731 {
402 template <class L, int X = sizeof(L)>
403 void Job(L l);
404
405 template <typename... Args>
Logger(Args &&...args)406 void Logger(Args &&... args) {
407 auto len = Invalid_Function((args)...);
408 // expected-error@-1 {{use of undeclared identifier 'Invalid_Function'}}
409 Job([len]() {});
410 }
411
GetMethod()412 void GetMethod() {
413 Logger();
414 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::Logger<>' requested here}}
415 }
416
417 template <typename T>
418 struct A {
419 T t;
420 // expected-error@-1 {{field has incomplete type 'void'}}
421 };
422
423 template <typename F>
g(F f)424 void g(F f) {
425 auto a = A<decltype(f())>{};
426 // expected-note@-1 {{in instantiation of template class 'PR20731::A<void>' requested here}}
427 auto xf = [a, f]() {};
428 int x = sizeof(xf);
429 };
f()430 void f() {
431 g([] {});
432 // expected-note-re@-1 {{in instantiation of function template specialization 'PR20731::g<(lambda at {{.*}}>' requested here}}
433 }
434
435 template <class _Rp> struct function {
436 template <class _Fp>
functionPR20731::function437 function(_Fp) {
438 static_assert(sizeof(_Fp) > 0, "Type must be complete.");
439 }
440 };
441
p(T t)442 template <typename T> void p(T t) {
443 auto l = some_undefined_function(t);
444 // expected-error@-1 {{use of undeclared identifier 'some_undefined_function'}}
445 function<void()>(([l]() {}));
446 }
q()447 void q() { p(0); }
448 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::p<int>' requested here}}
449 }
450
451 namespace lambda_in_default_mem_init {
f()452 template<typename T> void f() {
453 struct S { int n = []{ return 0; }(); };
454 }
455 template void f<int>();
456
g()457 template<typename T> void g() {
458 struct S { int n = [](int n){ return n; }(0); };
459 }
460 template void g<int>();
461 }
462
463 namespace error_in_transform_prototype {
464 template<class T>
f(T t)465 void f(T t) {
466 // expected-error@+2 {{type 'int' cannot be used prior to '::' because it has no members}}
467 // expected-error@+1 {{no member named 'ns' in 'error_in_transform_prototype::S'}}
468 auto x = [](typename T::ns::type &k) {};
469 }
470 class S {};
foo()471 void foo() {
472 f(5); // expected-note {{requested here}}
473 f(S()); // expected-note {{requested here}}
474 }
475 }
476
477 namespace PR21857 {
478 template<typename Fn> struct fun : Fn {
479 fun() = default;
480 using Fn::operator();
481 };
482 template<typename Fn> fun<Fn> wrap(Fn fn);
__anon0846e4886b02()483 auto x = wrap([](){});
484 }
485
486 namespace PR13987 {
487 class Enclosing {
__anon0846e4886c02()488 void Method(char c = []()->char {
489 int d = []()->int {
490 struct LocalClass {
491 int Method() { return 0; }
492 };
493 return 0;
494 }();
495 return d; }()
496 );
497 };
498 }
499
500 namespace PR23860 {
501 template <class> struct A {
__anon0846e4886e02PR23860::A502 void f(int x = []() {
503 struct B {
504 void g() {}
505 };
506 return 0;
507 }());
508 };
509
main()510 int main() {
511 }
512
513 A<int> a;
514 }
515
516 // rdar://22032373
517 namespace rdar22032373 {
foo()518 void foo() {
519 auto blk = [](bool b) {
520 if (b)
521 return undeclared_error; // expected-error {{use of undeclared identifier}}
522 return 0;
523 };
524 auto bar = []() {
525 return undef(); // expected-error {{use of undeclared identifier}}
526 return 0; // verify no init_conversion_failed diagnostic emitted.
527 };
528 }
529 }
530
531 namespace nested_lambda {
532 template <int N>
533 class S {};
534
foo()535 void foo() {
536 const int num = 18;
537 auto outer = []() {
538 auto inner = [](S<num> &X) {};
539 };
540 }
541 }
542
543 namespace PR27994 {
544 struct A { template <class T> A(T); };
545
546 template <class T>
547 struct B {
548 int x;
__anon0846e4887302PR27994::B549 A a = [&] { int y = x; };
__anon0846e4887502null550 A b = [&] { [&] { [&] { int y = x; }; }; };
__anon0846e4887702PR27994::B551 A d = [&](auto param) { int y = x; };
__anon0846e4887902null552 A e = [&](auto param) { [&] { [&](auto param2) { int y = x; }; }; };
553 };
554
555 B<int> b;
556
557 template <class T> struct C {
558 struct D {
559 int x;
__anon0846e4887b02PR27994::C::D560 A f = [&] { int y = x; };
561 };
562 };
563
func()564 int func() {
565 C<int> a;
566 decltype(a)::D b;
567 }
568 }
569
570 namespace PR30566 {
571 int name1; // expected-note {{'name1' declared here}}
572
573 struct S1 {
574 template<class T>
S1PR30566::S1575 S1(T t) { s = sizeof(t); }
576 int s;
577 };
578
foo1()579 void foo1() {
580 auto s0 = S1{[name=]() {}}; // expected-error 2 {{expected expression}}
581 auto s1 = S1{[name=name]() {}}; // expected-error {{use of undeclared identifier 'name'; did you mean 'name1'?}}
582 }
583 }
584
585 namespace PR25627_dont_odr_use_local_consts {
586
587 template<int> struct X {};
588
foo()589 void foo() {
590 const int N = 10;
591 (void) [] { X<N> x; };
592 }
593 }
594
595 namespace ConversionOperatorDoesNotHaveDeducedReturnType {
__anon0846e4887f02(int)596 auto x = [](int){};
__anon0846e4888002(auto &v) 597 auto y = [](auto &v) -> void { v.n = 0; };
598 using T = decltype(x);
599 using U = decltype(y);
600 using ExpectedTypeT = void (*)(int);
601 template<typename T>
602 using ExpectedTypeU = void (*)(T&);
603
604 struct X {
605 #if __cplusplus > 201402L
606 friend constexpr auto T::operator()(int) const;
607 friend constexpr T::operator ExpectedTypeT() const noexcept;
608
609 template<typename T>
610 friend constexpr void U::operator()(T&) const;
611 // FIXME: This should not match; the return type is specified as behaving
612 // "as if it were a decltype-specifier denoting the return type of
613 // [operator()]", which is not equivalent to this alias template.
614 template<typename T>
615 friend constexpr U::operator ExpectedTypeU<T>() const noexcept;
616 #else
617 friend auto T::operator()(int) const;
618 friend T::operator ExpectedTypeT() const;
619
620 template<typename T>
621 friend void U::operator()(T&) const;
622 // FIXME: This should not match, as above.
623 template<typename T>
624 friend U::operator ExpectedTypeU<T>() const;
625 #endif
626
627 private:
628 int n;
629 };
630
631 // Should be OK: lambda's call operator is a friend.
use(X & x)632 void use(X &x) { y(x); }
633
634 // This used to crash in return type deduction for the conversion opreator.
fConversionOperatorDoesNotHaveDeducedReturnType::A635 struct A { int n; void f() { +[](decltype(n)) {}; } };
636 }
637
638 namespace TypoCorrection {
639 template <typename T> struct X {};
640 // expected-note@-1 {{template parameter is declared here}}
641
642 template <typename T>
Run(const int & points)643 void Run(const int& points) {
644 // expected-note@-1 {{'points' declared here}}
645 auto outer_lambda = []() {
646 auto inner_lambda = [](const X<Points>&) {};
647 // expected-error@-1 {{use of undeclared identifier 'Points'; did you mean 'points'?}}
648 // expected-error@-2 {{template argument for template type parameter must be a type}}
649 };
650 }
651 }
652
operator_parens()653 void operator_parens() {
654 [&](int x){ operator()(); }(0); // expected-error {{undeclared 'operator()'}}
655 }
656
657 namespace captured_name {
Test()658 void Test() {
659 union { // expected-note {{'' declared here}}
660 int i;
661 };
662 [] { return i; }; // expected-error {{variable '' cannot be implicitly captured in a lambda with no capture-default specified}}
663 // expected-note@-1 {{lambda expression begins here}}
664
665 }
666 };
667