15030928dSRichard Smith // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 -Wno-c99-designator %s
25030928dSRichard Smith // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++2a -Wno-c99-designator %s
3db0b9f12SDouglas Gregor 
4bfdb108fSRichard Smith enum E { e };
5bfdb108fSRichard Smith 
69e2f0a4fSRichard Smith constexpr int id(int n) { return n; }
79e2f0a4fSRichard Smith 
8db0b9f12SDouglas Gregor class C {
9db0b9f12SDouglas Gregor 
10db0b9f12SDouglas Gregor   int f() {
11db0b9f12SDouglas Gregor     int foo, bar;
12db0b9f12SDouglas Gregor 
13db0b9f12SDouglas Gregor     []; // expected-error {{expected body of lambda expression}}
14db0b9f12SDouglas Gregor     [+] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
15db0b9f12SDouglas Gregor     [foo+] {}; // expected-error {{expected ',' or ']' in lambda capture list}}
16db0b9f12SDouglas Gregor     [foo,&this] {}; // expected-error {{'this' cannot be captured by reference}}
17db0b9f12SDouglas Gregor     [&this] {}; // expected-error {{'this' cannot be captured by reference}}
18db0b9f12SDouglas Gregor     [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
19db0b9f12SDouglas Gregor     [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
20656bc62aSDouglas Gregor     [] {};
21656bc62aSDouglas Gregor     [=] (int i) {};
22656bc62aSDouglas Gregor     [&] (int) mutable -> void {};
23656bc62aSDouglas Gregor     [foo,bar] () { return 3; };
24656bc62aSDouglas Gregor     [=,&foo] () {};
25656bc62aSDouglas Gregor     [&,foo] () {};
26656bc62aSDouglas Gregor     [this] () {};
27bfdb108fSRichard Smith     [] () -> class C { return C(); };
28bfdb108fSRichard Smith     [] () -> enum E { return e; };
29db0b9f12SDouglas Gregor 
306746c5d4SDouglas Gregor     [] -> int { return 0; }; // expected-error{{lambda requires '()' before return type}}
316746c5d4SDouglas Gregor     [] mutable -> int { return 0; }; // expected-error{{lambda requires '()' before 'mutable'}}
32b3afa6c4SRichard Smith     [](int) -> {}; // PR13652 expected-error {{expected a type}}
33db0b9f12SDouglas Gregor     return 1;
34db0b9f12SDouglas Gregor   }
35db0b9f12SDouglas Gregor 
36a80cae11SDouglas Gregor   void designator_or_lambda() {
37a80cae11SDouglas Gregor     typedef int T;
38a80cae11SDouglas Gregor     const int b = 0;
39a80cae11SDouglas Gregor     const int c = 1;
409e2f0a4fSRichard Smith     int d;
41abe1a398SDavid Blaikie     int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '(lambda}}
42a80cae11SDouglas Gregor     int a2[1] = {[b] = 1 };
439e2f0a4fSRichard Smith     int a3[1] = {[b,c] = 1 }; // expected-error{{expected ']'}} expected-note {{to match}}
44a80cae11SDouglas Gregor     int a4[1] = {[&b] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'const int *'}}
45a80cae11SDouglas Gregor     int a5[3] = { []{return 0;}() };
46a80cae11SDouglas Gregor     int a6[1] = {[this] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'C *'}}
47f53d1727SNicolas Lesser     int a7[1] = {[d(0)] { return d; } ()};
48f53d1727SNicolas Lesser     int a8[1] = {[d = 0] { return d; } ()};
49f53d1727SNicolas Lesser     int a10[1] = {[id(0)] { return id; } ()};
50f53d1727SNicolas Lesser #if __cplusplus <= 201103L
51f53d1727SNicolas Lesser     // expected-warning@-4{{extension}}
52f53d1727SNicolas Lesser     // expected-warning@-4{{extension}}
53f53d1727SNicolas Lesser     // expected-warning@-4{{extension}}
54f53d1727SNicolas Lesser #endif
559e2f0a4fSRichard Smith     int a9[1] = {[d = 0] = 1}; // expected-error{{is not an integral constant expression}}
56f53d1727SNicolas Lesser #if __cplusplus >= 201402L
57f53d1727SNicolas Lesser     // expected-note@-2{{constant expression cannot modify an object that is visible outside that expression}}
58f53d1727SNicolas Lesser #endif
599e2f0a4fSRichard Smith     int a11[1] = {[id(0)] = 1};
60a80cae11SDouglas Gregor   }
6110c6072dSRichard Smith 
6210c6072dSRichard Smith   void delete_lambda(int *p) {
6310c6072dSRichard Smith     delete [] p;
6410c6072dSRichard Smith     delete [] (int*) { new int }; // ok, compound-literal, not lambda
65f53d1727SNicolas Lesser     delete [] { return new int; } (); // expected-error {{'[]' after delete interpreted as 'delete[]'}}
6610c6072dSRichard Smith     delete [&] { return new int; } (); // ok, lambda
67f53d1727SNicolas Lesser 
68f53d1727SNicolas Lesser     delete []() { return new int; }(); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
69f53d1727SNicolas Lesser     delete [](E Enum) { return new int((int)Enum); }(e); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
70f53d1727SNicolas Lesser #if __cplusplus > 201703L
71f53d1727SNicolas Lesser     delete []<int = 0>() { return new int; }(); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
72f53d1727SNicolas Lesser #endif
7310c6072dSRichard Smith   }
7421b3ab43SRichard Smith 
7521b3ab43SRichard Smith   // We support init-captures in C++11 as an extension.
7621b3ab43SRichard Smith   int z;
7721b3ab43SRichard Smith   void init_capture() {
78f53d1727SNicolas Lesser     [n(0)] () mutable -> int { return ++n; };
79f53d1727SNicolas Lesser     [n{0}] { return; };
80f53d1727SNicolas Lesser     [a([&b = z]{})](){};
81f53d1727SNicolas Lesser     [n = 0] { return ++n; }; // expected-error {{captured by copy in a non-mutable}}
82f53d1727SNicolas Lesser     [n = {0}] { return; }; // expected-error {{<initializer_list>}}
83f53d1727SNicolas Lesser #if __cplusplus <= 201103L
84f53d1727SNicolas Lesser     // expected-warning@-6{{extension}}
85f53d1727SNicolas Lesser     // expected-warning@-6{{extension}}
86f53d1727SNicolas Lesser     // expected-warning@-6{{extension}}
87f53d1727SNicolas Lesser     // expected-warning@-7{{extension}}
88f53d1727SNicolas Lesser     // expected-warning@-7{{extension}}
89f53d1727SNicolas Lesser     // expected-warning@-7{{extension}}
90f53d1727SNicolas Lesser #endif
9121b3ab43SRichard Smith 
92ba71c085SRichard Smith     int x = 4;
93f53d1727SNicolas Lesser     auto y = [&r = x, x = x + 1]() -> int {
94f53d1727SNicolas Lesser #if __cplusplus <= 201103L
95f53d1727SNicolas Lesser       // expected-warning@-2{{extension}}
96f53d1727SNicolas Lesser       // expected-warning@-3{{extension}}
97f53d1727SNicolas Lesser #endif
98bb13c9a4SRichard Smith       r += 2;
99bb13c9a4SRichard Smith       return x + 2;
10021b3ab43SRichard Smith     } ();
10121b3ab43SRichard Smith   }
1027e1fd018SAaron Ballman 
1037e1fd018SAaron Ballman   void attributes() {
104e8d69b7fSAaron Ballman     [] __attribute__((noreturn)) {}; // expected-error {{lambda requires '()' before attribute specifier}}
1057e1fd018SAaron Ballman     []() [[]]
1067e1fd018SAaron Ballman       mutable {}; // expected-error {{expected body of lambda expression}}
1077e1fd018SAaron Ballman 
1087e1fd018SAaron Ballman     []() [[]] {};
1097e1fd018SAaron Ballman     []() [[]] -> void {};
1107e1fd018SAaron Ballman     []() mutable [[]] -> void {};
1117e1fd018SAaron Ballman     []() mutable noexcept [[]] -> void {};
1127e1fd018SAaron Ballman 
113e8d69b7fSAaron Ballman     // Testing GNU-style attributes on lambdas -- the attribute is specified
114e8d69b7fSAaron Ballman     // before the mutable specifier instead of after (unlike C++11).
115e8d69b7fSAaron Ballman     []() __attribute__((noreturn)) mutable { while(1); };
116e8d69b7fSAaron Ballman     []() mutable
117e8d69b7fSAaron Ballman       __attribute__((noreturn)) { while(1); }; // expected-error {{expected body of lambda expression}}
118*b2bc0a32SAaron Ballman 
119*b2bc0a32SAaron Ballman     // Testing support for P2173 on adding attributes to the declaration
120*b2bc0a32SAaron Ballman     // rather than the type.
121*b2bc0a32SAaron Ballman     [] [[]] () {}; // expected-warning {{an attribute specifier sequence in this position is a C++2b extension}}
122*b2bc0a32SAaron Ballman #if __cplusplus > 201703L
123*b2bc0a32SAaron Ballman     [] <typename> [[]] () {};  // expected-warning {{an attribute specifier sequence in this position is a C++2b extension}}
124*b2bc0a32SAaron Ballman #endif
125*b2bc0a32SAaron Ballman     [] [[]] {}; // expected-warning {{an attribute specifier sequence in this position is a C++2b extension}}
1267e1fd018SAaron Ballman   }
127db0b9f12SDouglas Gregor };
128e01c466cSDavid Majnemer 
129e01c466cSDavid Majnemer template <typename>
130e01c466cSDavid Majnemer void PR22122() {
131e01c466cSDavid Majnemer   [](int) -> {}; // expected-error {{expected a type}}
132e01c466cSDavid Majnemer }
133e01c466cSDavid Majnemer 
134e01c466cSDavid Majnemer template void PR22122<int>();
13589296ee2SDavid Majnemer 
136be19c48fSErik Pilkington namespace PR42778 {
137be19c48fSErik Pilkington struct A {
138be19c48fSErik Pilkington   template <class F> A(F&&) {}
139be19c48fSErik Pilkington };
140be19c48fSErik Pilkington 
141be19c48fSErik Pilkington struct S {
142be19c48fSErik Pilkington   void mf() { A{[*this]{}}; }
143be19c48fSErik Pilkington #if __cplusplus < 201703L
144be19c48fSErik Pilkington   // expected-warning@-2 {{C++17 extension}}
145be19c48fSErik Pilkington #endif
146be19c48fSErik Pilkington };
147be19c48fSErik Pilkington }
148be19c48fSErik Pilkington 
14989296ee2SDavid Majnemer struct S {
15089296ee2SDavid Majnemer   template <typename T>
15189296ee2SDavid Majnemer   void m (T x =[0); // expected-error{{expected variable name or 'this' in lambda capture list}}
15289296ee2SDavid Majnemer } s;
153a3aef35dSDavid Majnemer 
154a3aef35dSDavid Majnemer struct U {
155a3aef35dSDavid Majnemer   template <typename T>
156a3aef35dSDavid Majnemer   void m_fn1(T x = 0[0); // expected-error{{expected ']'}} expected-note{{to match this '['}}
157a3aef35dSDavid Majnemer } *U;
158