126f4b32fSDouglas Gregor // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 %s
2db0b9f12SDouglas Gregor 
3bfdb108fSRichard Smith enum E { e };
4bfdb108fSRichard Smith 
5*9e2f0a4fSRichard Smith constexpr int id(int n) { return n; }
6*9e2f0a4fSRichard Smith 
7db0b9f12SDouglas Gregor class C {
8db0b9f12SDouglas Gregor 
9db0b9f12SDouglas Gregor   int f() {
10db0b9f12SDouglas Gregor     int foo, bar;
11db0b9f12SDouglas Gregor 
12db0b9f12SDouglas Gregor     []; // expected-error {{expected body of lambda expression}}
13db0b9f12SDouglas Gregor     [+] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
14db0b9f12SDouglas Gregor     [foo+] {}; // expected-error {{expected ',' or ']' in lambda capture list}}
15db0b9f12SDouglas Gregor     [foo,&this] {}; // expected-error {{'this' cannot be captured by reference}}
16db0b9f12SDouglas Gregor     [&this] {}; // expected-error {{'this' cannot be captured by reference}}
17db0b9f12SDouglas Gregor     [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
18db0b9f12SDouglas Gregor     [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
19656bc62aSDouglas Gregor     [] {};
20656bc62aSDouglas Gregor     [=] (int i) {};
21656bc62aSDouglas Gregor     [&] (int) mutable -> void {};
22656bc62aSDouglas Gregor     [foo,bar] () { return 3; };
23656bc62aSDouglas Gregor     [=,&foo] () {};
24656bc62aSDouglas Gregor     [&,foo] () {};
25656bc62aSDouglas Gregor     [this] () {};
26bfdb108fSRichard Smith     [] () -> class C { return C(); };
27bfdb108fSRichard Smith     [] () -> enum E { return e; };
28db0b9f12SDouglas Gregor 
296746c5d4SDouglas Gregor     [] -> int { return 0; }; // expected-error{{lambda requires '()' before return type}}
306746c5d4SDouglas Gregor     [] mutable -> int { return 0; }; // expected-error{{lambda requires '()' before 'mutable'}}
31b3afa6c4SRichard Smith     [](int) -> {}; // PR13652 expected-error {{expected a type}}
32db0b9f12SDouglas Gregor     return 1;
33db0b9f12SDouglas Gregor   }
34db0b9f12SDouglas Gregor 
35a80cae11SDouglas Gregor   void designator_or_lambda() {
36a80cae11SDouglas Gregor     typedef int T;
37a80cae11SDouglas Gregor     const int b = 0;
38a80cae11SDouglas Gregor     const int c = 1;
39*9e2f0a4fSRichard Smith     int d;
40abe1a398SDavid Blaikie     int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '(lambda}}
41a80cae11SDouglas Gregor     int a2[1] = {[b] = 1 };
42*9e2f0a4fSRichard Smith     int a3[1] = {[b,c] = 1 }; // expected-error{{expected ']'}} expected-note {{to match}}
43a80cae11SDouglas Gregor     int a4[1] = {[&b] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'const int *'}}
44a80cae11SDouglas Gregor     int a5[3] = { []{return 0;}() };
45a80cae11SDouglas Gregor     int a6[1] = {[this] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'C *'}}
46*9e2f0a4fSRichard Smith     int a7[1] = {[d(0)] { return d; } ()}; // expected-warning{{extension}}
47*9e2f0a4fSRichard Smith     int a8[1] = {[d = 0] { return d; } ()}; // expected-warning{{extension}}
48*9e2f0a4fSRichard Smith     int a9[1] = {[d = 0] = 1}; // expected-error{{is not an integral constant expression}}
49*9e2f0a4fSRichard Smith     int a10[1] = {[id(0)] { return id; } ()}; // expected-warning{{extension}}
50*9e2f0a4fSRichard Smith     int a11[1] = {[id(0)] = 1};
51a80cae11SDouglas Gregor   }
5210c6072dSRichard Smith 
5310c6072dSRichard Smith   void delete_lambda(int *p) {
5410c6072dSRichard Smith     delete [] p;
5510c6072dSRichard Smith     delete [] (int*) { new int }; // ok, compound-literal, not lambda
5610c6072dSRichard Smith     delete [] { return new int; } (); // expected-error{{expected expression}}
5710c6072dSRichard Smith     delete [&] { return new int; } (); // ok, lambda
5810c6072dSRichard Smith   }
5921b3ab43SRichard Smith 
6021b3ab43SRichard Smith   // We support init-captures in C++11 as an extension.
6121b3ab43SRichard Smith   int z;
6221b3ab43SRichard Smith   void init_capture() {
635b013f50SRichard Smith     [n(0)] () mutable -> int { return ++n; }; // expected-warning{{extension}}
645b013f50SRichard Smith     [n{0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}}
655b013f50SRichard Smith     [n = 0] { return ++n; }; // expected-error {{captured by copy in a non-mutable}} expected-warning{{extension}}
665b013f50SRichard Smith     [n = {0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}}
675b013f50SRichard Smith     [a([&b = z]{})](){}; // expected-warning 2{{extension}}
6821b3ab43SRichard Smith 
69ba71c085SRichard Smith     int x = 4;
705b013f50SRichard Smith     auto y = [&r = x, x = x + 1]() -> int { // expected-warning 2{{extension}}
71bb13c9a4SRichard Smith       r += 2;
72bb13c9a4SRichard Smith       return x + 2;
7321b3ab43SRichard Smith     } ();
7421b3ab43SRichard Smith   }
757e1fd018SAaron Ballman 
767e1fd018SAaron Ballman   void attributes() {
777e1fd018SAaron Ballman     [] [[]] {}; // expected-error {{lambda requires '()' before attribute specifier}}
78e8d69b7fSAaron Ballman     [] __attribute__((noreturn)) {}; // expected-error {{lambda requires '()' before attribute specifier}}
797e1fd018SAaron Ballman     []() [[]]
807e1fd018SAaron Ballman       mutable {}; // expected-error {{expected body of lambda expression}}
817e1fd018SAaron Ballman 
827e1fd018SAaron Ballman     []() [[]] {};
837e1fd018SAaron Ballman     []() [[]] -> void {};
847e1fd018SAaron Ballman     []() mutable [[]] -> void {};
857e1fd018SAaron Ballman     []() mutable noexcept [[]] -> void {};
867e1fd018SAaron Ballman 
87e8d69b7fSAaron Ballman     // Testing GNU-style attributes on lambdas -- the attribute is specified
88e8d69b7fSAaron Ballman     // before the mutable specifier instead of after (unlike C++11).
89e8d69b7fSAaron Ballman     []() __attribute__((noreturn)) mutable { while(1); };
90e8d69b7fSAaron Ballman     []() mutable
91e8d69b7fSAaron Ballman       __attribute__((noreturn)) { while(1); }; // expected-error {{expected body of lambda expression}}
927e1fd018SAaron Ballman   }
93db0b9f12SDouglas Gregor };
94