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