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