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 3 {{declared}} 69 [&]() { return e; }; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}} 70 [&e]() { return e; }; // expected-error 2 {{__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 {{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 auto pp = [p]{}; // expected-error {{deleted constructor}} 121 auto qq = [q]{}; // expected-error {{deleted function}} expected-note {{because}} 122 123 auto a = [r]{}; // expected-note 2{{here}} 124 decltype(a) b = a; 125 decltype(a) c = static_cast<decltype(a)&&>(a); // ok, copies R 126 a = a; // expected-error {{copy assignment operator is implicitly deleted}} 127 a = static_cast<decltype(a)&&>(a); // expected-error {{copy assignment operator is implicitly deleted}} 128 } 129 } 130 131 namespace PR12031 { 132 struct X { 133 template<typename T> 134 X(const T&); 135 ~X(); 136 }; 137 138 void f(int i, X x); 139 void g() { 140 const int v = 10; 141 f(v, [](){}); 142 } 143 } 144 145 namespace Array { 146 int &f(int *p); 147 char &f(...); 148 void g() { 149 int n = -1; 150 [=] { 151 int arr[n]; // VLA 152 } (); 153 154 const int m = -1; 155 [] { 156 int arr[m]; // expected-error{{negative size}} 157 } (); 158 159 [&] { 160 int arr[m]; // expected-error{{negative size}} 161 } (); 162 163 [=] { 164 int arr[m]; // expected-error{{negative size}} 165 } (); 166 167 [m] { 168 int arr[m]; // expected-error{{negative size}} 169 } (); 170 } 171 } 172 173 void PR12248() 174 { 175 unsigned int result = 0; 176 auto l = [&]() { ++result; }; 177 } 178 179 namespace ModifyingCapture { 180 void test() { 181 int n = 0; 182 [=] { 183 n = 1; // expected-error {{cannot assign to a variable captured by copy in a non-mutable lambda}} 184 }; 185 } 186 } 187 188 namespace VariadicPackExpansion { 189 template<typename T, typename U> using Fst = T; 190 template<typename...Ts> bool g(Fst<bool, Ts> ...bools); 191 template<typename...Ts> bool f(Ts &&...ts) { 192 return g<Ts...>([&ts] { 193 if (!ts) 194 return false; 195 --ts; 196 return true; 197 } () ...); 198 } 199 void h() { 200 int a = 5, b = 2, c = 3; 201 while (f(a, b, c)) { 202 } 203 } 204 205 struct sink { 206 template<typename...Ts> sink(Ts &&...) {} 207 }; 208 209 template<typename...Ts> void local_class() { 210 sink { 211 [] (Ts t) { 212 struct S : Ts { 213 void f(Ts t) { 214 Ts &that = *this; 215 that = t; 216 } 217 Ts g() { return *this; }; 218 }; 219 S s; 220 s.f(t); 221 return s; 222 } (Ts()).g() ... 223 }; 224 }; 225 struct X {}; struct Y {}; 226 template void local_class<X, Y>(); 227 228 template<typename...Ts> void nested(Ts ...ts) { 229 f( 230 // Each expansion of this lambda implicitly captures all of 'ts', because 231 // the inner lambda also expands 'ts'. 232 [&] { 233 return ts + [&] { return f(ts...); } (); 234 } () ... 235 ); 236 } 237 template void nested(int, int, int); 238 239 template<typename...Ts> void nested2(Ts ...ts) { // expected-note 2{{here}} 240 // Capture all 'ts', use only one. 241 f([&ts...] { return ts; } ()...); 242 // Capture each 'ts', use it. 243 f([&ts] { return ts; } ()...); 244 // Capture all 'ts', use all of them. 245 f([&ts...] { return (int)f(ts...); } ()); 246 // Capture each 'ts', use all of them. Ill-formed. In more detail: 247 // 248 // We instantiate two lambdas here; the first captures ts$0, the second 249 // captures ts$1. Both of them reference both ts parameters, so both are 250 // ill-formed because ts can't be implicitly captured. 251 // 252 // FIXME: This diagnostic does not explain what's happening. We should 253 // specify which 'ts' we're referring to in its diagnostic name. We should 254 // also say which slice of the pack expansion is being performed in the 255 // instantiation backtrace. 256 f([&ts] { return (int)f(ts...); } ()...); // \ 257 // expected-error 2{{'ts' cannot be implicitly captured}} \ 258 // expected-note 2{{lambda expression begins here}} 259 } 260 template void nested2(int); // ok 261 template void nested2(int, int); // expected-note {{in instantiation of}} 262 } 263 264 namespace PR13860 { 265 void foo() { 266 auto x = PR13860UndeclaredIdentifier(); // expected-error {{use of undeclared identifier 'PR13860UndeclaredIdentifier'}} 267 auto y = [x]() { }; 268 static_assert(sizeof(y), ""); 269 } 270 } 271 272 namespace PR13854 { 273 auto l = [](void){}; 274 } 275 276 namespace PR14518 { 277 auto f = [](void) { return __func__; }; // no-warning 278 } 279 280 namespace PR16708 { 281 auto L = []() { 282 auto ret = 0; 283 return ret; 284 return 0; 285 }; 286 } 287 288 namespace TypeDeduction { 289 struct S {}; 290 void f() { 291 const S s {}; 292 S &&t = [&] { return s; } (); 293 #if __cplusplus > 201103L 294 S &&u = [&] () -> auto { return s; } (); 295 #endif 296 } 297 } 298 299 300 namespace lambdas_in_NSDMIs { 301 template<class T> 302 struct L { 303 T t{}; 304 T t2 = ([](int a) { return [](int b) { return b; };})(t)(t); 305 }; 306 L<int> l; 307 308 namespace non_template { 309 struct L { 310 int t = 0; 311 int t2 = ([](int a) { return [](int b) { return b; };})(t)(t); 312 }; 313 L l; 314 } 315 } 316 317 // PR18477: don't try to capture 'this' from an NSDMI encountered while parsing 318 // a lambda. 319 namespace NSDMIs_in_lambdas { 320 template<typename T> struct S { int a = 0; int b = a; }; 321 void f() { []() { S<int> s; }; } 322 323 auto x = []{ struct S { int n, m = n; }; }; 324 auto y = [&]{ struct S { int n, m = n; }; }; // expected-error {{non-local lambda expression cannot have a capture-default}} 325 void g() { auto z = [&]{ struct S { int n, m = n; }; }; } 326 } 327 328 namespace CaptureIncomplete { 329 struct Incomplete; // expected-note 2{{forward decl}} 330 void g(const Incomplete &a); 331 void f(Incomplete &a) { 332 (void) [a] {}; // expected-error {{incomplete}} 333 (void) [&a] {}; 334 335 (void) [=] { g(a); }; // expected-error {{incomplete}} 336 (void) [&] { f(a); }; 337 } 338 } 339 340 namespace CaptureAbstract { 341 struct S { 342 virtual void f() = 0; // expected-note {{unimplemented}} 343 int n = 0; 344 }; 345 struct T : S { 346 constexpr T() {} 347 void f(); 348 }; 349 void f() { 350 constexpr T t = T(); 351 S &s = const_cast<T&>(t); 352 // FIXME: Once we properly compute odr-use per DR712, this should be 353 // accepted (and should not capture 's'). 354 [=] { return s.n; }; // expected-error {{abstract}} 355 } 356 } 357 358 namespace PR18128 { 359 auto l = [=]{}; // expected-error {{non-local lambda expression cannot have a capture-default}} 360 361 struct S { 362 int n; 363 int (*f())[true ? 1 : ([=]{ return n; }(), 0)]; 364 // expected-error@-1 {{non-local lambda expression cannot have a capture-default}} 365 // expected-error@-2 {{invalid use of non-static data member 'n'}} 366 // expected-error@-3 {{a lambda expression may not appear inside of a constant expression}} 367 int g(int k = ([=]{ return n; }(), 0)); 368 // expected-error@-1 {{non-local lambda expression cannot have a capture-default}} 369 // expected-error@-2 {{invalid use of non-static data member 'n'}} 370 371 int a = [=]{ return n; }(); // ok 372 int b = [=]{ return [=]{ return n; }(); }(); // ok 373 int c = []{ int k = 0; return [=]{ return k; }(); }(); // ok 374 int d = []{ return [=]{ return n; }(); }(); // expected-error {{'this' cannot be implicitly captured in this context}} 375 }; 376 } 377 378 namespace PR18473 { 379 template<typename T> void f() { 380 T t(0); 381 (void) [=]{ int n = t; }; // expected-error {{deleted}} 382 } 383 384 template void f<int>(); 385 struct NoCopy { 386 NoCopy(int); 387 NoCopy(const NoCopy &) = delete; // expected-note {{deleted}} 388 operator int() const; 389 }; 390 template void f<NoCopy>(); // expected-note {{instantiation}} 391 } 392 393 void PR19249() { 394 auto x = [&x]{}; // expected-error {{cannot appear in its own init}} 395 } 396 397 namespace PR20731 { 398 template <class L, int X = sizeof(L)> 399 void Job(L l); 400 401 template <typename... Args> 402 void Logger(Args &&... args) { 403 auto len = Invalid_Function((args)...); 404 // expected-error@-1 {{use of undeclared identifier 'Invalid_Function'}} 405 Job([len]() {}); 406 } 407 408 void GetMethod() { 409 Logger(); 410 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::Logger<>' requested here}} 411 } 412 413 template <typename T> 414 struct A { 415 T t; 416 // expected-error@-1 {{field has incomplete type 'void'}} 417 }; 418 419 template <typename F> 420 void g(F f) { 421 auto a = A<decltype(f())>{}; 422 // expected-note@-1 {{in instantiation of template class 'PR20731::A<void>' requested here}} 423 auto xf = [a, f]() {}; 424 int x = sizeof(xf); 425 }; 426 void f() { 427 g([] {}); 428 // expected-note-re@-1 {{in instantiation of function template specialization 'PR20731::g<(lambda at {{.*}}>' requested here}} 429 } 430 431 template <class _Rp> struct function { 432 template <class _Fp> 433 function(_Fp) { 434 static_assert(sizeof(_Fp) > 0, "Type must be complete."); 435 } 436 }; 437 438 template <typename T> void p(T t) { 439 auto l = some_undefined_function(t); 440 // expected-error@-1 {{use of undeclared identifier 'some_undefined_function'}} 441 function<void()>(([l]() {})); 442 } 443 void q() { p(0); } 444 // expected-note@-1 {{in instantiation of function template specialization 'PR20731::p<int>' requested here}} 445 } 446 447 namespace lambda_in_default_mem_init { 448 template<typename T> void f() { 449 struct S { int n = []{ return 0; }(); }; 450 } 451 template void f<int>(); 452 453 template<typename T> void g() { 454 struct S { int n = [](int n){ return n; }(0); }; 455 } 456 template void g<int>(); 457 } 458 459 namespace error_in_transform_prototype { 460 template<class T> 461 void f(T t) { 462 // expected-error@+2 {{type 'int' cannot be used prior to '::' because it has no members}} 463 // expected-error@+1 {{no member named 'ns' in 'error_in_transform_prototype::S'}} 464 auto x = [](typename T::ns::type &k) {}; 465 } 466 class S {}; 467 void foo() { 468 f(5); // expected-note {{requested here}} 469 f(S()); // expected-note {{requested here}} 470 } 471 } 472 473 namespace PR21857 { 474 template<typename Fn> struct fun : Fn { 475 fun() = default; 476 using Fn::operator(); 477 }; 478 template<typename Fn> fun<Fn> wrap(Fn fn); 479 auto x = wrap([](){}); 480 } 481 482 namespace PR13987 { 483 class Enclosing { 484 void Method(char c = []()->char { 485 int d = []()->int { 486 struct LocalClass { 487 int Method() { return 0; } 488 }; 489 return 0; 490 }(); 491 return d; }() 492 ); 493 }; 494 } 495 496 namespace PR23860 { 497 template <class> struct A { 498 void f(int x = []() { 499 struct B { 500 void g() {} 501 }; 502 return 0; 503 }()); 504 }; 505 506 int main() { 507 } 508 509 A<int> a; 510 } 511 512 // rdar://22032373 513 namespace rdar22032373 { 514 void foo() { 515 auto blk = [](bool b) { 516 if (b) 517 return undeclared_error; // expected-error {{use of undeclared identifier}} 518 return 0; 519 }; 520 } 521 } 522 523 namespace nested_lambda { 524 template <int N> 525 class S {}; 526 527 void foo() { 528 const int num = 18; 529 auto outer = []() { 530 auto inner = [](S<num> &X) {}; 531 }; 532 } 533 } 534 535 namespace PR27994 { 536 struct A { template <class T> A(T); }; 537 538 template <class T> 539 struct B { 540 int x; 541 A a = [&] { int y = x; }; 542 A b = [&] { [&] { [&] { int y = x; }; }; }; 543 A d = [&](auto param) { int y = x; }; 544 A e = [&](auto param) { [&] { [&](auto param2) { int y = x; }; }; }; 545 }; 546 547 B<int> b; 548 549 template <class T> struct C { 550 struct D { 551 int x; 552 A f = [&] { int y = x; }; 553 }; 554 }; 555 556 int func() { 557 C<int> a; 558 decltype(a)::D b; 559 } 560 } 561 562 namespace PR30566 { 563 int name1; // expected-note {{'name1' declared here}} 564 565 struct S1 { 566 template<class T> 567 S1(T t) { s = sizeof(t); } 568 int s; 569 }; 570 571 void foo1() { 572 auto s0 = S1{[name=]() {}}; // expected-error 2 {{expected expression}} 573 auto s1 = S1{[name=name]() {}}; // expected-error {{use of undeclared identifier 'name'; did you mean 'name1'?}} 574 } 575 } 576 577 namespace PR25627_dont_odr_use_local_consts { 578 579 template<int> struct X {}; 580 581 void foo() { 582 const int N = 10; 583 (void) [] { X<N> x; }; 584 } 585 } 586