Debian Codegraph @

clang/test/SemaCXX/lambda-expressions.cpp

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