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
3*0620e6f4SMarek Kurdej // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++2b -Wno-c99-designator %s
4db0b9f12SDouglas Gregor
5bfdb108fSRichard Smith enum E { e };
6bfdb108fSRichard Smith
id(int n)79e2f0a4fSRichard Smith constexpr int id(int n) { return n; }
89e2f0a4fSRichard Smith
9db0b9f12SDouglas Gregor class C {
10db0b9f12SDouglas Gregor
f()11db0b9f12SDouglas Gregor int f() {
12db0b9f12SDouglas Gregor int foo, bar;
13db0b9f12SDouglas Gregor
14db0b9f12SDouglas Gregor []; // expected-error {{expected body of lambda expression}}
15db0b9f12SDouglas Gregor [+] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
16db0b9f12SDouglas Gregor [foo+] {}; // expected-error {{expected ',' or ']' in lambda capture list}}
17db0b9f12SDouglas Gregor [foo,&this] {}; // expected-error {{'this' cannot be captured by reference}}
18db0b9f12SDouglas Gregor [&this] {}; // expected-error {{'this' cannot be captured by reference}}
19db0b9f12SDouglas Gregor [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
20db0b9f12SDouglas Gregor [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
21656bc62aSDouglas Gregor [] {};
22656bc62aSDouglas Gregor [=] (int i) {};
23656bc62aSDouglas Gregor [&] (int) mutable -> void {};
24656bc62aSDouglas Gregor [foo,bar] () { return 3; };
25656bc62aSDouglas Gregor [=,&foo] () {};
26656bc62aSDouglas Gregor [&,foo] () {};
27656bc62aSDouglas Gregor [this] () {};
28bfdb108fSRichard Smith [] () -> class C { return C(); };
29bfdb108fSRichard Smith [] () -> enum E { return e; };
30db0b9f12SDouglas Gregor
31*0620e6f4SMarek Kurdej [] -> int { return 0; };
32*0620e6f4SMarek Kurdej [] mutable -> int { return 0; };
33*0620e6f4SMarek Kurdej #if __cplusplus <= 202002L
34*0620e6f4SMarek Kurdej // expected-warning@-3 {{lambda without a parameter clause is a C++2b extension}}
35*0620e6f4SMarek Kurdej // expected-warning@-3 {{is a C++2b extension}}
36*0620e6f4SMarek Kurdej #endif
37b3afa6c4SRichard Smith [](int) -> {}; // PR13652 expected-error {{expected a type}}
38db0b9f12SDouglas Gregor return 1;
39db0b9f12SDouglas Gregor }
40db0b9f12SDouglas Gregor
designator_or_lambda()41a80cae11SDouglas Gregor void designator_or_lambda() {
42a80cae11SDouglas Gregor typedef int T;
43a80cae11SDouglas Gregor const int b = 0;
44a80cae11SDouglas Gregor const int c = 1;
459e2f0a4fSRichard Smith int d;
46abe1a398SDavid Blaikie int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '(lambda}}
47a80cae11SDouglas Gregor int a2[1] = {[b] = 1 };
489e2f0a4fSRichard Smith int a3[1] = {[b,c] = 1 }; // expected-error{{expected ']'}} expected-note {{to match}}
49a80cae11SDouglas Gregor int a4[1] = {[&b] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'const int *'}}
50a80cae11SDouglas Gregor int a5[3] = { []{return 0;}() };
51a80cae11SDouglas Gregor int a6[1] = {[this] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'C *'}}
52f53d1727SNicolas Lesser int a7[1] = {[d(0)] { return d; } ()};
53f53d1727SNicolas Lesser int a8[1] = {[d = 0] { return d; } ()};
54f53d1727SNicolas Lesser int a10[1] = {[id(0)] { return id; } ()};
55f53d1727SNicolas Lesser #if __cplusplus <= 201103L
56f53d1727SNicolas Lesser // expected-warning@-4{{extension}}
57f53d1727SNicolas Lesser // expected-warning@-4{{extension}}
58f53d1727SNicolas Lesser // expected-warning@-4{{extension}}
59f53d1727SNicolas Lesser #endif
609e2f0a4fSRichard Smith int a9[1] = {[d = 0] = 1}; // expected-error{{is not an integral constant expression}}
61f53d1727SNicolas Lesser #if __cplusplus >= 201402L
62f53d1727SNicolas Lesser // expected-note@-2{{constant expression cannot modify an object that is visible outside that expression}}
63f53d1727SNicolas Lesser #endif
649e2f0a4fSRichard Smith int a11[1] = {[id(0)] = 1};
65a80cae11SDouglas Gregor }
6610c6072dSRichard Smith
delete_lambda(int * p)6710c6072dSRichard Smith void delete_lambda(int *p) {
6810c6072dSRichard Smith delete [] p;
6910c6072dSRichard Smith delete [] (int*) { new int }; // ok, compound-literal, not lambda
70f53d1727SNicolas Lesser delete [] { return new int; } (); // expected-error {{'[]' after delete interpreted as 'delete[]'}}
7110c6072dSRichard Smith delete [&] { return new int; } (); // ok, lambda
72f53d1727SNicolas Lesser
73f53d1727SNicolas Lesser delete []() { return new int; }(); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
74f53d1727SNicolas Lesser delete [](E Enum) { return new int((int)Enum); }(e); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
75f53d1727SNicolas Lesser #if __cplusplus > 201703L
76f53d1727SNicolas Lesser delete []<int = 0>() { return new int; }(); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
77f53d1727SNicolas Lesser #endif
7810c6072dSRichard Smith }
7921b3ab43SRichard Smith
8021b3ab43SRichard Smith // We support init-captures in C++11 as an extension.
8121b3ab43SRichard Smith int z;
init_capture()8221b3ab43SRichard Smith void init_capture() {
83f53d1727SNicolas Lesser [n(0)] () mutable -> int { return ++n; };
84f53d1727SNicolas Lesser [n{0}] { return; };
85f53d1727SNicolas Lesser [a([&b = z]{})](){};
86f53d1727SNicolas Lesser [n = 0] { return ++n; }; // expected-error {{captured by copy in a non-mutable}}
87f53d1727SNicolas Lesser [n = {0}] { return; }; // expected-error {{<initializer_list>}}
88f53d1727SNicolas Lesser #if __cplusplus <= 201103L
89f53d1727SNicolas Lesser // expected-warning@-6{{extension}}
90f53d1727SNicolas Lesser // expected-warning@-6{{extension}}
91f53d1727SNicolas Lesser // expected-warning@-6{{extension}}
92f53d1727SNicolas Lesser // expected-warning@-7{{extension}}
93f53d1727SNicolas Lesser // expected-warning@-7{{extension}}
94f53d1727SNicolas Lesser // expected-warning@-7{{extension}}
95f53d1727SNicolas Lesser #endif
9621b3ab43SRichard Smith
97ba71c085SRichard Smith int x = 4;
98f53d1727SNicolas Lesser auto y = [&r = x, x = x + 1]() -> int {
99f53d1727SNicolas Lesser #if __cplusplus <= 201103L
100f53d1727SNicolas Lesser // expected-warning@-2{{extension}}
101f53d1727SNicolas Lesser // expected-warning@-3{{extension}}
102f53d1727SNicolas Lesser #endif
103bb13c9a4SRichard Smith r += 2;
104bb13c9a4SRichard Smith return x + 2;
10521b3ab43SRichard Smith } ();
10621b3ab43SRichard Smith }
1077e1fd018SAaron Ballman
attributes()1087e1fd018SAaron Ballman void attributes() {
109*0620e6f4SMarek Kurdej [] __attribute__((noreturn)){};
110*0620e6f4SMarek Kurdej #if __cplusplus <= 202002L
111*0620e6f4SMarek Kurdej // expected-warning@-2 {{is a C++2b extension}}
112*0620e6f4SMarek Kurdej #endif
1137e1fd018SAaron Ballman []() [[]]
1147e1fd018SAaron Ballman mutable {}; // expected-error {{expected body of lambda expression}}
1157e1fd018SAaron Ballman
1167e1fd018SAaron Ballman []() [[]] {};
1177e1fd018SAaron Ballman []() [[]] -> void {};
1187e1fd018SAaron Ballman []() mutable [[]] -> void {};
1197e1fd018SAaron Ballman []() mutable noexcept [[]] -> void {};
1207e1fd018SAaron Ballman
121e8d69b7fSAaron Ballman // Testing GNU-style attributes on lambdas -- the attribute is specified
122e8d69b7fSAaron Ballman // before the mutable specifier instead of after (unlike C++11).
123e8d69b7fSAaron Ballman []() __attribute__((noreturn)) mutable { while(1); };
124e8d69b7fSAaron Ballman []() mutable
125e8d69b7fSAaron Ballman __attribute__((noreturn)) { while(1); }; // expected-error {{expected body of lambda expression}}
126b2bc0a32SAaron Ballman
127b2bc0a32SAaron Ballman // Testing support for P2173 on adding attributes to the declaration
128b2bc0a32SAaron Ballman // rather than the type.
129*0620e6f4SMarek Kurdej [][[]](){};
130*0620e6f4SMarek Kurdej #if __cplusplus <= 202002L
131*0620e6f4SMarek Kurdej // expected-warning@-2 {{an attribute specifier sequence in this position is a C++2b extension}}
132b2bc0a32SAaron Ballman #endif
133*0620e6f4SMarek Kurdej #if __cplusplus > 201703L
134*0620e6f4SMarek Kurdej []<typename>[[]](){};
135*0620e6f4SMarek Kurdej #if __cplusplus <= 202002L
136*0620e6f4SMarek Kurdej // expected-warning@-2 {{an attribute specifier sequence in this position is a C++2b extension}}
137*0620e6f4SMarek Kurdej #endif
138*0620e6f4SMarek Kurdej #endif
139*0620e6f4SMarek Kurdej [][[]]{};
140*0620e6f4SMarek Kurdej #if __cplusplus <= 202002L
141*0620e6f4SMarek Kurdej // expected-warning@-2 {{an attribute specifier sequence in this position is a C++2b extension}}
142*0620e6f4SMarek Kurdej #endif
143*0620e6f4SMarek Kurdej }
144*0620e6f4SMarek Kurdej
missing_parens()145*0620e6f4SMarek Kurdej void missing_parens() {
146*0620e6f4SMarek Kurdej [] mutable {};
147*0620e6f4SMarek Kurdej [] noexcept {};
148*0620e6f4SMarek Kurdej #if __cplusplus <= 202002L
149*0620e6f4SMarek Kurdej // expected-warning@-3 {{is a C++2b extension}}
150*0620e6f4SMarek Kurdej // expected-warning@-3 {{is a C++2b extension}}
151*0620e6f4SMarek Kurdej #endif
1527e1fd018SAaron Ballman }
153db0b9f12SDouglas Gregor };
154e01c466cSDavid Majnemer
155e01c466cSDavid Majnemer template <typename>
PR22122()156e01c466cSDavid Majnemer void PR22122() {
157e01c466cSDavid Majnemer [](int) -> {}; // expected-error {{expected a type}}
158e01c466cSDavid Majnemer }
159e01c466cSDavid Majnemer
160e01c466cSDavid Majnemer template void PR22122<int>();
16189296ee2SDavid Majnemer
162be19c48fSErik Pilkington namespace PR42778 {
163be19c48fSErik Pilkington struct A {
APR42778::A164be19c48fSErik Pilkington template <class F> A(F&&) {}
165be19c48fSErik Pilkington };
166be19c48fSErik Pilkington
167be19c48fSErik Pilkington struct S {
__anonefc70a885202null168be19c48fSErik Pilkington void mf() { A{[*this]{}}; }
169be19c48fSErik Pilkington #if __cplusplus < 201703L
170be19c48fSErik Pilkington // expected-warning@-2 {{C++17 extension}}
171be19c48fSErik Pilkington #endif
172be19c48fSErik Pilkington };
173be19c48fSErik Pilkington }
174be19c48fSErik Pilkington
17589296ee2SDavid Majnemer struct S {
17689296ee2SDavid Majnemer template <typename T>
17789296ee2SDavid Majnemer void m (T x =[0); // expected-error{{expected variable name or 'this' in lambda capture list}}
17889296ee2SDavid Majnemer } s;
179a3aef35dSDavid Majnemer
180a3aef35dSDavid Majnemer struct U {
181a3aef35dSDavid Majnemer template <typename T>
182a3aef35dSDavid Majnemer void m_fn1(T x = 0[0); // expected-error{{expected ']'}} expected-note{{to match this '['}}
183a3aef35dSDavid Majnemer } *U;
184