[Reland][Runtimes] Merge 'compile_commands.json' files from runtimes build (#116303)
[llvm-project.git] / clang / test / SemaCXX / cxx1y-generic-lambdas-capturing.cpp
blobb234c541a20398dfea768d55aed8852003652d0f
1 // RUN: %clang_cc1 -std=c++1y -verify -fblocks -emit-llvm-only %s
2 // RUN: %clang_cc1 -std=c++2a -verify -verify=expected-cxx2a -fblocks -emit-llvm-only -Wno-deprecated-this-capture %s
3 // RUN: %clang_cc1 -std=c++1y -verify -fblocks -emit-llvm-only -triple i386-windows-pc %s
4 // RUN: %clang_cc1 -std=c++2a -verify -verify=expected-cxx2a -fblocks -emit-llvm-only -triple i386-windows-pc -Wno-deprecated-this-capture %s
5 // DONTRUNYET: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fdelayed-template-parsing %s -DDELAYED_TEMPLATE_PARSING
6 // DONTRUNYET: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fms-extensions %s -DMS_EXTENSIONS
7 // DONTRUNYET: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fdelayed-template-parsing -fms-extensions %s -DMS_EXTENSIONS -DDELAYED_TEMPLATE_PARSING
9 constexpr int ODRUSE_SZ = sizeof(char);
11 template<class T, int N>
12 void f(T, const int (&)[N]) { }
14 template<class T>
15 void f(const T&, const int (&)[ODRUSE_SZ]) { }
17 #define DEFINE_SELECTOR(x) \
18 int selector_ ## x[sizeof(x) == ODRUSE_SZ ? ODRUSE_SZ : ODRUSE_SZ + 5]
20 #define F_CALL(x, a) f(x, selector_ ## a)
22 // This is a risky assumption, because if an empty class gets captured by value
23 // the lambda's size will still be '1'
24 #define ASSERT_NO_CAPTURES(L) static_assert(sizeof(L) == 1, "size of closure with no captures must be 1")
25 #define ASSERT_CLOSURE_SIZE_EXACT(L, N) static_assert(sizeof(L) == (N), "size of closure must be " #N)
26 #define ASSERT_CLOSURE_SIZE(L, N) static_assert(sizeof(L) >= (N), "size of closure must be >=" #N)
29 namespace sample {
30 struct X {
31 int i;
32 X(int i) : i(i) { }
36 namespace test_transformations_in_templates {
37 template<class T> void foo(T t) {
38 auto L = [](auto a) { return a; };
40 template<class T> void foo2(T t) {
41 auto L = [](auto a) -> void {
42 auto M = [](char b) -> void {
43 auto N = [](auto c) -> void {
44 int selector[sizeof(c) == 1 ?
45 (sizeof(b) == 1 ? 1 : 2)
46 : 2
47 ]{};
48 };
49 N('a');
50 };
52 L(3.14);
55 void doit() {
56 foo(3);
57 foo('a');
58 foo2('A');
62 namespace test_return_type_deduction {
64 void doit() {
66 auto L = [](auto a, auto b) {
67 if ( a > b ) return a;
68 return b;
70 L(2, 4);
72 auto L2 = [](auto a, int i) {
73 return a + i;
75 L2(3.14, 2);
78 int a; //expected-note{{declared here}}
79 auto B = []() { return ^{ return a; }; }; //expected-error{{cannot be implicitly capture}}\
80 //expected-note{{begins here}}\
81 //expected-note 2 {{capture 'a' by}}\
82 //expected-note 2 {{default capture by}}
83 //[](){ return ({int b = 5; return 'c'; 'x';}); };
85 //auto X = ^{ return a; };
87 //auto Y = []() -> auto { return 3; return 'c'; };
93 namespace test_no_capture{
94 void doit() {
95 const int x = 10; //expected-note{{declared here}}
97 // should not capture 'x' - variable undergoes lvalue-to-rvalue
98 auto L = [=](auto a) {
99 int y = x;
100 return a + y;
102 ASSERT_NO_CAPTURES(L);
105 // should not capture 'x' - even though certain instantiations require
106 auto L = [](auto a) { //expected-note{{begins here}} expected-note 2 {{capture 'x' by}} expected-note 2 {{default capture by}}
107 DEFINE_SELECTOR(a);
108 F_CALL(x, a); //expected-error{{'x' cannot be implicitly captured}}
110 ASSERT_NO_CAPTURES(L);
111 L('s'); //expected-note{{in instantiation of}}
114 // Does not capture because no default capture in inner most lambda 'b'
115 auto L = [=](auto a) {
116 return [=](int p) {
117 return [](auto b) {
118 DEFINE_SELECTOR(a);
119 F_CALL(x, a);
120 return 0;
124 ASSERT_NO_CAPTURES(L);
126 } // doit
127 } // namespace
129 namespace test_capture_of_potentially_evaluated_expression {
130 void doit() {
131 const int x = 5;
133 auto L = [=](auto a) {
134 DEFINE_SELECTOR(a);
135 F_CALL(x, a);
137 static_assert(sizeof(L) == 4, "Must be captured");
140 int j = 0; //expected-note{{declared}}
141 auto L = [](auto a) { //expected-note{{begins here}} expected-note 2 {{capture 'j' by}} expected-note 2 {{default capture by}}
142 return j + 1; //expected-error{{cannot be implicitly captured}}
146 const int x = 10;
147 auto L = [](auto a) {
148 //const int y = 20;
149 return [](int p) {
150 return [](auto b) {
151 DEFINE_SELECTOR(a);
152 F_CALL(x, a);
153 return 0;
157 auto M = L(3);
158 auto N = M(5);
162 { // if the nested capture does not implicitly or explicitly allow any captures
163 // nothing should capture - and instantiations will create errors if needed.
164 const int x = 0;
165 auto L = [=](auto a) { // <-- #A
166 const int y = 0;
167 return [](auto b) { // <-- #B
168 int c[sizeof(b)];
169 f(x, c);
170 f(y, c);
171 int i = x;
174 ASSERT_NO_CAPTURES(L);
175 auto M_int = L(2);
176 ASSERT_NO_CAPTURES(M_int);
178 { // Permutations of this example must be thoroughly tested!
179 const int x = 0;
180 sample::X cx{5};
181 auto L = [=](auto a) {
182 const int z = 3;
183 return [&,a](auto b) {
184 // expected-warning@-1 {{address of stack memory associated with local variable 'z' returned}}
185 // expected-note@#call {{in instantiation of}}
186 const int y = 5;
187 return [=](auto c) {
188 int d[sizeof(a) == sizeof(c) || sizeof(c) == sizeof(b) ? 2 : 1];
189 f(x, d);
190 f(y, d);
191 f(z, d); // expected-note {{implicitly captured by reference due to use here}}
192 decltype(a) A = a;
193 decltype(b) B = b;
194 const int &i = cx.i;
198 auto M = L(3)(3.5); // #call
199 M(3.14);
202 namespace Test_no_capture_of_clearly_no_odr_use {
203 auto foo() {
204 const int x = 10;
205 auto L = [=](auto a) {
206 return [=](auto b) {
207 return [=](auto c) {
208 int A = x;
209 return A;
213 auto M = L(1);
214 auto N = M(2.14);
215 ASSERT_NO_CAPTURES(L);
216 ASSERT_NO_CAPTURES(N);
218 return 0;
222 namespace Test_capture_of_odr_use_var {
223 auto foo() {
224 const int x = 10;
225 auto L = [=](auto a) {
226 return [=](auto b) {
227 return [=](auto c) {
228 int A = x;
229 const int &i = x;
230 decltype(a) A2 = a;
231 return A;
235 auto M_int = L(1);
236 auto N_int_int = M_int(2);
237 ASSERT_CLOSURE_SIZE_EXACT(L, sizeof(x));
238 // M_int captures both a & x
239 ASSERT_CLOSURE_SIZE_EXACT(M_int, sizeof(x) + sizeof(int));
240 // N_int_int captures both a & x
241 ASSERT_CLOSURE_SIZE_EXACT(N_int_int, sizeof(x) + sizeof(int));
242 auto M_double = L(3.14);
243 ASSERT_CLOSURE_SIZE(M_double, sizeof(x) + sizeof(double));
245 return 0;
247 auto run = foo();
251 namespace more_nested_captures_1 {
252 template<class T> struct Y {
253 static void f(int, double, ...) { }
254 template<class R>
255 static void f(const int&, R, ...) { }
256 template<class R>
257 void foo(R t) {
258 const int x = 10; //expected-note{{declared here}}
259 auto L = [](auto a) {
260 return [=](auto b) {
261 return [=](auto c) {
262 f(x, c, b, a); //expected-error{{reference to local variable 'x'}}
263 return 0;
267 auto M = L(t);
268 auto N = M('b');
269 N(3.14);
270 N(5); //expected-note{{in instantiation of}}
273 Y<int> yi;
274 int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}
278 namespace more_nested_captures_1_1 {
279 template<class T> struct Y {
280 static void f(int, double, ...) { }
281 template<class R>
282 static void f(const int&, R, ...) { }
283 template<class R>
284 void foo(R t) {
285 const int x = 10; //expected-note{{declared here}}
286 auto L = [](auto a) {
287 return [=](char b) {
288 return [=](auto c) {
289 f(x, c, b, a); //expected-error{{reference to local variable 'x'}}
290 return 0;
294 auto M = L(t);
295 auto N = M('b');
296 N(3.14);
297 N(5); //expected-note{{in instantiation of}}
300 Y<int> yi;
301 int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}
303 namespace more_nested_captures_1_2 {
304 template<class T> struct Y {
305 static void f(int, double, ...) { }
306 template<class R>
307 static void f(const int&, R, ...) { }
308 template<class R>
309 void foo(R t) {
310 const int x = 10;
311 auto L = [=](auto a) {
312 return [=](char b) {
313 return [=](auto c) {
314 f(x, c, b, a);
315 return 0;
319 auto M = L(t);
320 auto N = M('b');
321 N(3.14);
322 N(5);
325 Y<int> yi;
326 int run = (yi.foo(3.14), 0);
329 namespace more_nested_captures_1_3 {
330 template<class T> struct Y {
331 static void f(int, double, ...) { }
332 template<class R>
333 static void f(const int&, R, ...) { }
334 template<class R>
335 void foo(R t) {
336 const int x = 10; //expected-note{{declared here}}
337 auto L = [=](auto a) {
338 return [](auto b) {
339 const int y = 0;
340 return [=](auto c) {
341 f(x, c, b); //expected-error{{reference to local variable 'x'}}
342 f(y, b, c);
343 return 0;
347 auto M = L(t);
348 auto N = M('b');
349 N(3.14);
350 N(5); //expected-note{{in instantiation of}}
353 Y<int> yi;
354 int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}
358 namespace more_nested_captures_1_4 {
359 template<class T> struct Y {
360 static void f(int, double, ...) { }
361 template<class R>
362 static void f(const int&, R, ...) { }
363 template<class R>
364 void foo(R t) {
365 const int x = 10; //expected-note{{declared here}}
366 auto L = [=](auto a) {
367 T t2{t};
368 return [](auto b) {
369 const int y = 0; //expected-note{{declared here}}
370 return [](auto c) { //expected-note 2{{lambda expression begins here}} expected-note 2 {{capture 'x' by}} expected-note 2 {{capture 'y' by}} expected-note 4 {{default capture by}}
371 f(x, c); //expected-error{{variable 'x'}}
372 f(y, c); //expected-error{{variable 'y'}}
373 return 0;
377 auto M = L(t);
378 auto N_char = M('b');
379 N_char(3.14);
380 auto N_double = M(3.14);
381 N_double(3.14);
382 N_char(3); //expected-note{{in instantiation of}}
385 Y<int> yi;
386 int run = (yi.foo('a'), 0); //expected-note{{in instantiation of}}
390 namespace more_nested_captures_2 {
391 template<class T> struct Y {
392 static void f(int, double) { }
393 template<class R>
394 static void f(const int&, R) { }
395 template<class R>
396 void foo(R t) {
397 const int x = 10;
398 auto L = [=](auto a) {
399 return [=](auto b) {
400 return [=](auto c) {
401 f(x, c);
402 return 0;
406 auto M = L(t);
407 auto N = M('b');
408 N(3);
409 N(3.14);
412 Y<int> yi;
413 int run = (yi.foo(3.14), 0);
417 namespace more_nested_captures_3 {
418 template<class T> struct Y {
419 static void f(int, double) { }
420 template<class R>
421 static void f(const int&, R) { }
422 template<class R>
423 void foo(R t) {
424 const int x = 10; //expected-note{{declared here}}
425 auto L = [](auto a) {
426 return [=](auto b) {
427 return [=](auto c) {
428 f(x, c); //expected-error{{reference to local variable 'x'}}
429 return 0;
433 auto M = L(t);
434 auto N = M('b');
435 N(3); //expected-note{{in instantiation of}}
436 N(3.14);
439 Y<int> yi;
440 int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}
444 namespace more_nested_captures_4 {
445 template<class T> struct Y {
446 static void f(int, double) { }
447 template<class R>
448 static void f(const int&, R) { }
449 template<class R>
450 void foo(R t) {
451 const int x = 10; //expected-note{{'x' declared here}}
452 auto L = [](auto a) {
453 return [=](char b) {
454 return [=](auto c) {
455 f(x, c); //expected-error{{reference to local variable 'x'}}
456 return 0;
460 auto M = L(t);
461 auto N = M('b');
462 N(3); //expected-note{{in instantiation of}}
463 N(3.14);
466 Y<int> yi;
467 int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}
471 namespace more_nested_captures_5 {
472 template<class T> struct Y {
473 static void f(int, double) { }
474 template<class R>
475 static void f(const int&, R) { }
476 template<class R>
477 void foo(R t) {
478 const int x = 10;
479 auto L = [=](auto a) {
480 return [=](char b) {
481 return [=](auto c) {
482 f(x, c);
483 return 0;
487 auto M = L(t);
488 auto N = M('b');
489 N(3);
490 N(3.14);
493 Y<int> yi;
494 int run = (yi.foo(3.14), 0);
498 namespace lambdas_in_NSDMIs {
499 template<class T>
500 struct L {
501 T t{};
502 T t2 = ([](auto a) { return [](auto b) { return b; };})(t)(t);
503 T t3 = ([](auto a) { return a; })(t);
505 L<int> l;
506 int run = l.t2;
508 namespace test_nested_decltypes_in_trailing_return_types {
509 int foo() {
510 auto L = [](auto a) {
511 return [](auto b, decltype(a) b2) -> decltype(a) {
512 return decltype(a){};
515 auto M = L(3.14);
516 M('a', 6.26);
517 return 0;
521 namespace more_this_capture_1 {
522 struct X {
523 void f(int) { }
524 static void f(double) { }
525 void foo() {
527 auto L = [=](auto a) {
528 f(a);
530 L(3);
531 L(3.13);
534 auto L = [](auto a) { // expected-note {{explicitly capture 'this'}}
535 f(a); //expected-error{{this}}
537 L(3.13);
538 L(2); //expected-note{{in instantiation}}
542 int g() {
543 auto L = [=](auto a) {
544 return [](int i) {
545 return [=](auto b) {
546 f(b);
547 int x = i;
551 auto M = L(0.0);
552 auto N = M(3);
553 N(5.32); // OK
554 return 0;
557 int run = X{}.g();
559 namespace more_this_capture_1_1 {
560 struct X {
561 void f(int) { }
562 static void f(double) { }
564 int g() {
565 auto L = [=](auto a) {
566 return [](int i) { // expected-note {{explicitly capture 'this'}} expected-note {{while substituting into a lambda}}
567 return [=](auto b) { // expected-note {{while substituting into a lambda}}
568 f(decltype(a){}); //expected-error{{this}}
569 int x = i;
573 auto M = L(0.0);
574 auto N = M(3);
575 N(5.32); // OK
576 L(3); // expected-note{{instantiation}}
577 return 0;
580 int run = X{}.g();
583 namespace more_this_capture_1_1_1 {
584 struct X {
585 void f(int) { }
586 static void f(double) { }
588 int g() {
589 auto L = [=](auto a) {
590 return [](auto b) { // expected-note {{explicitly capture 'this'}} expected-note {{while substituting into a lambda}}
591 return [=](int i) { // expected-note {{while substituting into a lambda}}
592 f(b);
593 f(decltype(a){}); //expected-error{{this}}
597 auto M = L(0.0); // OK
598 auto N = M(3.3); //OK
599 auto M_int = L(0); //expected-note{{instantiation}}
600 return 0;
603 int run = X{}.g();
607 namespace more_this_capture_1_1_1_1 {
608 struct X {
609 void f(int) { }
610 static void f(double) { }
612 int g() {
613 auto L = [=](auto a) {
614 return [](auto b) { // expected-note {{explicitly capture 'this'}}
615 return [=](int i) { // expected-note {{while substituting into a lambda}}
616 f(b); //expected-error{{this}}
617 f(decltype(a){});
621 auto M_double = L(0.0); // OK
622 auto N = M_double(3); //expected-note{{instantiation}}
624 return 0;
627 int run = X{}.g();
630 namespace more_this_capture_2 {
631 struct X {
632 void f(int) { }
633 static void f(double) { }
635 int g() {
636 auto L = [=](auto a) {
637 return [](int i) {
638 return [=](auto b) { // expected-cxx2a-note {{explicitly capture 'this'}}
639 f(b); //expected-error{{'this' cannot}}
640 int x = i;
644 auto M = L(0.0);
645 auto N = M(3);
646 N(5); // NOT OK expected-note{{in instantiation of}}
647 return 0;
650 int run = X{}.g();
652 namespace diagnose_errors_early_in_generic_lambdas {
654 int foo()
657 { // This variable is used and must be caught early, do not need instantiation
658 const int x = 0; //expected-note{{declared}}
659 auto L = [](auto a) { //expected-note{{begins}} expected-note 2 {{capture 'x' by}} expected-note 2 {{default capture by}}
660 const int &r = x; //expected-error{{variable}}
663 { // This variable is not used
664 const int x = 0;
665 auto L = [](auto a) {
666 int i = x;
671 const int x = 0; //expected-note{{declared}}
672 auto L = [=](auto a) { // <-- #A
673 const int y = 0;
674 return [](auto b) { //expected-note{{begins}} expected-note 2 {{capture 'x' by}} expected-note 2 {{default capture by}}
675 int c[sizeof(b)];
676 f(x, c);
677 f(y, c);
678 int i = x;
679 // This use will always be an error regardless of instantiation
680 // so diagnose this early.
681 const int &r = x; //expected-error{{variable}}
686 return 0;
689 int run = foo();
692 namespace generic_nongenerics_interleaved_1 {
693 int foo() {
695 auto L = [](int a) {
696 int y = 10;
697 return [=](auto b) {
698 return a + y;
701 auto M = L(3);
702 M(5);
705 int x;
706 auto L = [](int a) {
707 int y = 10;
708 return [=](auto b) {
709 return a + y;
712 auto M = L(3);
713 M(5);
716 // FIXME: why are there 2 error messages here?
717 int x;
718 auto L = [](auto a) { //expected-note {{declared here}}
719 int y = 10; //expected-note {{declared here}}
720 return [](int b) { //expected-note 2{{expression begins here}} expected-note 2 {{capture 'a' by}} expected-note 2 {{capture 'y' by}} expected-note 4 {{default capture by}}
721 return [=] (auto c) {
722 return a + y; //expected-error 2{{cannot be implicitly captured}}
728 int x;
729 auto L = [](auto a) {
730 int y = 10;
731 return [=](int b) {
732 return [=] (auto c) {
733 return a + y;
738 return 1;
741 int run = foo();
743 namespace dont_capture_refs_if_initialized_with_constant_expressions {
745 auto foo(int i) {
746 // This is surprisingly not odr-used within the lambda!
747 static int j;
748 j = i;
749 int &ref_j = j;
750 return [](auto a) { return ref_j; }; // ok
753 template<class T>
754 auto foo2(T t) {
755 // This is surprisingly not odr-used within the lambda!
756 static T j;
757 j = t;
758 T &ref_j = j;
759 return [](auto a) { return ref_j; }; // ok
762 int do_test() {
763 auto L = foo(3);
764 auto L_int = L(3);
765 auto L_char = L('a');
766 auto L1 = foo2(3.14);
767 auto L1_int = L1(3);
768 auto L1_char = L1('a');
769 return 0;
772 } // dont_capture_refs_if_initialized_with_constant_expressions
774 namespace test_conversion_to_fptr {
776 template<class T> struct X {
778 T (*fp)(T) = [](auto a) { return a; };
782 X<int> xi;
784 template<class T>
785 void fooT(T t, T (*fp)(T) = [](auto a) { return a; }) {
786 fp(t);
789 int test() {
791 auto L = [](auto a) { return a; };
792 int (*fp)(int) = L;
793 fp(5);
794 L(3);
795 char (*fc)(char) = L;
796 fc('b');
797 L('c');
798 double (*fd)(double) = L;
799 fd(3.14);
800 fd(6.26);
801 L(4.25);
804 auto L = [](auto a) ->int { return a; }; //expected-note 2{{candidate template ignored}}
805 int (*fp)(int) = L;
806 char (*fc)(char) = L; //expected-error{{no viable conversion}}
807 double (*fd)(double) = L; //expected-error{{no viable conversion}}
810 int x = 5;
811 auto L = [=](auto b, char c = 'x') {
812 int i = x;
813 return [](auto a) ->decltype(a) { return a; };
815 int (*fp)(int) = L(8);
816 fp(5);
817 L(3);
818 char (*fc)(char) = L('a');
819 fc('b');
820 L('c');
821 double (*fd)(double) = L(3.14);
822 fd(3.14);
823 fd(6.26);
827 auto L = [=](auto b) {
828 return [](auto a) ->decltype(b)* { return (decltype(b)*)0; };
830 int* (*fp)(int) = L(8);
831 fp(5);
832 L(3);
833 char* (*fc)(char) = L('a');
834 fc('b');
835 L('c');
836 double* (*fd)(double) = L(3.14);
837 fd(3.14);
838 fd(6.26);
841 auto L = [=](auto b) {
842 return [](auto a) ->decltype(b)* { return (decltype(b)*)0; }; //expected-note{{candidate template ignored}}
844 char* (*fp)(int) = L('8');
845 fp(5);
846 char* (*fc)(char) = L('a');
847 fc('b');
848 double* (*fi)(int) = L(3.14);
849 fi(5);
850 int* (*fi2)(int) = L(3.14); //expected-error{{no viable conversion}}
854 auto L = [=](auto b) {
855 return [](auto a) {
856 return [=](auto c) {
857 return [](auto d) ->decltype(a + b + c + d) { return d; };
861 int (*fp)(int) = L('8')(3)(short{});
862 double (*fs)(char) = L(3.14)(short{})('4');
865 fooT(3);
866 fooT('a');
867 fooT(3.14);
868 fooT("abcdefg");
869 return 0;
871 int run2 = test();
876 namespace this_capture {
877 void f(char, int) { }
878 template<class T>
879 void f(T, const int&) { }
881 struct X {
882 int x = 0;
883 void foo() {
884 auto L = [=](auto a) {
885 return [=](auto b) {
886 //f(a, x++);
887 x++;
890 L('a')(5);
891 L('b')(4);
892 L(3.14)('3');
898 int run = (X{}.foo(), 0);
900 namespace this_capture_unresolvable {
901 struct X {
902 void f(int) { }
903 static void f(double) { }
905 int g() {
906 auto lam = [=](auto a) { f(a); }; // captures 'this'
907 lam(0); // ok.
908 lam(0.0); // ok.
909 return 0;
911 int g2() {
912 auto lam = [](auto a) { f(a); }; // expected-error{{'this'}} expected-note {{explicitly capture 'this'}}
913 lam(0); // expected-note{{in instantiation of}}
914 lam(0.0); // ok.
915 return 0;
917 double (*fd)(double) = [](auto a) { f(a); return a; };
921 int run = X{}.g();
925 namespace check_nsdmi_and_this_capture_of_member_functions {
927 struct FunctorDouble {
928 template<class T> FunctorDouble(T t) { t(2.14); };
930 struct FunctorInt {
931 template<class T> FunctorInt(T t) { t(2); }; //expected-note{{in instantiation of}}
934 template<class T> struct YUnresolvable {
935 void f(int) { }
936 static void f(double) { }
938 T t = [](auto a) { f(a); return a; };
939 T t2 = [=](auto b) { f(b); return b; };
942 template<class T> struct YUnresolvable2 {
943 void f(int) { }
944 static void f(double) { }
946 T t = [](auto a) { f(a); return a; }; //expected-error{{'this'}} \
947 //expected-note{{in instantiation of}}\
948 //expected-note {{explicitly capture 'this'}}
949 T t2 = [=](auto b) { f(b); return b; };
953 YUnresolvable<FunctorDouble> yud;
954 // This will cause an error since it call's with an int and calls a member function.
955 YUnresolvable2<FunctorInt> yui;
958 template<class T> struct YOnlyStatic {
959 static void f(double) { }
961 T t = [](auto a) { f(a); return a; };
963 YOnlyStatic<FunctorDouble> yos;
964 template<class T> struct YOnlyNonStatic {
965 void f(int) { }
967 T t = [](auto a) { f(a); return a; }; //expected-error{{'this'}} expected-note {{explicitly capture 'this'}}
974 namespace check_nsdmi_and_this_capture_of_data_members {
976 struct FunctorDouble {
977 template<class T> FunctorDouble(T t) { t(2.14); };
979 struct FunctorInt {
980 template<class T> FunctorInt(T t) { t(2); };
983 template<class T> struct YThisCapture {
984 const int x = 10;
985 static double d;
986 T t = [](auto a) { return x; }; //expected-error{{'this'}} expected-note {{explicitly capture 'this'}}
987 T t2 = [](auto b) { return d; };
988 T t3 = [this](auto a) {
989 return [=](auto b) {
990 return x;
993 T t4 = [=](auto a) {
994 return [=](auto b) {
995 return x;
998 T t5 = [](auto a) { // expected-note {{explicitly capture 'this'}}
999 return [=](auto b) {
1000 return x; //expected-error{{'this'}}
1005 template<class T> double YThisCapture<T>::d = 3.14;
1011 #ifdef DELAYED_TEMPLATE_PARSING
1012 template<class T> void foo_no_error(T t) {
1013 auto L = []()
1014 { return t; };
1016 template<class T> void foo(T t) { //expected-note 2{{declared here}}
1017 auto L = []() //expected-note 2{{begins here}}
1018 { return t; }; //expected-error 2{{cannot be implicitly captured}}
1020 template void foo(int); //expected-note{{in instantiation of}}
1022 #else
1024 template<class T> void foo(T t) { //expected-note{{declared here}}
1025 auto L = []() //expected-note{{begins here}} expected-note 2 {{capture 't' by}} expected-note 2 {{default capture by}}
1026 { return t; }; //expected-error{{cannot be implicitly captured}}
1029 #endif
1032 namespace no_this_capture_for_static {
1034 struct X {
1035 static void f(double) { }
1037 int g() {
1038 auto lam = [=](auto a) { f(a); };
1039 lam(0); // ok.
1040 ASSERT_NO_CAPTURES(lam);
1041 return 0;
1045 int run = X{}.g();
1048 namespace this_capture_for_non_static {
1050 struct X {
1051 void f(double) { }
1053 int g() {
1054 auto L = [=](auto a) { f(a); };
1055 L(0);
1056 auto L2 = [](auto a) { f(a); }; //expected-error {{cannot be implicitly captured}} expected-note {{explicitly capture 'this'}}
1057 return 0;
1061 int run = X{}.g();
1064 namespace this_captures_with_num_args_disambiguation {
1066 struct X {
1067 void f(int) { }
1068 static void f(double, int i) { }
1069 int g() {
1070 auto lam = [](auto a) { f(a, a); };
1071 lam(0);
1072 return 0;
1076 int run = X{}.g();
1078 namespace enclosing_function_is_template_this_capture {
1079 // Only error if the instantiation tries to use the member function.
1080 struct X {
1081 void f(int) { }
1082 static void f(double) { }
1083 template<class T>
1084 int g(T t) {
1085 auto L = [](auto a) { f(a); }; //expected-error{{'this'}} expected-note {{explicitly capture 'this'}}
1086 L(t); // expected-note{{in instantiation of}}
1087 return 0;
1091 int run = X{}.g(0.0); // OK.
1092 int run2 = X{}.g(0); // expected-note{{in instantiation of}}
1097 namespace enclosing_function_is_template_this_capture_2 {
1098 // This should error, even if not instantiated, since
1099 // this would need to be captured.
1100 struct X {
1101 void f(int) { }
1102 template<class T>
1103 int g(T t) {
1104 auto L = [](auto a) { f(a); }; //expected-error{{'this'}} expected-note {{explicitly capture 'this'}}
1105 L(t);
1106 return 0;
1113 namespace enclosing_function_is_template_this_capture_3 {
1114 // This should not error, this does not need to be captured.
1115 struct X {
1116 static void f(int) { }
1117 template<class T>
1118 int g(T t) {
1119 auto L = [](auto a) { f(a); };
1120 L(t);
1121 return 0;
1125 int run = X{}.g(0.0); // OK.
1126 int run2 = X{}.g(0); // OK.
1130 namespace nested_this_capture_1 {
1131 struct X {
1132 void f(int) { }
1133 static void f(double) { }
1135 int g() {
1136 auto L = [=](auto a) {
1137 return [this]() {
1138 return [=](auto b) {
1139 f(b);
1143 auto M = L(0);
1144 auto N = M();
1145 N(5);
1146 return 0;
1150 int run = X{}.g();
1155 namespace nested_this_capture_2 {
1156 struct X {
1157 void f(int) { }
1158 static void f(double) { }
1160 int g() {
1161 auto L = [=](auto a) {
1162 return [&]() {
1163 return [=](auto b) {
1164 f(b);
1168 auto M = L(0);
1169 auto N = M();
1170 N(5);
1171 N(3.14);
1172 return 0;
1176 int run = X{}.g();
1180 namespace nested_this_capture_3_1 {
1181 struct X {
1182 template<class T>
1183 void f(int, T t) { }
1184 template<class T>
1185 static void f(double, T t) { }
1187 int g() {
1188 auto L = [=](auto a) {
1189 return [&](auto c) {
1190 return [=](auto b) {
1191 f(b, c);
1195 auto M = L(0);
1196 auto N = M('a');
1197 N(5);
1198 N(3.14);
1199 return 0;
1203 int run = X{}.g();
1208 namespace nested_this_capture_3_2 {
1209 struct X {
1210 void f(int) { }
1211 static void f(double) { }
1213 int g() {
1214 auto L = [=](auto a) {
1215 return [](int i) {
1216 return [=](auto b) { // expected-cxx2a-note {{explicitly capture 'this'}}
1217 f(b); //expected-error {{'this' cannot}}
1218 int x = i;
1222 auto M = L(0.0);
1223 auto N = M(3);
1224 N(5); //expected-note {{in instantiation of}}
1225 N(3.14); // OK.
1226 return 0;
1230 int run = X{}.g();
1234 namespace nested_this_capture_4 {
1235 struct X {
1236 void f(int) { }
1237 static void f(double) { }
1239 int g() {
1240 auto L = [](auto a) {
1241 return [=](auto i) {
1242 return [=](auto b) { // expected-cxx2a-note {{explicitly capture 'this'}}
1243 f(b); //expected-error {{'this' cannot}}
1244 int x = i;
1248 auto M = L(0.0);
1249 auto N = M(3);
1250 N(5); //expected-note {{in instantiation of}}
1251 N(3.14); // OK.
1252 return 0;
1256 int run = X{}.g();
1259 namespace capture_enclosing_function_parameters {
1262 inline auto foo(int x) {
1263 int i = 10;
1264 auto lambda = [=](auto z) { return x + z; };
1265 return lambda;
1268 int foo2() {
1269 auto L = foo(3);
1270 L(4);
1271 L('a');
1272 L(3.14);
1273 return 0;
1276 inline auto foo3(int x) {
1277 int local = 1;
1278 auto L = [=](auto a) {
1279 int i = a[local];
1280 return [=](auto b) mutable {
1281 auto n = b;
1282 return [&, n](auto c) mutable {
1283 ++local;
1284 return ++x;
1288 auto M = L("foo-abc");
1289 auto N = M("foo-def");
1290 auto O = N("foo-ghi");
1292 return L;
1295 int main() {
1296 auto L3 = foo3(3);
1297 auto M3 = L3("L3-1");
1298 auto N3 = M3("M3-1");
1299 auto O3 = N3("N3-1");
1300 N3("N3-2");
1301 M3("M3-2");
1302 M3("M3-3");
1303 L3("L3-2");
1305 } // end ns
1307 namespace capture_arrays {
1309 inline int sum_array(int n) {
1310 int array2[5] = { 1, 2, 3, 4, 5};
1312 auto L = [=](auto N) -> int {
1313 int sum = 0;
1314 int array[5] = { 1, 2, 3, 4, 5 };
1315 sum += array2[sum];
1316 sum += array2[N];
1317 return 0;
1319 L(2);
1320 return L(n);
1324 namespace capture_non_odr_used_variable_because_named_in_instantiation_dependent_expressions {
1326 // even though 'x' is not odr-used, it should be captured.
1328 int test() {
1329 const int x = 10;
1330 auto L = [=](auto a) {
1331 (void) +x + a;
1333 ASSERT_CLOSURE_SIZE_EXACT(L, sizeof(x));
1336 } //end ns
1337 #ifdef MS_EXTENSIONS
1338 namespace explicit_spec {
1339 template<class R> struct X {
1340 template<class T> int foo(T t) {
1341 auto L = [](auto a) { return a; };
1342 L(&t);
1343 return 0;
1346 template<> int foo<char>(char c) { //expected-warning{{explicit specialization}}
1347 const int x = 10;
1348 auto LC = [](auto a) { return a; };
1349 R r;
1350 LC(&r);
1351 auto L = [=](auto a) {
1352 return [=](auto b) {
1353 int d[sizeof(a)];
1354 f(x, d);
1357 auto M = L(1);
1359 ASSERT_NO_CAPTURES(M);
1360 return 0;
1365 int run_char = X<int>{}.foo('a');
1366 int run_int = X<double>{}.foo(4);
1368 #endif // MS_EXTENSIONS
1370 namespace nsdmi_capturing_this {
1371 struct X {
1372 int m = 10;
1373 int n = [this](auto) { return m; }(20);
1376 template<class T>
1377 struct XT {
1378 T m = 10;
1379 T n = [this](auto) { return m; }(20);
1382 XT<int> xt{};
1387 void PR33318(int i) {
1388 [&](auto) { static_assert(&i != nullptr, ""); }(0); // expected-warning 2{{always true}} expected-note {{instantiation}}
1391 // Check to make sure that we don't capture when member-calls are made to members that are not of 'this' class.
1392 namespace PR34266 {
1393 // https://bugs.llvm.org/show_bug.cgi?id=34266
1394 namespace ns1 {
1395 struct A {
1396 static void bar(int) { }
1397 static void bar(double) { }
1400 struct B
1402 template<class T>
1403 auto f() {
1404 auto L = [=] {
1405 T{}.bar(3.0);
1406 T::bar(3);
1409 ASSERT_NO_CAPTURES(L);
1410 return L;
1414 void test() {
1415 B{}.f<A>();
1417 } // end ns1
1419 namespace ns2 {
1420 struct A {
1421 static void bar(int) { }
1422 static void bar(double) { }
1425 struct B
1427 using T = A;
1428 auto f() {
1429 auto L = [=](auto a) {
1430 T{}.bar(a);
1431 T::bar(a);
1434 ASSERT_NO_CAPTURES(L);
1435 return L;
1439 void test() {
1440 B{}.f()(3.0);
1441 B{}.f()(3);
1443 } // end ns2
1445 namespace ns3 {
1446 struct A {
1447 void bar(int) { }
1448 static void bar(double) { }
1451 struct B
1453 using T = A;
1454 auto f() {
1455 auto L = [=](auto a) {
1456 T{}.bar(a);
1457 T::bar(a); // This call ignores the instance member function because the implicit object argument fails to convert.
1460 ASSERT_NO_CAPTURES(L);
1461 return L;
1465 void test() {
1466 B{}.f()(3.0);
1467 B{}.f()(3);
1470 } // end ns3
1473 namespace ns4 {
1474 struct A {
1475 void bar(int) { }
1476 static void bar(double) { }
1479 struct B : A
1481 using T = A;
1482 auto f() {
1483 auto L = [=](auto a) {
1484 T{}.bar(a);
1485 T::bar(a);
1488 // just check to see if the size if >= 2 bytes (which should be the case if we capture anything)
1489 ASSERT_CLOSURE_SIZE(L, 2);
1490 return L;
1494 void test() {
1495 B{}.f()(3.0);
1496 B{}.f()(3);
1499 } // end ns4
1501 namespace ns5 {
1502 struct A {
1503 void bar(int) { }
1504 static void bar(double) { }
1507 struct B
1509 template<class T>
1510 auto f() {
1511 auto L = [&](auto a) {
1512 T{}.bar(a);
1513 T::bar(a);
1517 ASSERT_NO_CAPTURES(L);
1518 return L;
1522 void test() {
1523 B{}.f<A>()(3.0);
1524 B{}.f<A>()(3);
1527 } // end ns5
1529 } // end PR34266
1531 namespace capture_pack {
1532 #if __cplusplus >= 201702L
1533 constexpr
1534 #endif
1535 auto v =
1536 [](auto ...a) {
1537 [&](auto ...b) {
1538 ((a = b), ...); // expected-warning 0-1{{extension}}
1539 }(100, 20, 3);
1540 return (a + ...); // expected-warning 0-1{{extension}}
1541 }(400, 50, 6);
1542 #if __cplusplus >= 201702L
1543 static_assert(v == 123);
1544 #endif