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