Debian Codegraph @

clang/test/AST/ByteCode/cxx23.cpp

raw
1 // UNSUPPORTED: target={{.*}}-zos{{.*}}
2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -fcxx-exceptions -Wno-deprecated-volatile -verify=ref,ref20,all,all20 %s
3 // RUN: %clang_cc1 -std=c++23 -fsyntax-only -fcxx-exceptions -Wno-deprecated-volatile -verify=ref,ref23,all,all23 %s
4 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -fcxx-exceptions -Wno-deprecated-volatile -verify=expected,expected20,all,all20 %s -fexperimental-new-constant-interpreter
5 // RUN: %clang_cc1 -std=c++23 -fsyntax-only -fcxx-exceptions -Wno-deprecated-volatile -verify=expected,expected23,all,all23 %s -fexperimental-new-constant-interpreter
6
7
8 #define assert_active(F) if (!__builtin_is_within_lifetime(&F)) (1/0);
9 #define assert_inactive(F) if ( __builtin_is_within_lifetime(&F)) (1/0);
10
11 inline constexpr void* operator new(__SIZE_TYPE__, void* p) noexcept { return p; }
12 namespace std {
13 template<typename T, typename... Args>
14 constexpr T* construct_at(T* p, Args&&... args) { return ::new((void*)p) T(static_cast<Args&&>(args)...); }
15 }
16
17 constexpr int f(int n) { // all20-error {{constexpr function never produces a constant expression}}
18 static const int m = n; // all-note {{control flows through the definition of a static variable}} \
19 // all20-note {{control flows through the definition of a static variable}} \
20 // all20-warning {{is a C++23 extension}}
21
22 return m;
23 }
24 static_assert(f(0) == 0, ""); // all-error {{not an integral constant expression}} \
25 // all-note {{in call to}}
26
27 constexpr int g(int n) { // all20-error {{constexpr function never produces a constant expression}}
28 thread_local const int m = n; // all-note {{control flows through the definition of a thread_local variable}} \
29 // all20-note {{control flows through the definition of a thread_local variable}} \
30 // all20-warning {{is a C++23 extension}}
31 return m;
32 }
33 static_assert(g(0) == 0, ""); // all-error {{not an integral constant expression}} \
34 // all-note {{in call to}}
35
36 constexpr int c_thread_local(int n) { // all20-error {{constexpr function never produces a constant expression}}
37 static _Thread_local int m = 0; // all20-note 2{{control flows through the definition of a thread_local variable}} \
38 // all23-note {{control flows through the definition of a thread_local variable}} \
39 // all20-warning {{is a C++23 extension}}
40 return m;
41 }
42 static_assert(c_thread_local(0) == 0, ""); // all-error {{not an integral constant expression}} \
43 // all-note {{in call to}}
44
45
46 constexpr int gnu_thread_local(int n) { // all20-error {{constexpr function never produces a constant expression}}
47 static __thread int m = 0; // all20-note 2{{control flows through the definition of a thread_local variable}} \
48 // all23-note {{control flows through the definition of a thread_local variable}} \
49 // all20-warning {{is a C++23 extension}}
50 return m;
51 }
52 static_assert(gnu_thread_local(0) == 0, ""); // all-error {{not an integral constant expression}} \
53 // all-note {{in call to}}
54
55 constexpr int h(int n) { // all20-error {{constexpr function never produces a constant expression}}
56 static const int m = n; // all20-note {{control flows through the definition of a static variable}} \
57 // all20-warning {{is a C++23 extension}}
58 return &m - &m;
59 }
60
61 constexpr int i(int n) { // all20-error {{constexpr function never produces a constant expression}}
62 thread_local const int m = n; // all20-note {{control flows through the definition of a thread_local variable}} \
63 // all20-warning {{is a C++23 extension}}
64 return &m - &m;
65 }
66
67 constexpr int j(int n) {
68 if (!n)
69 return 0;
70 static const int m = n; // ref20-warning {{is a C++23 extension}} \
71 // expected20-warning {{is a C++23 extension}}
72 return m;
73 }
74 constexpr int j0 = j(0);
75
76 constexpr int k(int n) {
77 if (!n)
78 return 0;
79 thread_local const int m = n; // ref20-warning {{is a C++23 extension}} \
80 // expected20-warning {{is a C++23 extension}}
81
82 return m;
83 }
84 constexpr int k0 = k(0);
85
86 namespace ThreadLocalStore {
87 thread_local int &&a = 0;
88 void store() { a = 42; }
89 }
90
91 #if __cplusplus >= 202302L
92 constexpr int &b = b; // all-error {{must be initialized by a constant expression}} \
93 // all-note {{initializer of 'b' is not a constant expression}} \
94 // all-note {{declared here}}
95 #endif
96
97 namespace StaticLambdas {
98 constexpr auto static_capture_constexpr() {
99 char n = 'n';
100 return [n] static { return n; }(); // expected23-error {{a static lambda cannot have any captures}} \
101 // expected20-error {{a static lambda cannot have any captures}} \
102 // expected20-warning {{are a C++23 extension}} \
103 // expected20-warning {{is a C++23 extension}} \
104 // ref23-error {{a static lambda cannot have any captures}} \
105 // ref20-error {{a static lambda cannot have any captures}} \
106 // ref20-warning {{are a C++23 extension}} \
107 // ref20-warning {{is a C++23 extension}}
108 }
109 static_assert(static_capture_constexpr()); // expected23-error {{static assertion expression is not an integral constant expression}} \
110 // expected20-error {{static assertion expression is not an integral constant expression}} \
111 // ref23-error {{static assertion expression is not an integral constant expression}} \
112 // ref20-error {{static assertion expression is not an integral constant expression}}
113
114 constexpr auto capture_constexpr() {
115 char n = 'n';
116 return [n] { return n; }();
117 }
118 static_assert(capture_constexpr());
119 }
120
121 namespace StaticOperators {
122 auto lstatic = []() static { return 3; }; // ref20-warning {{C++23 extension}} \
123 // expected20-warning {{C++23 extension}}
124 static_assert(lstatic() == 3, "");
125 constexpr int (*f2)(void) = lstatic;
126 static_assert(f2() == 3);
127
128 struct S1 {
129 constexpr S1() { // all20-error {{never produces a constant expression}}
130 throw; // all-note {{not valid in a constant expression}} \
131 // all20-note {{not valid in a constant expression}}
132 }
133 static constexpr int operator()() { return 3; } // ref20-warning {{C++23 extension}} \
134 // expected20-warning {{C++23 extension}}
135 };
136 static_assert(S1{}() == 3, ""); // all-error {{not an integral constant expression}} \
137 // all-note {{in call to}}
138
139
140
141 }
142
143 int test_in_lambdas() {
144 auto c = [](int n) constexpr {
145 if (n == 0)
146 return 0;
147 else
148 goto test; // all-note {{subexpression not valid in a constant expression}} \
149 // all20-warning {{use of this statement in a constexpr function is a C++23 extension}}
150 test:
151 return 1;
152 };
153 c(0);
154 constexpr auto A = c(1); // all-error {{must be initialized by a constant expression}} \
155 // all-note {{in call to}}
156 return 0;
157 }
158
159 /// PackIndexExpr.
160 template <auto... p>
161 struct check_ice {
162 enum e {
163 x = p...[0] // all-warning {{is a C++2c extension}}
164 };
165 };
166 static_assert(check_ice<42>::x == 42);
167
168
169 namespace VirtualBases {
170 namespace One {
171 struct U { int n; };
172 struct V : U { int n; };
173 struct A : virtual V { int n; };
174 struct Aa { int n; };
175 struct B : virtual A, Aa {};
176 struct C : virtual A, Aa {};
177 struct D : B, C {};
178
179 /// Calls the constructor of D.
180 D d;
181 }
182
183 #if __cplusplus >= 202302L
184 struct VBase {};
185 struct HasVBase : virtual VBase {}; // all23-note 1{{virtual base class declared here}}
186 struct Derived : HasVBase {
187 constexpr Derived() {} // all23-error {{constexpr constructor not allowed in struct with virtual base class}}
188 };
189 template<typename T> struct DerivedFromVBase : T {
190 constexpr DerivedFromVBase();
191 };
192 constexpr int f(DerivedFromVBase<HasVBase>) {}
193 template<typename T> constexpr DerivedFromVBase<T>::DerivedFromVBase() : T() {}
194 constexpr int nVBase = (DerivedFromVBase<HasVBase>(), 0); // all23-error {{constant expression}} \
195 // all23-note {{cannot construct object of type 'DerivedFromVBase<VirtualBases::HasVBase>' with virtual base class in a constant expression}}
196 #endif
197 }
198
199 namespace LabelGoto {
200 constexpr int foo() { // all20-error {{never produces a constant expression}}
201 a: // all20-warning {{use of this statement in a constexpr function is a C++23 extension}}
202 goto a; // all20-note 2{{subexpression not valid in a constant expression}} \
203 // ref23-note {{subexpression not valid in a constant expression}} \
204 // expected23-note {{subexpression not valid in a constant expression}}
205
206 return 1;
207 }
208 static_assert(foo() == 1, ""); // all-error {{not an integral constant expression}} \
209 // all-note {{in call to}}
210 }
211
212 namespace ExplicitLambdaThis {
213 constexpr auto f = [x = 3]<typename Self>(this Self self) { // all20-error {{explicit object parameters are incompatible with C++ standards before C++2b}}
214 return x;
215 };
216 static_assert(f());
217 }
218
219 namespace std {
220 struct strong_ordering {
221 int n;
222 constexpr operator int() const { return n; }
223 static const strong_ordering less, equal, greater;
224 };
225 constexpr strong_ordering strong_ordering::less = {-1};
226 constexpr strong_ordering strong_ordering::equal = {0};
227 constexpr strong_ordering strong_ordering::greater = {1};
228 }
229
230 namespace UndefinedThreeWay {
231 struct A {
232 friend constexpr std::strong_ordering operator<=>(const A&, const A&) = default; // all-note {{declared here}}
233 };
234
235 constexpr std::strong_ordering operator<=>(const A&, const A&) noexcept;
236 constexpr std::strong_ordering (*test_a_threeway)(const A&, const A&) = &operator<=>;
237 static_assert(!(*test_a_threeway)(A(), A())); // all-error {{static assertion expression is not an integral constant expression}} \
238 // all-note {{undefined function 'operator<=>' cannot be used in a constant expression}}
239 }
240
241 /// FIXME: The new interpreter is missing the "initializer of q is not a constant expression" diagnostics.a
242 /// That's because the cast from void* to int* is considered fine, but diagnosed. So we don't consider
243 /// q to be uninitialized.
244 namespace VoidCast {
245 constexpr void* p = nullptr;
246 constexpr int* q = static_cast<int*>(p); // all-error {{must be initialized by a constant expression}} \
247 // all-note {{cast from 'void *' is not allowed in a constant expression}} \
248 // ref-note {{declared here}}
249 static_assert(q == nullptr); // ref-error {{not an integral constant expression}} \
250 // ref-note {{initializer of 'q' is not a constant expression}}
251 }
252
253 namespace ExplicitLambdaInstancePointer {
254 struct C {
255 constexpr C(auto) { }
256 };
257 void foo() {
258 constexpr auto b = [](this C) { return 1; }; // all20-error {{explicit object parameters are incompatible with C++ standards before C++2b}}
259 constexpr int (*fp)(C) = b;
260 static_assert(fp(1) == 1, "");
261 }
262
263 /// Failure case. The instance pointer is of type int.
264 struct S2 {
265 constexpr K(auto) { } // all-error {{a type specifier is required for all declarations}}
266 };
267 constexpr auto b = [](this K) { return 1; }; // all20-error {{explicit object parameters are incompatible with C++ standards before C++2b}} \
268 // all-error {{unknown type name 'K'}}
269 constexpr int (*fp)(K) = b; // all-error {{unknown type name 'K'}}
270 static_assert(fp(1) == 1, ""); // expected-error {{not an integral constant expression}}
271 }
272
273 namespace TwosComplementShifts {
274 using uint32 = __UINT32_TYPE__;
275 using int32 = __INT32_TYPE__;
276 static_assert(uint32(int32(0x1234) << 16) == 0x12340000);
277 static_assert(uint32(int32(0x1234) << 19) == 0x91a00000);
278 static_assert(uint32(int32(0x1234) << 20) == 0x23400000);
279 static_assert(uint32(int32(0x1234) << 24) == 0x34000000);
280 static_assert(uint32(int32(-1) << 31) == 0x80000000);
281
282 static_assert(-2 >> 1 == -1);
283 static_assert(-3 >> 1 == -2);
284 static_assert(-7 >> 1 == -4);
285 }
286
287 namespace AnonUnionDtor {
288 struct A {
289 A ();
290 ~A();
291 };
292
293 template <class T>
294 struct opt
295 {
296 union {
297 char c;
298 T data;
299 };
300
301 constexpr opt() {}
302
303 constexpr ~opt() {
304 if (engaged)
305 data.~T();
306 }
307
308 bool engaged = false;
309 };
310
311 consteval void foo() {
312 opt<A> a;
313 }
314
315 void bar() { foo(); }
316 }
317
318 /// FIXME: The two interpreters disagree about there to diagnose the non-constexpr destructor call.
319 namespace NonLiteralDtorInParam {
320 class NonLiteral { // all20-note {{is not an aggregate and has no constexpr constructors other than copy or move constructors}}
321 public:
322 NonLiteral() {}
323 ~NonLiteral() {} // all23-note {{declared here}}
324 };
325 constexpr int F2(NonLiteral N) { // all20-error {{constexpr function's 1st parameter type 'NonLiteral' is not a literal type}} \
326 // all23-note {{non-constexpr function '~NonLiteral' cannot be used in a constant expression}}
327 return 8;
328 }
329
330
331 void test() {
332 NonLiteral L;
333 constexpr auto D = F2(L); // all23-error {{must be initialized by a constant expression}}
334 }
335 }
336
337 namespace ZeroSizedArray {
338 struct S {
339 constexpr ~S() {
340 }
341 };
342 constexpr int foo() {
343 S s[0];
344 return 1;
345 }
346 static_assert(foo() == 1);
347 }
348 namespace VoidCast {
349 constexpr int a = 12;
350 constexpr const int *b = &a;
351 constexpr int *f = (int*)(void*)b; // all-error {{must be initialized by a constant expression}} \
352 // all-note {{cast from 'void *' is not allowed in a constant expression}}
353 }
354
355 #if __cplusplus >= 202302L
356 namespace NestedUnions {
357 consteval bool test_nested() {
358 union {
359 union { int i; char c; } u;
360 long l;
361 };
362 std::construct_at(&l);
363 assert_active(l);
364 assert_inactive(u);
365
366 std::construct_at(&u);
367 assert_active(u);
368 assert_inactive(l);
369 assert_active(u.i);
370 assert_inactive(u.c);
371
372 std::construct_at(&u.i);
373 assert_active(u);
374 assert_inactive(u.c);
375
376
377 std::construct_at(&u.c);
378 assert_active(u);
379 assert_inactive(u.i);
380 assert_active(u.c);
381 assert_inactive(l);
382 return true;
383 }
384 static_assert(test_nested());
385 }
386
387 namespace UnionMemberCallDiags {
388 struct A { int n; };
389 struct B { A a; };
390 constexpr A a = (A() = B().a);
391
392 union C {
393 int n;
394 A a;
395 };
396
397 constexpr bool g() {
398 C c = {.n = 1};
399 c.a.operator=(B{2}.a); // all-note {{member call on member 'a' of union with active member 'n' is not allowed in a constant expression}}
400 return c.a.n == 2;
401 }
402 static_assert(g()); // all-error {{not an integral constant expression}} \
403 // all-note {{in call to}}
404 }
405 #endif
406
407 namespace Volatile {
408 constexpr void test1() {// all20-error {{never produces a constant expression}}
409 int k;
410 volatile int &m = k;
411 m = 10; // all20-note {{assignment to volatile-qualified type 'volatile int'}}
412 }
413
414 constexpr void test2() { // all20-error {{never produces a constant expression}}
415 volatile int k = 12;
416
417 k = 13; // all20-note {{assignment to volatile-qualified type 'volatile int'}}
418 }
419
420 constexpr void test3() { // all20-error {{never produces a constant expression}}
421 volatile int k = 12; // all20-note {{volatile object declared here}}
422
423 *((int *)&k) = 13; // all20-note {{assignment to volatile object 'k' is not allowed in a constant expression}}
424 }
425
426 constexpr void test4() { // all20-error {{never produces a constant expression}}
427 int k = 12;
428
429 *((volatile int *)&k) = 13; // all20-note {{assignment to volatile-qualified type 'volatile int' is not allowed in a constant expression}}
430 }
431
432 #if __cplusplus >= 202302L
433 struct S {
434 volatile int k;
435 };
436 constexpr int test5() {
437 S s;
438 s.k = 12; // all-note {{assignment to volatile-qualified type 'volatile int' is not}}
439
440 return 0;
441 }
442 static_assert(test5() == 0); // all-error{{not an integral constant expression}} \
443 // all-note {{in call to}}
444 #endif
445
446 constexpr bool test6(volatile int k) { // ref20-error {{never produces a constant expression}}
447 k = 14; // ref20-note {{assignment to volatile-qualified type 'volatile int' is not}} \
448 // all-note {{assignment to volatile-qualified type 'volatile int' is not}}
449 return true;
450 }
451 static_assert(test6(5)); // all-error {{not an integral constant expression}} \
452 // all-note {{in call to}}
453
454 constexpr bool test7(volatile int k) { // all-note {{declared here}}
455 *((int *)&k) = 13; // all-note {{assignment to volatile object 'k' is not allowed in a constant expression}}
456 return true;
457 }
458 static_assert(test7(12)); // all-error {{not an integral constant expression}} \
459 // all-note {{in call to}}
460
461 constexpr int test8(bool b) {
462 if (!b)
463 return -1;
464 struct C {
465 int a;
466 };
467 volatile C c{12}; // all-note {{volatile object declared here}}
468 return ((C&)(c)).a; // all-note {{read of volatile object}}
469 }
470 static_assert(test8(true) == 0); // all-error {{not an integral constant expression}} \
471 // all-note {{in call to}}
472
473 struct C {
474 int n;
475 constexpr C() : n(1) { n = 2; int b= n;}
476 };
477 constexpr int f(bool get) {
478 if (get)
479 return -1;
480 volatile C c;
481 return 2;
482 }
483 static_assert(f(false) == 2, "");
484 }
485
486 namespace AIEWithIndex0Narrows {
487 template <class _Tp> struct greater {
488 constexpr void operator()(_Tp, _Tp) {}
489 };
490 struct S {
491 constexpr S() : i_() {}
492 int i_;
493 };
494
495 constexpr void sort(S *__first) {
496 for (int __start = 0; __start >= 0; --__start) {
497 greater<S>{}(__first[0], __first[0]);
498 }
499 }
500 constexpr bool test() {
501 S *ia = new S[2];
502
503 sort(ia + 1);
504 delete[] ia;
505 return true;
506 }
507 static_assert(test());
508 }
509
510 #if __cplusplus >= 202302L
511 namespace InactiveLocalsInConditionalOp {
512 struct A { constexpr A(){}; ~A(); constexpr int get() { return 10; } }; // all-note 2{{declared here}}
513 constexpr int get(bool b) {
514 return b ? A().get() : 1; // all-note {{non-constexpr function '~A' cannot be used in a constant expression}}
515 }
516 static_assert(get(false) == 1, "");
517 static_assert(get(true) == 10, ""); // all-error {{not an integral constant expression}} \
518 // all-note {{in call to}}
519
520 static_assert( (false ? A().get() : 1) == 1);
521 static_assert( (true ? A().get() : 1) == 1); // all-error {{not an integral constant expression}} \
522 // all-note {{non-constexpr function '~A' cannot be used in a constant expression}}
523
524 constexpr bool test2(bool b) {
525 unsigned long __ms = b ? (const unsigned long &)0 : __ms;
526 return true;
527 }
528 static_assert(test2(true));
529
530 }
531 #endif
532
533 namespace UnknownParams {
534 class X {
535 public:
536 constexpr operator bool(){ return true; }
537 };
538 int foo(X x) {
539 static_assert(x);
540 return 1;
541 }
542
543 int foo2(X &x) { // all20-note {{declared here}}
544 static_assert(x); // all20-error {{not an integral constant expression}} \
545 // all20-note {{function parameter 'x' with unknown value cannot be used in a constant expression}}
546 return 1;
547 }
548 }