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