xref: /llvm-project/clang/test/Parser/cxx0x-lambda-expressions.cpp (revision 0620e6f4b76a9725dbd82454d58c5a68a7e47074)
1 // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 -Wno-c99-designator %s
2 // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++2a -Wno-c99-designator %s
3 // RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++2b -Wno-c99-designator %s
4 
5 enum E { e };
6 
7 constexpr int id(int n) { return n; }
8 
9 class C {
10 
11   int f() {
12     int foo, bar;
13 
14     []; // expected-error {{expected body of lambda expression}}
15     [+] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
16     [foo+] {}; // expected-error {{expected ',' or ']' in lambda capture list}}
17     [foo,&this] {}; // expected-error {{'this' cannot be captured by reference}}
18     [&this] {}; // expected-error {{'this' cannot be captured by reference}}
19     [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
20     [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
21     [] {};
22     [=] (int i) {};
23     [&] (int) mutable -> void {};
24     [foo,bar] () { return 3; };
25     [=,&foo] () {};
26     [&,foo] () {};
27     [this] () {};
28     [] () -> class C { return C(); };
29     [] () -> enum E { return e; };
30 
31     [] -> int { return 0; };
32     [] mutable -> int { return 0; };
33 #if __cplusplus <= 202002L
34     // expected-warning@-3 {{lambda without a parameter clause is a C++2b extension}}
35     // expected-warning@-3 {{is a C++2b extension}}
36 #endif
37     [](int) -> {}; // PR13652 expected-error {{expected a type}}
38     return 1;
39   }
40 
41   void designator_or_lambda() {
42     typedef int T;
43     const int b = 0;
44     const int c = 1;
45     int d;
46     int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '(lambda}}
47     int a2[1] = {[b] = 1 };
48     int a3[1] = {[b,c] = 1 }; // expected-error{{expected ']'}} expected-note {{to match}}
49     int a4[1] = {[&b] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'const int *'}}
50     int a5[3] = { []{return 0;}() };
51     int a6[1] = {[this] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'C *'}}
52     int a7[1] = {[d(0)] { return d; } ()};
53     int a8[1] = {[d = 0] { return d; } ()};
54     int a10[1] = {[id(0)] { return id; } ()};
55 #if __cplusplus <= 201103L
56     // expected-warning@-4{{extension}}
57     // expected-warning@-4{{extension}}
58     // expected-warning@-4{{extension}}
59 #endif
60     int a9[1] = {[d = 0] = 1}; // expected-error{{is not an integral constant expression}}
61 #if __cplusplus >= 201402L
62     // expected-note@-2{{constant expression cannot modify an object that is visible outside that expression}}
63 #endif
64     int a11[1] = {[id(0)] = 1};
65   }
66 
67   void delete_lambda(int *p) {
68     delete [] p;
69     delete [] (int*) { new int }; // ok, compound-literal, not lambda
70     delete [] { return new int; } (); // expected-error {{'[]' after delete interpreted as 'delete[]'}}
71     delete [&] { return new int; } (); // ok, lambda
72 
73     delete []() { return new int; }(); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
74     delete [](E Enum) { return new int((int)Enum); }(e); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
75 #if __cplusplus > 201703L
76     delete []<int = 0>() { return new int; }(); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
77 #endif
78   }
79 
80   // We support init-captures in C++11 as an extension.
81   int z;
82   void init_capture() {
83     [n(0)] () mutable -> int { return ++n; };
84     [n{0}] { return; };
85     [a([&b = z]{})](){};
86     [n = 0] { return ++n; }; // expected-error {{captured by copy in a non-mutable}}
87     [n = {0}] { return; }; // expected-error {{<initializer_list>}}
88 #if __cplusplus <= 201103L
89     // expected-warning@-6{{extension}}
90     // expected-warning@-6{{extension}}
91     // expected-warning@-6{{extension}}
92     // expected-warning@-7{{extension}}
93     // expected-warning@-7{{extension}}
94     // expected-warning@-7{{extension}}
95 #endif
96 
97     int x = 4;
98     auto y = [&r = x, x = x + 1]() -> int {
99 #if __cplusplus <= 201103L
100       // expected-warning@-2{{extension}}
101       // expected-warning@-3{{extension}}
102 #endif
103       r += 2;
104       return x + 2;
105     } ();
106   }
107 
108   void attributes() {
109     [] __attribute__((noreturn)){};
110 #if __cplusplus <= 202002L
111     // expected-warning@-2 {{is a C++2b extension}}
112 #endif
113     []() [[]]
114       mutable {}; // expected-error {{expected body of lambda expression}}
115 
116     []() [[]] {};
117     []() [[]] -> void {};
118     []() mutable [[]] -> void {};
119     []() mutable noexcept [[]] -> void {};
120 
121     // Testing GNU-style attributes on lambdas -- the attribute is specified
122     // before the mutable specifier instead of after (unlike C++11).
123     []() __attribute__((noreturn)) mutable { while(1); };
124     []() mutable
125       __attribute__((noreturn)) { while(1); }; // expected-error {{expected body of lambda expression}}
126 
127     // Testing support for P2173 on adding attributes to the declaration
128     // rather than the type.
129     [][[]](){};
130 #if __cplusplus <= 202002L
131     // expected-warning@-2 {{an attribute specifier sequence in this position is a C++2b extension}}
132 #endif
133 #if __cplusplus > 201703L
134     []<typename>[[]](){};
135 #if __cplusplus <= 202002L
136     // expected-warning@-2 {{an attribute specifier sequence in this position is a C++2b extension}}
137 #endif
138 #endif
139     [][[]]{};
140 #if __cplusplus <= 202002L
141     // expected-warning@-2 {{an attribute specifier sequence in this position is a C++2b extension}}
142 #endif
143   }
144 
145   void missing_parens() {
146     [] mutable {};
147     [] noexcept {};
148 #if __cplusplus <= 202002L
149     // expected-warning@-3 {{is a C++2b extension}}
150     // expected-warning@-3 {{is a C++2b extension}}
151 #endif
152   }
153 };
154 
155 template <typename>
156 void PR22122() {
157   [](int) -> {}; // expected-error {{expected a type}}
158 }
159 
160 template void PR22122<int>();
161 
162 namespace PR42778 {
163 struct A {
164   template <class F> A(F&&) {}
165 };
166 
167 struct S {
168   void mf() { A{[*this]{}}; }
169 #if __cplusplus < 201703L
170   // expected-warning@-2 {{C++17 extension}}
171 #endif
172 };
173 }
174 
175 struct S {
176   template <typename T>
177   void m (T x =[0); // expected-error{{expected variable name or 'this' in lambda capture list}}
178 } s;
179 
180 struct U {
181   template <typename T>
182   void m_fn1(T x = 0[0); // expected-error{{expected ']'}} expected-note{{to match this '['}}
183 } *U;
184