126f4b32fSDouglas Gregor // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 %s 2db0b9f12SDouglas Gregor 3bfdb108fSRichard Smith enum E { e }; 4bfdb108fSRichard Smith 5db0b9f12SDouglas Gregor class C { 6db0b9f12SDouglas Gregor 7db0b9f12SDouglas Gregor int f() { 8db0b9f12SDouglas Gregor int foo, bar; 9db0b9f12SDouglas Gregor 10db0b9f12SDouglas Gregor []; // expected-error {{expected body of lambda expression}} 11db0b9f12SDouglas Gregor [+] {}; // expected-error {{expected variable name or 'this' in lambda capture list}} 12db0b9f12SDouglas Gregor [foo+] {}; // expected-error {{expected ',' or ']' in lambda capture list}} 13db0b9f12SDouglas Gregor [foo,&this] {}; // expected-error {{'this' cannot be captured by reference}} 14db0b9f12SDouglas Gregor [&this] {}; // expected-error {{'this' cannot be captured by reference}} 15db0b9f12SDouglas Gregor [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}} 16db0b9f12SDouglas Gregor [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}} 17656bc62aSDouglas Gregor [] {}; 18656bc62aSDouglas Gregor [=] (int i) {}; 19656bc62aSDouglas Gregor [&] (int) mutable -> void {}; 20656bc62aSDouglas Gregor [foo,bar] () { return 3; }; 21656bc62aSDouglas Gregor [=,&foo] () {}; 22656bc62aSDouglas Gregor [&,foo] () {}; 23656bc62aSDouglas Gregor [this] () {}; 24bfdb108fSRichard Smith [] () -> class C { return C(); }; 25bfdb108fSRichard Smith [] () -> enum E { return e; }; 26db0b9f12SDouglas Gregor 276746c5d4SDouglas Gregor [] -> int { return 0; }; // expected-error{{lambda requires '()' before return type}} 286746c5d4SDouglas Gregor [] mutable -> int { return 0; }; // expected-error{{lambda requires '()' before 'mutable'}} 29b3afa6c4SRichard Smith [](int) -> {}; // PR13652 expected-error {{expected a type}} 30db0b9f12SDouglas Gregor return 1; 31db0b9f12SDouglas Gregor } 32db0b9f12SDouglas Gregor 33a80cae11SDouglas Gregor void designator_or_lambda() { 34a80cae11SDouglas Gregor typedef int T; 35a80cae11SDouglas Gregor const int b = 0; 36a80cae11SDouglas Gregor const int c = 1; 37be20d432SEli Friedman int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '<lambda}} 38a80cae11SDouglas Gregor int a2[1] = {[b] = 1 }; 39a80cae11SDouglas Gregor int a3[1] = {[b,c] = 1 }; // expected-error{{expected body of lambda expression}} 40a80cae11SDouglas Gregor int a4[1] = {[&b] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'const int *'}} 41a80cae11SDouglas Gregor int a5[3] = { []{return 0;}() }; 42a80cae11SDouglas Gregor int a6[1] = {[this] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'C *'}} 43a80cae11SDouglas Gregor } 4410c6072dSRichard Smith 4510c6072dSRichard Smith void delete_lambda(int *p) { 4610c6072dSRichard Smith delete [] p; 4710c6072dSRichard Smith delete [] (int*) { new int }; // ok, compound-literal, not lambda 4810c6072dSRichard Smith delete [] { return new int; } (); // expected-error{{expected expression}} 4910c6072dSRichard Smith delete [&] { return new int; } (); // ok, lambda 5010c6072dSRichard Smith } 5121b3ab43SRichard Smith 5221b3ab43SRichard Smith // We support init-captures in C++11 as an extension. 5321b3ab43SRichard Smith int z; 5421b3ab43SRichard Smith void init_capture() { 555b013f50SRichard Smith [n(0)] () mutable -> int { return ++n; }; // expected-warning{{extension}} 565b013f50SRichard Smith [n{0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}} 575b013f50SRichard Smith [n = 0] { return ++n; }; // expected-error {{captured by copy in a non-mutable}} expected-warning{{extension}} 585b013f50SRichard Smith [n = {0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}} 595b013f50SRichard Smith [a([&b = z]{})](){}; // expected-warning 2{{extension}} 6021b3ab43SRichard Smith 61ba71c085SRichard Smith int x = 4; 625b013f50SRichard Smith auto y = [&r = x, x = x + 1]() -> int { // expected-warning 2{{extension}} 63bb13c9a4SRichard Smith r += 2; 64bb13c9a4SRichard Smith return x + 2; 6521b3ab43SRichard Smith } (); 6621b3ab43SRichard Smith } 677e1fd018SAaron Ballman 687e1fd018SAaron Ballman void attributes() { 697e1fd018SAaron Ballman [] [[]] {}; // expected-error {{lambda requires '()' before attribute specifier}} 70*e8d69b7fSAaron Ballman [] __attribute__((noreturn)) {}; // expected-error {{lambda requires '()' before attribute specifier}} 717e1fd018SAaron Ballman []() [[]] 727e1fd018SAaron Ballman mutable {}; // expected-error {{expected body of lambda expression}} 737e1fd018SAaron Ballman 747e1fd018SAaron Ballman []() [[]] {}; 757e1fd018SAaron Ballman []() [[]] -> void {}; 767e1fd018SAaron Ballman []() mutable [[]] -> void {}; 777e1fd018SAaron Ballman []() mutable noexcept [[]] -> void {}; 787e1fd018SAaron Ballman 79*e8d69b7fSAaron Ballman // Testing GNU-style attributes on lambdas -- the attribute is specified 80*e8d69b7fSAaron Ballman // before the mutable specifier instead of after (unlike C++11). 81*e8d69b7fSAaron Ballman []() __attribute__((noreturn)) mutable { while(1); }; 82*e8d69b7fSAaron Ballman []() mutable 83*e8d69b7fSAaron Ballman __attribute__((noreturn)) { while(1); }; // expected-error {{expected body of lambda expression}} 847e1fd018SAaron Ballman } 85db0b9f12SDouglas Gregor }; 86