1 // RUN: %clang_cc1 -std=c++14 -Wno-unused-value -fsyntax-only -verify -fblocks %s
3 namespace std { class type_info; };
5 namespace ExplicitCapture {
9 static void Overload(int);
11 virtual C& Overload(float);
13 void ImplicitThisCapture() {
14 [](){(void)Member;}; // expected-error {{'this' cannot be implicitly captured in this context}}
17 [this](){(void)Member;};
19 []{[this]{};};// expected-error {{'this' cannot be implicitly captured in this context}}
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}}
28 [this] () {}; // expected-error {{'this' cannot be captured in this context}}
32 namespace ReturnDeduction {
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; };
45 namespace ImplicitCapture {
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}}
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; }; }; //
61 union { // expected-note {{declared}}
66 [=]() { return c; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}}
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}}
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}}
77 struct G { G(); G(G&); int a; }; // expected-note 6 {{not viable}}
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'}}
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}}
86 // References can appear in constant expressions if they are initialized by
87 // reference constant expressions.
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}}
94 [] { return ref_j; }; // ok
98 namespace SpecialMembers {
100 auto a = []{}; // expected-note 2{{here}} expected-note 2{{candidate}}
101 decltype(a) b; // expected-error {{no matching constructor}}
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}}
108 P(const P&) = delete; // expected-note {{deleted here}}
111 ~Q() = delete; // expected-note {{deleted here}}
114 R(const R&) = default;
116 R &operator=(const R&) = delete;
117 R &operator=(R&&) = delete;
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}}
123 auto a = [r]{}; // expected-note 2{{here}}
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}}
156 int arr[m]; // expected-error{{negative size}}
160 int arr[m]; // expected-error{{negative size}}
164 int arr[m]; // expected-error{{negative size}}
168 int arr[m]; // expected-error{{negative size}}
175 unsigned int result = 0;
176 auto l = [&]() { ++result; };
179 namespace ModifyingCapture {
183 n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
188 namespace VariadicPackExpansion {
189 template<typename T, typename U> using Fst = T;
190 template<typename...Ts> bool g(Fst<bool, Ts> ...bools);
191 template<typename...Ts> bool f(Ts &&...ts) {
192 return g<Ts...>([&ts] {
200 int a = 5, b = 2, c = 3;
206 template<typename...Ts> sink(Ts &&...) {}
209 template<typename...Ts> void local_class() {
217 Ts g() { return *this; };
225 struct X {}; struct Y {};
226 template void local_class<X, Y>();
228 template<typename...Ts> void nested(Ts ...ts) {
230 // Each expansion of this lambda implicitly captures all of 'ts', because
231 // the inner lambda also expands 'ts'.
233 return ts + [&] { return f(ts...); } ();
237 template void nested(int, int, int);
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:
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.
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}}
260 template void nested2(int); // ok
261 template void nested2(int, int); // expected-note {{in instantiation of}}
266 auto x = PR13860UndeclaredIdentifier(); // expected-error {{use of undeclared identifier 'PR13860UndeclaredIdentifier'}}
268 static_assert(sizeof(y), "");
277 auto f = [](void) { return __func__; }; // no-warning
288 namespace TypeDeduction {
292 S &&t = [&] { return s; } ();
293 #if __cplusplus > 201103L
294 S &&u = [&] () -> auto { return s; } ();
300 namespace lambdas_in_NSDMIs {
304 T t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
308 namespace non_template {
311 int t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
317 // PR18477: don't try to capture 'this' from an NSDMI encountered while parsing
319 namespace NSDMIs_in_lambdas {
320 template<typename T> struct S { int a = 0; int b = a; };
321 void f() { []() { S<int> s; }; }
323 auto x = []{ struct S { int n, m = n; }; };
324 auto y = [&]{ struct S { int n, m = n; }; }; // expected-error {{non-local lambda expression cannot have a capture-default}}
325 void g() { auto z = [&]{ struct S { int n, m = n; }; }; }
328 namespace CaptureIncomplete {
329 struct Incomplete; // expected-note 2{{forward decl}}
330 void g(const Incomplete &a);
331 void f(Incomplete &a) {
332 (void) [a] {}; // expected-error {{incomplete}}
335 (void) [=] { g(a); }; // expected-error {{incomplete}}
336 (void) [&] { f(a); };
340 namespace CaptureAbstract {
342 virtual void f() = 0; // expected-note {{unimplemented}}
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}}
359 auto l = [=]{}; // expected-error {{non-local lambda expression cannot have a capture-default}}
363 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}}
367 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'}}
371 int a = [=]{ return n; }(); // ok
372 int b = [=]{ return [=]{ return n; }(); }(); // ok
373 int c = []{ int k = 0; return [=]{ return k; }(); }(); // ok
374 int d = []{ return [=]{ return n; }(); }(); // expected-error {{'this' cannot be implicitly captured in this context}}
379 template<typename T> void f() {
381 (void) [=]{ int n = t; }; // expected-error {{deleted}}
384 template void f<int>();
387 NoCopy(const NoCopy &) = delete; // expected-note {{deleted}}
388 operator int() const;
390 template void f<NoCopy>(); // expected-note {{instantiation}}
394 auto x = [&x]{}; // expected-error {{cannot appear in its own init}}
398 template <class L, int X = sizeof(L)>
401 template <typename... Args>
402 void Logger(Args &&... args) {
403 auto len = Invalid_Function((args)...);
404 // expected-error@-1 {{use of undeclared identifier 'Invalid_Function'}}
410 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::Logger<>' requested here}}
413 template <typename T>
416 // expected-error@-1 {{field has incomplete type 'void'}}
419 template <typename 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]() {};
428 // expected-note-re@-1 {{in instantiation of function template specialization 'PR20731::g<(lambda at {{.*}}>' requested here}}
431 template <class _Rp> struct function {
434 static_assert(sizeof(_Fp) > 0, "Type must be complete.");
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]() {}));
444 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::p<int>' requested here}}
447 namespace lambda_in_default_mem_init {
448 template<typename T> void f() {
449 struct S { int n = []{ return 0; }(); };
451 template void f<int>();
453 template<typename T> void g() {
454 struct S { int n = [](int n){ return n; }(0); };
456 template void g<int>();
459 namespace error_in_transform_prototype {
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) {};
468 f(5); // expected-note {{requested here}}
469 f(S()); // expected-note {{requested here}}
474 template<typename Fn> struct fun : Fn {
476 using Fn::operator();
478 template<typename Fn> fun<Fn> wrap(Fn fn);
479 auto x = wrap([](){});
484 void Method(char c = []()->char {
487 int Method() { return 0; }
497 template <class> struct A {
498 void f(int x = []() {
513 namespace rdar22032373 {
515 auto blk = [](bool b) {
517 return undeclared_error; // expected-error {{use of undeclared identifier}}
523 namespace nested_lambda {
530 auto inner = [](S<num> &X) {};
536 struct A { template <class T> A(T); };
541 A a = [&] { int y = x; };
542 A b = [&] { [&] { [&] { int y = x; }; }; };
543 A d = [&](auto param) { int y = x; };
544 A e = [&](auto param) { [&] { [&](auto param2) { int y = x; }; }; };
549 template <class T> struct C {
552 A f = [&] { int y = x; };
563 int name1; // expected-note {{'name1' declared here}}
567 S1(T t) { s = sizeof(t); }
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'?}}
577 namespace PR25627_dont_odr_use_local_consts {
579 template<int> struct X {};
583 (void) [] { X<N> x; };
587 namespace ConversionOperatorDoesNotHaveDeducedReturnType {
589 auto y = [](auto) -> void {};
590 using T = decltype(x);
591 using U = decltype(y);
592 using ExpectedTypeT = void (*)(int);
594 using ExpectedTypeU = void (*)(T);
597 friend T::operator ExpectedTypeT() const;
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
605 friend U::operator ExpectedTypeU<T>() const;
608 // This used to crash in return type deduction for the conversion opreator.
609 struct A { int n; void f() { +[](decltype(n)) {}; } };
612 namespace TypoCorrection {
613 template <typename T> struct X {};
614 // expected-note@-1 {{template parameter is declared here}}
616 template <typename T>
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}}