Debian Codegraph @

clang/test/SemaCXX/cxx1z-constexpr-lambdas.cpp

raw
1 // RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks %s -fcxx-exceptions
2 // RUN: %clang_cc1 -std=c++20 -verify -fsyntax-only -fblocks %s -fcxx-exceptions
3 // RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks -fdelayed-template-parsing %s -fcxx-exceptions
4 // RUN: %clang_cc1 -std=c++14 -verify -fsyntax-only -fblocks %s -DCPP14_AND_EARLIER -fcxx-exceptions
5
6 // RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks %s -fcxx-exceptions -fexperimental-new-constant-interpreter
7 // RUN: %clang_cc1 -std=c++20 -verify -fsyntax-only -fblocks %s -fcxx-exceptions -fexperimental-new-constant-interpreter
8 // RUN: %clang_cc1 -std=c++1z -verify -fsyntax-only -fblocks -fdelayed-template-parsing %s -fcxx-exceptions -fexperimental-new-constant-interpreter
9 // RUN: %clang_cc1 -std=c++14 -verify -fsyntax-only -fblocks %s -DCPP14_AND_EARLIER -fcxx-exceptions -fexperimental-new-constant-interpreter
10
11 namespace test_lambda_is_literal {
12 #ifdef CPP14_AND_EARLIER
13 //expected-error@+4{{not a literal type}}
14 //expected-note@+2{{lambda closure types are non-literal types before C++17}}
15 #endif
16 auto L = [] { };
17 constexpr int foo(decltype(L) l) { return 0; }
18
19 }
20
21 #ifndef CPP14_AND_EARLIER
22 namespace test_constexpr_checking {
23
24 namespace ns1 {
25 struct NonLit { ~NonLit(); }; //expected-note{{not literal}}
26 auto L = [](NonLit NL) constexpr { }; //expected-error{{not a literal type}}
27 } // end ns1
28
29 namespace ns2 {
30 auto L = [](int I) constexpr { if (I == 5) asm("non-constexpr"); };
31 #if __cpp_constexpr < 201907L
32 //expected-warning@-2{{use of this statement in a constexpr function is a C++20 extension}}
33 #endif
34 } // end ns1
35
36 // This is not constexpr until C++20, as the requirements on constexpr
37 // functions don't permit try-catch blocks.
38 #if __cplusplus <= 201703L
39 // expected-error@#try-catch {{constant expression}}
40 // expected-note@#try-catch {{non-constexpr function 'operator()'}}
41 // expected-note@#try-catch {{declared here}}
42 #endif
43 constexpr int try_catch = [] { // #try-catch
44 try { return 0; } catch (...) { return 1; }
45 }();
46
47 // These lambdas have constexpr operator() even though they can never produce a
48 // constant expression.
49 auto never_constant_1 = [] { // expected-note {{here}}
50 volatile int n = 0;
51 return n;
52 };
53 auto never_constant_2 = [] () -> int { // expected-note {{here}}
54 };
55 struct test_never_constant {
56 #if __cplusplus >= 201703L
57 // expected-error@+3 {{non-constexpr declaration of 'operator()' follows constexpr declaration}}
58 // expected-error@+3 {{non-constexpr declaration of 'operator()' follows constexpr declaration}}
59 #endif
60 friend auto decltype(never_constant_1)::operator()() const;
61 friend int decltype(never_constant_2)::operator()() const;
62 };
63
64 } // end ns test_constexpr_checking
65
66 namespace test_constexpr_call {
67
68 namespace ns1 {
69 auto L = [](int I) { return I; };
70 static_assert(L(3) == 3);
71 } // end ns1
72 namespace ns2 {
73 auto L = [](auto a) { return a; };
74 static_assert(L(3) == 3);
75 static_assert(L(3.14) == 3.14);
76 }
77 namespace ns3 {
78 auto L = [](auto a) { asm("non-constexpr"); return a; };
79 constexpr int I = //expected-error{{must be initialized by a constant expression}}
80 L(3);
81 #if __cpp_constexpr < 201907L
82 //expected-note@-2{{non-constexpr function}}
83 //expected-note@-5{{declared here}}
84 #else
85 //expected-note@-7{{subexpression not valid in a constant expression}}
86 //expected-note@-6{{in call to}}
87 #endif
88 }
89
90 } // end ns test_constexpr_call
91
92 namespace test_captureless_lambda {
93 void f() {
94 const char c = 'c';
95 auto L = [] { return c; };
96 constexpr char C = L();
97 }
98
99 void f(char c) { //expected-note{{declared here}}
100 auto L = [] { return c; }; //expected-error{{cannot be implicitly captured}} expected-note{{lambda expression begins here}} expected-note 2 {{capture 'c' by}} expected-note 2 {{default capture by}}
101 int I = L();
102 }
103
104 }
105
106 namespace test_conversion_function_for_non_capturing_lambdas {
107
108 namespace ns1 {
109 auto L = [](int i) { return i; };
110 constexpr int (*fpi)(int) = L;
111 static_assert(fpi(3) == 3);
112 auto GL = [](auto a) { return a; };
113
114 constexpr char (*fp2)(char) = GL;
115 constexpr double (*fp3)(double) = GL;
116 constexpr const char* (*fp4)(const char*) = GL;
117 static_assert(fp2('3') == '3');
118 static_assert(fp3(3.14) == 3.14);
119 constexpr const char *Str = "abc";
120 static_assert(fp4(Str) == Str);
121
122 auto NCL = [](int i) { static int j; return j; }; //expected-note{{declared here}}
123 constexpr int (*fp5)(int) = NCL;
124 constexpr int I = //expected-error{{must be initialized by a constant expression}}
125 fp5(5); //expected-note{{non-constexpr function}}
126
127 namespace test_dont_always_instantiate_constexpr_templates {
128
129 auto explicit_return_type = [](auto x) -> int { return x.get(); };
130 decltype(explicit_return_type(0)) c; // OK
131
132 auto deduced_return_type = [](auto x) { return x.get(); }; //expected-error{{not a structure or union}}
133 decltype(deduced_return_type(0)) d; //expected-note{{requested here}}
134
135
136
137 } // end ns test_dont_always_instantiate_constexpr_templates
138 } // end ns1
139
140 } // end ns test_conversion_function_for_non_capturing_lambdas
141
142 namespace test_lambda_is_cce {
143 namespace ns1_simple_lambda {
144
145 namespace ns0 {
146 constexpr int I = [](auto a) { return a; }(10);
147
148 static_assert(I == 10);
149 static_assert(10 == [](auto a) { return a; }(10));
150 static_assert(3.14 == [](auto a) { return a; }(3.14));
151
152 } //end ns0
153
154 namespace ns1 {
155 constexpr auto f(int i) {
156 double d = 3.14;
157 auto L = [=](auto a) {
158 int Isz = sizeof(i);
159 return sizeof(i) + sizeof(a) + sizeof(d);
160 };
161 int I = L("abc") + L(nullptr);
162 return L;
163 }
164 constexpr auto L = f(3);
165 constexpr auto M = L("abc") + L(nullptr);
166
167 static_assert(M == sizeof(int) * 2 + sizeof(double) * 2 + sizeof(nullptr) + sizeof(const char*));
168
169 } // end ns1
170
171 namespace ns2 {
172 constexpr auto f(int i) {
173 auto L = [](auto a) { return a + a; };
174 return L;
175 }
176 constexpr auto L = f(3);
177 constexpr int I = L(6);
178 static_assert(I == 12);
179 } // end ns2
180
181 namespace contained_lambdas_call_operator_is_not_constexpr {
182 constexpr auto f(int i) {
183 double d = 3.14;
184 auto L = [=](auto a) {
185 int Isz = sizeof(i);
186 asm("hello");
187 return sizeof(i) + sizeof(a) + sizeof(d);
188 };
189 return L;
190 }
191
192 constexpr auto L = f(3);
193
194 constexpr auto M = // expected-error{{must be initialized by}}
195 L("abc");
196 #if __cpp_constexpr < 201907L
197 //expected-note@-2{{non-constexpr function}}
198 //expected-note@-14{{declared here}}
199 #else
200 //expected-note@-14{{subexpression not valid in a constant expression}}
201 //expected-note@-6{{in call to}}
202 #endif
203 } // end ns contained_lambdas_call_operator_is_not_constexpr
204
205
206
207 } // end ns1_simple_lambda
208
209 namespace test_captures_1 {
210 namespace ns1 {
211 constexpr auto f(int i) {
212 struct S { int x; } s = { i * 2 };
213 auto L = [=](auto a) {
214 return i + s.x + a;
215 };
216 return L;
217 }
218 constexpr auto M = f(3);
219
220 static_assert(M(10) == 19);
221
222 } // end test_captures_1::ns1
223
224 namespace ns2 {
225
226 constexpr auto foo(int n) {
227 auto L = [i = n] (auto N) mutable {
228 if (!N(i)) throw "error";
229 return [&i] {
230 return ++i;
231 };
232 };
233 auto M = L([n](int p) { return p == n; });
234 M(); M();
235 L([n](int p) { return p == n + 2; });
236
237 return L;
238 }
239
240 constexpr auto L = foo(3);
241
242 } // end test_captures_1::ns2
243 namespace ns3 {
244
245 constexpr auto foo(int n) {
246 auto L = [i = n] (auto N) mutable {
247 if (!N(i)) throw "error";
248 return [&i] {
249 return [i]() mutable {
250 return ++i;
251 };
252 };
253 };
254 auto M = L([n](int p) { return p == n; });
255 M()(); M()();
256 L([n](int p) { return p == n; });
257
258 return L;
259 }
260
261 constexpr auto L = foo(3);
262 } // end test_captures_1::ns3
263
264 namespace ns2_capture_this_byval {
265 struct S {
266 int s;
267 constexpr S(int s) : s{s} { }
268 constexpr auto f(S o) {
269 return [*this,o] (auto a) { return s + o.s + a.s; };
270 }
271 };
272
273 constexpr auto L = S{5}.f(S{10});
274 static_assert(L(S{100}) == 115);
275 } // end test_captures_1::ns2_capture_this_byval
276
277 namespace ns2_capture_this_byref {
278
279 struct S {
280 int s;
281 constexpr S(int s) : s{s} { }
282 constexpr auto f() const {
283 return [this] { return s; };
284 }
285 };
286
287 constexpr S SObj{5};
288 constexpr auto L = SObj.f();
289 constexpr int I = L();
290 static_assert(I == 5);
291
292 } // end ns2_capture_this_byref
293
294 } // end test_captures_1
295
296 namespace test_capture_array {
297 namespace ns1 {
298 constexpr auto f(int I) {
299 int arr[] = { I, I *2, I * 3 };
300 auto L1 = [&] (auto a) { return arr[a]; };
301 int r = L1(2);
302 struct X { int x, y; };
303 return [=](auto a) { return X{arr[a],r}; };
304 }
305 constexpr auto L = f(3);
306 static_assert(L(0).x == 3);
307 static_assert(L(0).y == 9);
308 static_assert(L(1).x == 6);
309 static_assert(L(1).y == 9);
310 } // end ns1
311
312 } // end test_capture_array
313 namespace ns1_test_lvalue_type {
314 void f() {
315 volatile int n;
316 constexpr bool B = [&]{ return &n; }() == &n; // should be accepted
317 }
318 } // end ns1_unimplemented
319
320 } // end ns test_lambda_is_cce
321
322 namespace PR36054 {
323 constexpr int fn() {
324 int Capture = 42;
325 return [=]() constexpr { return Capture; }();
326 }
327
328 static_assert(fn() == 42, "");
329
330 template <class T>
331 constexpr int tfn() {
332 int Capture = 42;
333 return [=]() constexpr { return Capture; }();
334 }
335
336 static_assert(tfn<int>() == 42, "");
337
338 constexpr int gfn() {
339 int Capture = 42;
340 return [=](auto P) constexpr { return Capture + P; }(58);
341 }
342
343 static_assert(gfn() == 100, "");
344
345 constexpr bool OtherCaptures() {
346 int Capture = 42;
347 constexpr auto Outer = [](auto P) constexpr { return 42 + P; };
348 auto Inner = [&](auto O) constexpr { return O(58) + Capture; };
349 return Inner(Outer) == 142;
350 }
351
352 static_assert(OtherCaptures(), "");
353 } // namespace PR36054
354
355 #endif // ndef CPP14_AND_EARLIER
356
357
358 #if __cpp_constexpr >= 201907L
359 namespace GH114234 {
360 template <auto Arg>
361 auto g() { return Arg; }
362
363 template <typename>
364 auto f() {
365 []<typename>() {
366 g<[] { return 123; }()>();
367 }.template operator()<int>();
368 }
369
370 void test() { f<int>(); }
371 }
372
373 namespace GH97958 {
374 static_assert(
375 []<int I=0>() -> decltype([]{ return true; })
376 { return {}; }()());
377 }
378
379 #endif
380
381 #ifndef CPP14_AND_EARLIER
382 namespace GH145956 {
383 constexpr int f() {
384 struct Pair { int first; int second; };
385 Pair p = {1, 2};
386 auto const& [key, value] = p;
387 return [&] { return key; }();
388 #if __cpp_constexpr < 202002L
389 // expected-warning@-2 {{captured structured bindings are a C++20 extension}}
390 // expected-note@-4 {{'key' declared here}}
391 #endif
392 }
393 static_assert(f() == 1);
394 constexpr auto retlambda() {
395 struct Pair { int first; int second; };
396 Pair p = {1, 2};
397 auto const& [key, value] = p;
398 return [=] { return key; };
399 #if __cpp_constexpr < 202002L
400 // expected-warning@-2 {{captured structured bindings are a C++20 extension}}
401 // expected-note@-4 {{'key' declared here}}
402 #endif
403 }
404 constexpr auto lambda = retlambda();
405 static_assert(lambda() == 1);
406 }
407 #endif