1546 lines · cpp
1// RUN: %clang_cc1 -std=c++1y -verify -fblocks -emit-llvm-only %s2// RUN: %clang_cc1 -std=c++2a -verify -verify=expected-cxx2a -fblocks -emit-llvm-only -Wno-deprecated-this-capture %s3// RUN: %clang_cc1 -std=c++1y -verify -fblocks -emit-llvm-only -triple i386-windows-pc %s4// RUN: %clang_cc1 -std=c++2a -verify -verify=expected-cxx2a -fblocks -emit-llvm-only -triple i386-windows-pc -Wno-deprecated-this-capture %s5// DONTRUNYET: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fdelayed-template-parsing %s -DDELAYED_TEMPLATE_PARSING6// DONTRUNYET: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fms-extensions %s -DMS_EXTENSIONS7// DONTRUNYET: %clang_cc1 -std=c++1y -verify -fsyntax-only -fblocks -fdelayed-template-parsing -fms-extensions %s -DMS_EXTENSIONS -DDELAYED_TEMPLATE_PARSING8 9constexpr int ODRUSE_SZ = sizeof(char);10 11template<class T, int N>12void f(T, const int (&)[N]) { }13 14template<class T>15void f(const T&, const int (&)[ODRUSE_SZ]) { }16 17#define DEFINE_SELECTOR(x) \18 int selector_ ## x[sizeof(x) == ODRUSE_SZ ? ODRUSE_SZ : ODRUSE_SZ + 5]19 20#define F_CALL(x, a) f(x, selector_ ## a)21 22// This is a risky assumption, because if an empty class gets captured by value23// 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)27 28 29namespace sample {30 struct X { 31 int i;32 X(int i) : i(i) { }33 };34} 35 36namespace test_transformations_in_templates {37template<class T> void foo(T t) {38 auto L = [](auto a) { return a; };39}40template<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 : 247 ]{}; 48 }; 49 N('a');50 }; 51 };52 L(3.14);53}54 55void doit() {56 foo(3);57 foo('a');58 foo2('A');59}60}61 62namespace test_return_type_deduction {63 64void doit() {65 66 auto L = [](auto a, auto b) {67 if ( a > b ) return a;68 return b;69 };70 L(2, 4);71 {72 auto L2 = [](auto a, int i) {73 return a + i;74 };75 L2(3.14, 2);76 }77 {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';}); };84 85 //auto X = ^{ return a; };86 87 //auto Y = []() -> auto { return 3; return 'c'; };88 } 89} 90}91 92 93namespace test_no_capture{94void doit() {95 const int x = 10; //expected-note{{declared here}}96 {97 // should not capture 'x' - variable undergoes lvalue-to-rvalue98 auto L = [=](auto a) {99 int y = x;100 return a + y;101 };102 ASSERT_NO_CAPTURES(L);103 }104 {105 // should not capture 'x' - even though certain instantiations require106 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}}109 };110 ASSERT_NO_CAPTURES(L);111 L('s'); //expected-note{{in instantiation of}}112 }113 {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; 121 }; 122 };123 };124 ASSERT_NO_CAPTURES(L);125 } 126} // doit127} // namespace128 129namespace test_capture_of_potentially_evaluated_expression {130void doit() {131 const int x = 5;132 {133 auto L = [=](auto a) {134 DEFINE_SELECTOR(a);135 F_CALL(x, a);136 };137 static_assert(sizeof(L) == 4, "Must be captured");138 }139 {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}}143 };144 }145 {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; 154 }; 155 };156 };157 auto M = L(3);158 auto N = M(5);159 160 }161 162 { // if the nested capture does not implicitly or explicitly allow any captures163 // nothing should capture - and instantiations will create errors if needed.164 const int x = 0;165 auto L = [=](auto a) { // <-- #A166 const int y = 0;167 return [](auto b) { // <-- #B168 int c[sizeof(b)];169 f(x, c);170 f(y, c);171 int i = x;172 };173 };174 ASSERT_NO_CAPTURES(L);175 auto M_int = L(2);176 ASSERT_NO_CAPTURES(M_int);177 }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;195 }; 196 };197 };198 auto M = L(3)(3.5); // #call199 M(3.14);200 }201}202namespace Test_no_capture_of_clearly_no_odr_use {203auto 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;210 };211 };212 };213 auto M = L(1);214 auto N = M(2.14);215 ASSERT_NO_CAPTURES(L);216 ASSERT_NO_CAPTURES(N);217 218 return 0;219}220}221 222namespace Test_capture_of_odr_use_var {223auto 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;232 };233 };234 };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));244 245 return 0;246}247auto run = foo();248}249 250} 251namespace more_nested_captures_1 {252template<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; 264 };265 };266 };267 auto M = L(t);268 auto N = M('b');269 N(3.14);270 N(5); //expected-note{{in instantiation of}}271 }272};273Y<int> yi;274int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}275}276 277 278namespace more_nested_captures_1_1 {279template<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; 291 };292 };293 };294 auto M = L(t);295 auto N = M('b');296 N(3.14);297 N(5); //expected-note{{in instantiation of}}298 }299};300Y<int> yi;301int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}302}303namespace more_nested_captures_1_2 {304template<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; 316 };317 };318 };319 auto M = L(t);320 auto N = M('b');321 N(3.14);322 N(5); 323 }324};325Y<int> yi;326int run = (yi.foo(3.14), 0); 327}328 329namespace more_nested_captures_1_3 {330template<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; 344 };345 };346 };347 auto M = L(t);348 auto N = M('b');349 N(3.14);350 N(5); //expected-note{{in instantiation of}}351 }352};353Y<int> yi;354int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}355}356 357 358namespace more_nested_captures_1_4 {359template<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;374 };375 };376 };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}}383 }384};385Y<int> yi;386int run = (yi.foo('a'), 0); //expected-note{{in instantiation of}}387}388 389 390namespace more_nested_captures_2 {391template<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; 403 };404 };405 };406 auto M = L(t);407 auto N = M('b');408 N(3);409 N(3.14);410 }411};412Y<int> yi;413int run = (yi.foo(3.14), 0);414 415}416 417namespace more_nested_captures_3 {418template<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; 430 };431 };432 };433 auto M = L(t);434 auto N = M('b');435 N(3); //expected-note{{in instantiation of}}436 N(3.14);437 }438};439Y<int> yi;440int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}441 442}443 444namespace more_nested_captures_4 {445template<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; 457 };458 };459 };460 auto M = L(t);461 auto N = M('b');462 N(3); //expected-note{{in instantiation of}}463 N(3.14);464 }465};466Y<int> yi;467int run = (yi.foo(3.14), 0); //expected-note{{in instantiation of}}468 469}470 471namespace more_nested_captures_5 {472template<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; 484 };485 };486 };487 auto M = L(t);488 auto N = M('b');489 N(3); 490 N(3.14);491 }492};493Y<int> yi;494int run = (yi.foo(3.14), 0);495 496}497 498namespace lambdas_in_NSDMIs {499template<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); 504 };505 L<int> l; 506 int run = l.t2; 507}508namespace test_nested_decltypes_in_trailing_return_types {509int foo() {510 auto L = [](auto a) {511 return [](auto b, decltype(a) b2) -> decltype(a) {512 return decltype(a){};513 };514 };515 auto M = L(3.14);516 M('a', 6.26);517 return 0;518}519}520 521namespace more_this_capture_1 {522struct X {523 void f(int) { }524 static void f(double) { }525 void foo() {526 {527 auto L = [=](auto a) {528 f(a);529 };530 L(3);531 L(3.13);532 }533 {534 auto L = [](auto a) { // expected-note {{explicitly capture 'this'}}535 f(a); //expected-error{{this}}536 };537 L(3.13);538 L(2); //expected-note{{in instantiation}}539 }540 }541 542 int g() {543 auto L = [=](auto a) { 544 return [](int i) {545 return [=](auto b) {546 f(b); 547 int x = i;548 };549 };550 };551 auto M = L(0.0); 552 auto N = M(3);553 N(5.32); // OK 554 return 0;555 }556};557int run = X{}.g();558}559namespace more_this_capture_1_1 {560struct X {561 void f(int) { }562 static void f(double) { }563 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;570 };571 };572 };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;578 }579};580int run = X{}.g();581}582 583namespace more_this_capture_1_1_1 {584struct X {585 void f(int) { }586 static void f(double) { }587 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}}594 };595 };596 };597 auto M = L(0.0); // OK598 auto N = M(3.3); //OK599 auto M_int = L(0); //expected-note{{instantiation}}600 return 0;601 }602};603int run = X{}.g();604}605 606 607namespace more_this_capture_1_1_1_1 {608struct X {609 void f(int) { }610 static void f(double) { }611 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){}); 618 };619 };620 };621 auto M_double = L(0.0); // OK622 auto N = M_double(3); //expected-note{{instantiation}}623 624 return 0;625 }626};627int run = X{}.g();628}629 630namespace more_this_capture_2 {631struct X {632 void f(int) { }633 static void f(double) { }634 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;641 };642 };643 };644 auto M = L(0.0); 645 auto N = M(3);646 N(5); // NOT OK expected-note{{in instantiation of}}647 return 0;648 }649};650int run = X{}.g();651}652namespace diagnose_errors_early_in_generic_lambdas {653 654int foo()655{656 657 { // This variable is used and must be caught early, do not need instantiation658 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}}661 };662 }663 { // This variable is not used 664 const int x = 0; 665 auto L = [](auto a) { 666 int i = x; 667 };668 }669 { 670 671 const int x = 0; //expected-note{{declared}}672 auto L = [=](auto a) { // <-- #A673 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 instantiation680 // so diagnose this early.681 const int &r = x; //expected-error{{variable}}682 };683 };684 685 }686 return 0;687}688 689int run = foo();690}691 692namespace generic_nongenerics_interleaved_1 {693int foo() {694 {695 auto L = [](int a) {696 int y = 10;697 return [=](auto b) { 698 return a + y;699 };700 };701 auto M = L(3);702 M(5);703 }704 {705 int x;706 auto L = [](int a) {707 int y = 10;708 return [=](auto b) { 709 return a + y;710 };711 };712 auto M = L(3);713 M(5);714 }715 {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}}723 };724 };725 };726 }727 {728 int x;729 auto L = [](auto a) { 730 int y = 10; 731 return [=](int b) { 732 return [=] (auto c) {733 return a + y; 734 };735 };736 };737 }738 return 1;739}740 741int run = foo();742}743namespace dont_capture_refs_if_initialized_with_constant_expressions {744 745auto 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; }; // ok751}752 753template<class T>754auto 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; }; // ok760}761 762int 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;770}771 772} // dont_capture_refs_if_initialized_with_constant_expressions773 774namespace test_conversion_to_fptr {775 776template<class T> struct X {777 778 T (*fp)(T) = [](auto a) { return a; };779 780};781 782X<int> xi;783 784template<class T> 785void fooT(T t, T (*fp)(T) = [](auto a) { return a; }) {786 fp(t);787}788 789int test() {790{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);802}803{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}}808}809{810 int x = 5;811 auto L = [=](auto b, char c = 'x') {812 int i = x;813 return [](auto a) ->decltype(a) { return a; };814 };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);824 825}826{827 auto L = [=](auto b) {828 return [](auto a) ->decltype(b)* { return (decltype(b)*)0; };829 };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);839}840{841 auto L = [=](auto b) {842 return [](auto a) ->decltype(b)* { return (decltype(b)*)0; }; //expected-note{{candidate template ignored}}843 };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}}851}852 853{854 auto L = [=](auto b) {855 return [](auto a) { 856 return [=](auto c) { 857 return [](auto d) ->decltype(a + b + c + d) { return d; }; 858 }; 859 }; 860 };861 int (*fp)(int) = L('8')(3)(short{});862 double (*fs)(char) = L(3.14)(short{})('4');863}864 865 fooT(3);866 fooT('a');867 fooT(3.14);868 fooT("abcdefg");869 return 0;870}871int run2 = test();872 873}874 875 876namespace this_capture {877void f(char, int) { }878template<class T> 879void f(T, const int&) { }880 881struct X {882 int x = 0;883 void foo() {884 auto L = [=](auto a) {885 return [=](auto b) {886 //f(a, x++);887 x++;888 };889 };890 L('a')(5);891 L('b')(4);892 L(3.14)('3');893 894 }895 896};897 898int run = (X{}.foo(), 0);899 900namespace this_capture_unresolvable {901struct X {902 void f(int) { }903 static void f(double) { }904 905 int g() {906 auto lam = [=](auto a) { f(a); }; // captures 'this'907 lam(0); // ok.908 lam(0.0); // ok.909 return 0;910 }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;916 }917 double (*fd)(double) = [](auto a) { f(a); return a; };918 919};920 921int run = X{}.g();922 923}924 925namespace check_nsdmi_and_this_capture_of_member_functions {926 927struct FunctorDouble {928 template<class T> FunctorDouble(T t) { t(2.14); };929};930struct FunctorInt {931 template<class T> FunctorInt(T t) { t(2); }; //expected-note{{in instantiation of}}932};933 934template<class T> struct YUnresolvable {935 void f(int) { }936 static void f(double) { }937 938 T t = [](auto a) { f(a); return a; }; 939 T t2 = [=](auto b) { f(b); return b; };940};941 942template<class T> struct YUnresolvable2 {943 void f(int) { }944 static void f(double) { }945 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; };950};951 952 953YUnresolvable<FunctorDouble> yud;954// This will cause an error since it call's with an int and calls a member function.955YUnresolvable2<FunctorInt> yui;956 957 958template<class T> struct YOnlyStatic {959 static void f(double) { }960 961 T t = [](auto a) { f(a); return a; };962};963YOnlyStatic<FunctorDouble> yos;964template<class T> struct YOnlyNonStatic {965 void f(int) { }966 967 T t = [](auto a) { f(a); return a; }; //expected-error{{'this'}} expected-note {{explicitly capture 'this'}}968};969 970 971}972 973 974namespace check_nsdmi_and_this_capture_of_data_members {975 976struct FunctorDouble {977 template<class T> FunctorDouble(T t) { t(2.14); };978};979struct FunctorInt {980 template<class T> FunctorInt(T t) { t(2); }; 981};982 983template<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;991 };992 };993 T t4 = [=](auto a) {994 return [=](auto b) {995 return x;996 };997 };998 T t5 = [](auto a) { // expected-note {{explicitly capture 'this'}}999 return [=](auto b) {1000 return x; //expected-error{{'this'}}1001 };1002 };1003};1004 1005template<class T> double YThisCapture<T>::d = 3.14;1006 1007 1008}1009 1010 1011#ifdef DELAYED_TEMPLATE_PARSING1012template<class T> void foo_no_error(T t) { 1013 auto L = []() 1014 { return t; }; 1015}1016template<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}}1019}1020template void foo(int); //expected-note{{in instantiation of}}1021 1022#else1023 1024template<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}}1027}1028 1029#endif1030}1031 1032namespace no_this_capture_for_static {1033 1034struct X {1035 static void f(double) { }1036 1037 int g() {1038 auto lam = [=](auto a) { f(a); }; 1039 lam(0); // ok.1040 ASSERT_NO_CAPTURES(lam);1041 return 0;1042 }1043};1044 1045int run = X{}.g();1046}1047 1048namespace this_capture_for_non_static {1049 1050struct X {1051 void f(double) { }1052 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;1058 }1059};1060 1061int run = X{}.g();1062}1063 1064namespace this_captures_with_num_args_disambiguation {1065 1066struct 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;1073 }1074};1075 1076int run = X{}.g();1077}1078namespace enclosing_function_is_template_this_capture {1079// Only error if the instantiation tries to use the member function.1080struct 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;1088 }1089};1090 1091int run = X{}.g(0.0); // OK.1092int run2 = X{}.g(0); // expected-note{{in instantiation of}}1093 1094 1095}1096 1097namespace enclosing_function_is_template_this_capture_2 {1098// This should error, even if not instantiated, since1099// this would need to be captured.1100struct 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;1107 }1108};1109 1110}1111 1112 1113namespace enclosing_function_is_template_this_capture_3 {1114// This should not error, this does not need to be captured.1115struct 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;1122 }1123};1124 1125int run = X{}.g(0.0); // OK.1126int run2 = X{}.g(0); // OK.1127 1128}1129 1130namespace nested_this_capture_1 {1131struct X {1132 void f(int) { }1133 static void f(double) { }1134 1135 int g() {1136 auto L = [=](auto a) { 1137 return [this]() {1138 return [=](auto b) {1139 f(b); 1140 };1141 };1142 };1143 auto M = L(0);1144 auto N = M();1145 N(5); 1146 return 0;1147 }1148};1149 1150int run = X{}.g();1151 1152}1153 1154 1155namespace nested_this_capture_2 {1156struct X {1157 void f(int) { }1158 static void f(double) { }1159 1160 int g() {1161 auto L = [=](auto a) { 1162 return [&]() {1163 return [=](auto b) {1164 f(b); 1165 };1166 };1167 };1168 auto M = L(0);1169 auto N = M();1170 N(5); 1171 N(3.14); 1172 return 0;1173 }1174};1175 1176int run = X{}.g();1177 1178}1179 1180namespace nested_this_capture_3_1 {1181struct X {1182 template<class T>1183 void f(int, T t) { }1184 template<class T>1185 static void f(double, T t) { }1186 1187 int g() {1188 auto L = [=](auto a) { 1189 return [&](auto c) {1190 return [=](auto b) {1191 f(b, c); 1192 };1193 };1194 };1195 auto M = L(0);1196 auto N = M('a');1197 N(5); 1198 N(3.14); 1199 return 0;1200 }1201};1202 1203int run = X{}.g();1204 1205}1206 1207 1208namespace nested_this_capture_3_2 {1209struct X {1210 void f(int) { }1211 static void f(double) { }1212 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;1219 };1220 };1221 };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;1227 }1228};1229 1230int run = X{}.g();1231 1232}1233 1234namespace nested_this_capture_4 {1235struct X {1236 void f(int) { }1237 static void f(double) { }1238 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;1245 };1246 };1247 };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;1253 }1254};1255 1256int run = X{}.g();1257 1258}1259namespace capture_enclosing_function_parameters {1260 1261 1262inline auto foo(int x) {1263 int i = 10;1264 auto lambda = [=](auto z) { return x + z; };1265 return lambda;1266}1267 1268int foo2() {1269 auto L = foo(3);1270 L(4);1271 L('a');1272 L(3.14);1273 return 0;1274}1275 1276inline 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;1285 };1286 };1287 };1288 auto M = L("foo-abc");1289 auto N = M("foo-def");1290 auto O = N("foo-ghi");1291 1292 return L;1293}1294 1295int 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");1304}1305} // end ns1306 1307namespace capture_arrays {1308 1309inline int sum_array(int n) {1310 int array2[5] = { 1, 2, 3, 4, 5};1311 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;1318 };1319 L(2);1320 return L(n);1321}1322}1323 1324namespace capture_non_odr_used_variable_because_named_in_instantiation_dependent_expressions {1325 1326// even though 'x' is not odr-used, it should be captured.1327 1328int test() {1329 const int x = 10;1330 auto L = [=](auto a) {1331 (void) +x + a;1332 };1333 ASSERT_CLOSURE_SIZE_EXACT(L, sizeof(x));1334}1335 1336} //end ns1337#ifdef MS_EXTENSIONS1338namespace explicit_spec {1339template<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;1344 }1345 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);1355 };1356 };1357 auto M = L(1);1358 1359 ASSERT_NO_CAPTURES(M);1360 return 0;1361 }1362 1363}; 1364 1365int run_char = X<int>{}.foo('a');1366int run_int = X<double>{}.foo(4);1367}1368#endif // MS_EXTENSIONS1369 1370namespace nsdmi_capturing_this {1371struct X {1372 int m = 10;1373 int n = [this](auto) { return m; }(20);1374};1375 1376template<class T>1377struct XT {1378 T m = 10;1379 T n = [this](auto) { return m; }(20);1380};1381 1382XT<int> xt{};1383 1384 1385}1386 1387void PR33318(int i) {1388 [&](auto) { static_assert(&i != nullptr, ""); }(0); // expected-warning 2{{always true}} expected-note {{instantiation}}1389}1390 1391// Check to make sure that we don't capture when member-calls are made to members that are not of 'this' class.1392namespace PR34266 {1393// https://bugs.llvm.org/show_bug.cgi?id=342661394namespace ns1 {1395struct A {1396 static void bar(int) { }1397 static void bar(double) { }1398};1399 1400struct B 1401{1402 template<class T>1403 auto f() {1404 auto L = [=] { 1405 T{}.bar(3.0); 1406 T::bar(3);1407 1408 };1409 ASSERT_NO_CAPTURES(L);1410 return L;1411 };1412};1413 1414void test() {1415 B{}.f<A>();1416}1417} // end ns11418 1419namespace ns2 {1420struct A {1421 static void bar(int) { }1422 static void bar(double) { }1423};1424 1425struct B 1426{1427 using T = A;1428 auto f() {1429 auto L = [=](auto a) { 1430 T{}.bar(a); 1431 T::bar(a);1432 1433 };1434 ASSERT_NO_CAPTURES(L);1435 return L;1436 };1437};1438 1439void test() {1440 B{}.f()(3.0);1441 B{}.f()(3);1442}1443} // end ns21444 1445namespace ns3 {1446struct A {1447 void bar(int) { }1448 static void bar(double) { }1449};1450 1451struct B 1452{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.1458 1459 };1460 ASSERT_NO_CAPTURES(L);1461 return L;1462 };1463};1464 1465void test() {1466 B{}.f()(3.0);1467 B{}.f()(3); 1468}1469 1470} // end ns31471 1472 1473namespace ns4 {1474struct A {1475 void bar(int) { }1476 static void bar(double) { }1477};1478 1479struct B : A1480{1481 using T = A;1482 auto f() {1483 auto L = [=](auto a) { 1484 T{}.bar(a); 1485 T::bar(a); 1486 1487 };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;1491 };1492};1493 1494void test() {1495 B{}.f()(3.0);1496 B{}.f()(3); 1497}1498 1499} // end ns41500 1501namespace ns5 {1502struct A {1503 void bar(int) { }1504 static void bar(double) { }1505};1506 1507struct B 1508{1509 template<class T>1510 auto f() {1511 auto L = [&](auto a) { 1512 T{}.bar(a); 1513 T::bar(a); 1514 1515 };1516 1517 ASSERT_NO_CAPTURES(L);1518 return L;1519 };1520};1521 1522void test() {1523 B{}.f<A>()(3.0);1524 B{}.f<A>()(3); 1525}1526 1527} // end ns51528 1529} // end PR342661530 1531namespace capture_pack {1532#if __cplusplus >= 201702L1533 constexpr1534#endif1535 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 >= 201702L1543 static_assert(v == 123);1544#endif1545}1546