Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / lambda-expressions.cpp
blob1797eef320b8659934c04ac86e0a53f61e00511f
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 -verify -ast-dump -fblocks %s | FileCheck %s
4 namespace std { class type_info; };
6 namespace ExplicitCapture {
7 class C {
8 int Member;
10 static void Overload(int);
11 void Overload();
12 virtual C& Overload(float);
14 void ImplicitThisCapture() {
15 []() { (void)Member; }; // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}}
16 const int var = []() {(void)Member; return 0; }(); // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}}
17 [&](){(void)Member;};
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}} expected-note {{explicitly capture 'this'}}
24 []{(void)typeid(Overload());};
25 [] { (void)typeid(Overload(.5f)); }; // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}}
29 void f() {
30 [this] () {}; // expected-error {{'this' cannot be captured in this context}}
34 namespace ReturnDeduction {
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; };
47 namespace ImplicitCapture {
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}} expected-note 2 {{capture 'a' by}} expected-note 2 {{default capture by}}
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}} expected-note 2 {{capture 'a' by}} expected-note 2 {{default capture by}}
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}} expected-note 2 {{capture 'a' by}} expected-note 2 {{default capture by}}
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}} expected-note 4 {{capture 'a' by}} expected-note 4 {{default capture by}}
58 [=]() { return [&a] { return a; }; }; //
60 const int b = 2;
61 []() { return b; };
63 union { // expected-note {{declared}}
64 int c;
65 float d;
67 d = 3;
68 [=]() { return c; }; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}}
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}}
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}} expected-note 2 {{capture 'f' by}} expected-note 2 {{default capture by}}
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'}}
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}} expected-note 2 {{capture 'h' by}} expected-note 2 {{default capture by}}
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}} expected-note 2 {{capture 'ref_i' by}} expected-note 2 {{default capture by}}
94 static int j;
95 int &ref_j = j;
96 [] { return ref_j; }; // ok
100 namespace SpecialMembers {
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}}
109 struct P {
110 P(const P&) = delete; //expected-note {{deleted here}} // expected-cxx14-note {{deleted here}}
112 struct Q {
113 ~Q() = delete; // expected-note {{deleted here}}
115 struct R {
116 R(const R&) = default;
117 R(R&&) = delete;
118 R &operator=(const R&) = delete;
119 R &operator=(R&&) = delete;
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-re@-1 {{copy constructor of '(lambda at {{.*}})' is implicitly deleted because field '' has a deleted copy constructor}}
125 auto qq = [q]{}; // expected-error {{deleted function}} expected-note {{because}}
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}}
135 namespace PR12031 {
136 struct X {
137 template<typename T>
138 X(const T&);
139 ~X();
142 void f(int i, X x);
143 void g() {
144 const int v = 10;
145 f(v, [](){});
149 namespace Array {
150 int &f(int *p);
151 char &f(...);
152 void g() {
153 int n = -1; // expected-note {{declared here}}
154 [=] {
155 int arr[n]; // expected-warning {{variable length arrays in C++ are a Clang extension}} \
156 expected-note {{read of non-const variable 'n' is not allowed in a constant expression}}
157 } ();
159 const int m = -1;
160 [] {
161 int arr[m]; // expected-error{{negative size}}
162 } ();
164 [&] {
165 int arr[m]; // expected-error{{negative size}}
166 } ();
168 [=] {
169 int arr[m]; // expected-error{{negative size}}
170 } ();
172 [m] {
173 int arr[m]; // expected-error{{negative size}}
174 } ();
178 void PR12248()
180 unsigned int result = 0;
181 auto l = [&]() { ++result; };
184 namespace ModifyingCapture {
185 void test() {
186 int n = 0;
187 [=] {
188 n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}}
193 namespace VariadicPackExpansion {
194 template<typename T, typename U> using Fst = T;
195 template<typename...Ts> bool g(Fst<bool, Ts> ...bools);
196 template<typename...Ts> bool f(Ts &&...ts) {
197 return g<Ts...>([&ts] {
198 if (!ts)
199 return false;
200 --ts;
201 return true;
202 } () ...);
204 void h() {
205 int a = 5, b = 2, c = 3;
206 while (f(a, b, c)) {
210 struct sink {
211 template<typename...Ts> sink(Ts &&...) {}
214 template<typename...Ts> void local_class() {
215 sink {
216 [] (Ts t) {
217 struct S : Ts {
218 void f(Ts t) {
219 Ts &that = *this;
220 that = t;
222 Ts g() { return *this; };
224 S s;
225 s.f(t);
226 return s;
227 } (Ts()).g() ...
230 struct X {}; struct Y {};
231 template void local_class<X, Y>();
233 template<typename...Ts> void nested(Ts ...ts) {
235 // Each expansion of this lambda implicitly captures all of 'ts', because
236 // the inner lambda also expands 'ts'.
237 [&] {
238 return ts + [&] { return f(ts...); } ();
239 } () ...
242 template void nested(int, int, int);
244 template<typename...Ts> void nested2(Ts ...ts) { // expected-note 2{{here}}
245 // Capture all 'ts', use only one.
246 f([&ts...] { return ts; } ()...);
247 // Capture each 'ts', use it.
248 f([&ts] { return ts; } ()...);
249 // Capture all 'ts', use all of them.
250 f([&ts...] { return (int)f(ts...); } ());
251 // Capture each 'ts', use all of them. Ill-formed. In more detail:
253 // We instantiate two lambdas here; the first captures ts$0, the second
254 // captures ts$1. Both of them reference both ts parameters, so both are
255 // ill-formed because ts can't be implicitly captured.
257 // FIXME: This diagnostic does not explain what's happening. We should
258 // specify which 'ts' we're referring to in its diagnostic name. We should
259 // also say which slice of the pack expansion is being performed in the
260 // instantiation backtrace.
261 f([&ts] { return (int)f(ts...); } ()...); // \
262 // expected-error 2{{'ts' cannot be implicitly captured}} \
263 // expected-note 2{{lambda expression begins here}} \
264 // expected-note 4 {{capture 'ts' by}} \
265 // expected-note 2 {{while substituting into a lambda}}
267 template void nested2(int); // ok
268 template void nested2(int, int); // expected-note 2 {{in instantiation of}}
271 namespace PR13860 {
272 void foo() {
273 auto x = PR13860UndeclaredIdentifier(); // expected-error {{use of undeclared identifier 'PR13860UndeclaredIdentifier'}}
274 auto y = [x]() { };
275 static_assert(sizeof(y), "");
279 namespace PR13854 {
280 auto l = [](void){};
283 namespace PR14518 {
284 auto f = [](void) { return __func__; }; // no-warning
287 namespace PR16708 {
288 auto L = []() {
289 auto ret = 0;
290 return ret;
291 return 0;
295 namespace TypeDeduction {
296 struct S {};
297 void f() {
298 const S s {};
299 S &&t = [&] { return s; } ();
300 #if __cplusplus > 201103L
301 S &&u = [&] () -> auto { return s; } ();
302 #endif
307 namespace lambdas_in_NSDMIs {
308 template<class T>
309 struct L {
310 T t{};
311 T t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
313 L<int> l;
315 namespace non_template {
316 struct L {
317 int t = 0;
318 int t2 = ([](int a) { return [](int b) { return b; };})(t)(t);
320 L l;
324 // PR18477: don't try to capture 'this' from an NSDMI encountered while parsing
325 // a lambda.
326 namespace NSDMIs_in_lambdas {
327 template<typename T> struct S { int a = 0; int b = a; };
328 void f() { []() { S<int> s; }; }
330 auto x = []{ struct S { int n, m = n; }; };
331 auto y = [&]{ struct S { int n, m = n; }; }; // expected-error {{non-local lambda expression cannot have a capture-default}}
332 void g() { auto z = [&]{ struct S { int n, m = n; }; }; }
335 namespace CaptureIncomplete {
336 struct Incomplete; // expected-note 2{{forward decl}}
337 void g(const Incomplete &a);
338 void f(Incomplete &a) {
339 (void) [a] {}; // expected-error {{incomplete}}
340 (void) [&a] {};
342 (void) [=] { g(a); }; // expected-error {{incomplete}}
343 (void) [&] { f(a); };
347 namespace CaptureAbstract {
348 struct S {
349 virtual void f() = 0; // expected-note {{unimplemented}}
350 int n = 0;
352 struct T : S {
353 constexpr T() {}
354 void f();
356 void f() {
357 constexpr T t = T();
358 S &s = const_cast<T&>(t);
359 // FIXME: Once we properly compute odr-use per DR712, this should be
360 // accepted (and should not capture 's').
361 [=] { return s.n; }; // expected-error {{abstract}}
365 namespace PR18128 {
366 auto l = [=]{}; // expected-error {{non-local lambda expression cannot have a capture-default}}
368 struct S {
369 int n;
370 int (*f())[true ? 1 : ([=]{ return n; }(), 0)];
371 // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
372 // expected-error@-2 {{invalid use of non-static data member 'n'}}
373 // expected-cxx14-error@-3 {{a lambda expression may not appear inside of a constant expression}}
374 int g(int k = ([=]{ return n; }(), 0));
375 // expected-error@-1 {{non-local lambda expression cannot have a capture-default}}
376 // expected-error@-2 {{invalid use of non-static data member 'n'}}
378 int a = [=]{ return n; }(); // ok
379 int b = [=]{ return [=]{ return n; }(); }(); // ok
380 int c = []{ int k = 0; return [=]{ return k; }(); }(); // ok
381 int d = [] { return [=] { return n; }(); }(); // expected-error {{'this' cannot be implicitly captured in this context}} expected-note {{explicitly capture 'this'}}
385 namespace gh67687 {
386 struct S {
387 int n;
388 int a = (4, []() { return n; }()); // expected-error {{'this' cannot be implicitly captured in this context}} \
389 // expected-note {{explicitly capture 'this'}}
393 namespace PR18473 {
394 template<typename T> void f() {
395 T t(0);
396 (void) [=]{ int n = t; }; // expected-error {{deleted}} expected-note {{while substituting into a lambda}}
399 template void f<int>();
400 struct NoCopy {
401 NoCopy(int);
402 NoCopy(const NoCopy &) = delete; // expected-note {{deleted}}
403 operator int() const;
405 template void f<NoCopy>(); // expected-note {{instantiation}}
408 void PR19249() {
409 auto x = [&x]{}; // expected-error {{cannot appear in its own init}}
412 namespace PR20731 {
413 template <class L, int X = sizeof(L)>
414 void Job(L l);
416 template <typename... Args>
417 void Logger(Args &&... args) {
418 auto len = Invalid_Function((args)...);
419 // expected-error@-1 {{use of undeclared identifier 'Invalid_Function'}}
420 Job([len]() {});
423 void GetMethod() {
424 Logger();
425 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::Logger<>' requested here}}
428 template <typename T>
429 struct A {
430 T t;
431 // expected-error@-1 {{field has incomplete type 'void'}}
434 template <typename F>
435 void g(F f) {
436 auto a = A<decltype(f())>{};
437 // expected-note@-1 {{in instantiation of template class 'PR20731::A<void>' requested here}}
438 auto xf = [a, f]() {};
439 int x = sizeof(xf);
441 void f() {
442 g([] {});
443 // expected-note-re@-1 {{in instantiation of function template specialization 'PR20731::g<(lambda at {{.*}}>' requested here}}
446 template <class _Rp> struct function {
447 template <class _Fp>
448 function(_Fp) {
449 static_assert(sizeof(_Fp) > 0, "Type must be complete.");
453 template <typename T> void p(T t) {
454 auto l = some_undefined_function(t);
455 // expected-error@-1 {{use of undeclared identifier 'some_undefined_function'}}
456 function<void()>(([l]() {}));
458 void q() { p(0); }
459 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::p<int>' requested here}}
462 namespace lambda_in_default_mem_init {
463 template<typename T> void f() {
464 struct S { int n = []{ return 0; }(); };
466 template void f<int>();
468 template<typename T> void g() {
469 struct S { int n = [](int n){ return n; }(0); };
471 template void g<int>();
474 namespace error_in_transform_prototype {
475 template<class T>
476 void f(T t) {
477 // expected-error@+2 {{type 'int' cannot be used prior to '::' because it has no members}}
478 // expected-error@+1 {{no member named 'ns' in 'error_in_transform_prototype::S'}}
479 auto x = [](typename T::ns::type &k) {}; // expected-note 2 {{while substituting into a lambda}}
481 class S {};
482 void foo() {
483 f(5); // expected-note {{requested here}}
484 f(S()); // expected-note {{requested here}}
488 namespace PR21857 {
489 template<typename Fn> struct fun : Fn {
490 fun() = default;
491 using Fn::operator();
493 template<typename Fn> fun<Fn> wrap(Fn fn);
494 auto x = wrap([](){});
497 namespace PR13987 {
498 class Enclosing {
499 void Method(char c = []()->char {
500 int d = []()->int {
501 struct LocalClass {
502 int Method() { return 0; }
504 return 0;
505 }();
506 return d; }()
511 namespace PR23860 {
512 template <class> struct A {
513 void f(int x = []() {
514 struct B {
515 void g() {}
517 return 0;
518 }());
521 int main() {
524 A<int> a;
527 namespace rdar22032373 {
528 void foo() {
529 auto blk = [](bool b) {
530 if (b)
531 return undeclared_error; // expected-error {{use of undeclared identifier}}
532 return 0;
534 auto bar = []() {
535 return undef(); // expected-error {{use of undeclared identifier}}
536 return 0; // verify no init_conversion_failed diagnostic emitted.
541 namespace nested_lambda {
542 template <int N>
543 class S {};
545 void foo() {
546 const int num = 18;
547 auto outer = []() {
548 auto inner = [](S<num> &X) {};
553 namespace PR27994 {
554 struct A { template <class T> A(T); };
556 template <class T>
557 struct B {
558 int x;
559 A a = [&] { int y = x; };
560 A b = [&] { [&] { [&] { int y = x; }; }; };
561 A d = [&](auto param) { int y = x; };
562 A e = [&](auto param) { [&] { [&](auto param2) { int y = x; }; }; };
565 B<int> b;
567 template <class T> struct C {
568 struct D {
569 int x;
570 A f = [&] { int y = x; };
574 int func() {
575 C<int> a;
576 decltype(a)::D b;
580 namespace PR30566 {
581 int name1; // expected-note {{'name1' declared here}}
583 struct S1 {
584 template<class T>
585 S1(T t) { s = sizeof(t); }
586 int s;
589 void foo1() {
590 auto s0 = S1{[name=]() {}}; // expected-error 2 {{expected expression}}
591 auto s1 = S1{[name=name]() {}}; // expected-error {{use of undeclared identifier 'name'; did you mean 'name1'?}}
595 namespace PR25627_dont_odr_use_local_consts {
597 template<int> struct X {};
599 void foo() {
600 const int N = 10;
601 (void) [] { X<N> x; };
605 namespace ConversionOperatorDoesNotHaveDeducedReturnType {
606 auto x = [](int){};
607 auto y = [](auto &v) -> void { v.n = 0; };
608 using T = decltype(x);
609 using U = decltype(y);
610 using ExpectedTypeT = void (*)(int);
611 template<typename T>
612 using ExpectedTypeU = void (*)(T&);
614 struct X {
615 #if __cplusplus > 201402L
616 friend constexpr auto T::operator()(int) const;
617 friend constexpr T::operator ExpectedTypeT() const noexcept;
619 template<typename T>
620 friend constexpr void U::operator()(T&) const;
621 // FIXME: This should not match; the return type is specified as behaving
622 // "as if it were a decltype-specifier denoting the return type of
623 // [operator()]", which is not equivalent to this alias template.
624 template<typename T>
625 friend constexpr U::operator ExpectedTypeU<T>() const noexcept;
626 #else
627 friend auto T::operator()(int) const;
628 friend T::operator ExpectedTypeT() const;
630 template<typename T>
631 friend void U::operator()(T&) const;
632 // FIXME: This should not match, as above.
633 template<typename T>
634 friend U::operator ExpectedTypeU<T>() const;
635 #endif
637 private:
638 int n;
641 // Should be OK: lambda's call operator is a friend.
642 void use(X &x) { y(x); }
644 // This used to crash in return type deduction for the conversion opreator.
645 struct A { int n; void f() { +[](decltype(n)) {}; } };
648 namespace TypoCorrection {
649 template <typename T> struct X {};
650 // expected-note@-1 {{template parameter is declared here}}
652 template <typename T>
653 void Run(const int& points) {
654 // expected-note@-1 {{'points' declared here}}
655 auto outer_lambda = []() {
656 auto inner_lambda = [](const X<Points>&) {};
657 // expected-error@-1 {{use of undeclared identifier 'Points'; did you mean 'points'?}}
658 // expected-error@-2 {{template argument for template type parameter must be a type}}
663 void operator_parens() {
664 [&](int x){ operator()(); }(0); // expected-error {{undeclared 'operator()'}}
667 namespace captured_name {
668 void Test() {
669 union { // expected-note {{'' declared here}}
670 int i;
672 [] { return i; }; // expected-error {{variable '' cannot be implicitly captured in a lambda with no capture-default specified}}
673 // expected-note@-1 {{lambda expression begins here}}
674 // expected-note@-2 2 {{default capture by}}
678 namespace GH60518 {
679 // Lambdas should not try to capture
680 // function parameters that are used in enable_if
681 struct StringLiteral {
682 template <int N>
683 StringLiteral(const char (&array)[N])
684 __attribute__((enable_if(__builtin_strlen(array) == 2,
685 "invalid string literal")));
688 namespace cpp_attribute {
689 struct StringLiteral {
690 template <int N>
691 StringLiteral(const char (&array)[N]) [[clang::annotate_type("test", array)]];
695 void Func1() {
696 [[maybe_unused]] auto y = [&](decltype(StringLiteral("xx"))) {};
697 [[maybe_unused]] auto z = [&](decltype(cpp_attribute::StringLiteral("xx"))) {};
702 #if __cplusplus > 201402L
703 namespace GH60936 {
704 struct S {
705 int i;
706 // `&i` in default initializer causes implicit `this` access.
707 int *p = &i;
710 static_assert([]() constexpr {
711 S r = S{2};
712 return r.p != nullptr;
713 }());
714 } // namespace GH60936
715 #endif
717 // Call operator attributes refering to a variable should
718 // be properly handled after D124351
719 constexpr int i = 2;
720 void foo() {
721 (void)[=][[gnu::aligned(i)]] () {}; // expected-warning{{C++23 extension}}
722 // CHECK: AlignedAttr
723 // CHECK-NEXT: ConstantExpr
724 // CHECK-NEXT: value: Int 2
727 void GH48527() {
728 auto a = []()__attribute__((b(({ return 0; })))){}; // expected-warning {{unknown attribute 'b' ignored}}
731 void GH67492() {
732 constexpr auto test = 42;
733 auto lambda = (test, []() noexcept(true) {});