Debian Codegraph @

clang/test/AST/Interp/cxx20.cpp

raw
1 // RUN: %clang_cc1 -fcxx-exceptions -fexperimental-new-constant-interpreter -std=c++20 -verify=both,expected -fcxx-exceptions %s
2 // RUN: %clang_cc1 -fcxx-exceptions -std=c++20 -verify=both,ref -fcxx-exceptions %s
3
4 void test_alignas_operand() {
5 alignas(8) char dummy;
6 static_assert(__alignof(dummy) == 8);
7 }
8
9 constexpr int getMinus5() {
10 int a = 10;
11 a = -5;
12 int *p = &a;
13 return *p;
14 }
15 static_assert(getMinus5() == -5, "");
16
17 constexpr int assign() {
18 int m = 10;
19 int k = 12;
20
21 m = (k = 20);
22
23 return m;
24 }
25 static_assert(assign() == 20, "");
26
27
28 constexpr int pointerAssign() {
29 int m = 10;
30 int *p = &m;
31
32 *p = 12; // modifies m
33
34 return m;
35 }
36 static_assert(pointerAssign() == 12, "");
37
38 constexpr int pointerDeref() {
39 int m = 12;
40 int *p = &m;
41
42 return *p;
43 }
44 static_assert(pointerDeref() == 12, "");
45
46 constexpr int pointerAssign2() {
47 int m = 10;
48 int *p = &m;
49 int **pp = &p;
50
51 **pp = 12;
52
53 int v = **pp;
54
55 return v;
56 }
57 static_assert(pointerAssign2() == 12, "");
58
59 constexpr int unInitLocal() {
60 int a;
61 return a; // both-note {{read of uninitialized object}}
62 }
63 static_assert(unInitLocal() == 0, ""); // both-error {{not an integral constant expression}} \
64 // both-note {{in call to 'unInitLocal()'}}
65
66 constexpr int initializedLocal() {
67 int a;
68 a = 20;
69 return a;
70 }
71 static_assert(initializedLocal() == 20);
72
73 constexpr int initializedLocal2() {
74 int a[2];
75 return *a; // both-note {{read of uninitialized object is not allowed in a constant expression}}
76 }
77 static_assert(initializedLocal2() == 20); // both-error {{not an integral constant expression}} \
78 // both-note {{in call to}}
79
80
81 struct Int { int a; };
82 constexpr int initializedLocal3() {
83 Int i;
84 return i.a; // both-note {{read of uninitialized object is not allowed in a constant expression}}
85 }
86 static_assert(initializedLocal3() == 20); // both-error {{not an integral constant expression}} \
87 // both-note {{in call to}}
88
89
90
91 #if 0
92 // FIXME: This code should be rejected because we pass an uninitialized value
93 // as a function parameter.
94 constexpr int inc(int a) { return a + 1; }
95 constexpr int f() {
96 int i;
97 return inc(i);
98 }
99 static_assert(f());
100 #endif
101
102 /// Distinct literals have disctinct addresses.
103 /// see https://github.com/llvm/llvm-project/issues/58754
104 constexpr auto foo(const char *p) { return p; }
105 constexpr auto p1 = "test1";
106 constexpr auto p2 = "test2";
107
108 constexpr bool b1 = foo(p1) == foo(p1);
109 static_assert(b1);
110
111 constexpr bool b2 = foo(p1) == foo(p2); // ref-error {{must be initialized by a constant expression}} \
112 // ref-note {{comparison of addresses of literals}} \
113 // ref-note {{declared here}}
114 static_assert(!b2); // ref-error {{not an integral constant expression}} \
115 // ref-note {{not a constant expression}}
116
117 constexpr auto name1() { return "name1"; }
118 constexpr auto name2() { return "name2"; }
119
120 constexpr auto b3 = name1() == name1();
121 static_assert(b3);
122 constexpr auto b4 = name1() == name2(); // ref-error {{must be initialized by a constant expression}} \
123 // ref-note {{has unspecified value}} \
124 // ref-note {{declared here}}
125 static_assert(!b4); // ref-error {{not an integral constant expression}} \
126 // ref-note {{not a constant expression}}
127
128 namespace UninitializedFields {
129 class A {
130 public:
131 int a; // both-note 4{{subobject declared here}}
132 constexpr A() {}
133 };
134 constexpr A a; // both-error {{must be initialized by a constant expression}} \
135 // both-note {{subobject 'a' is not initialized}}
136 constexpr A aarr[2]; // both-error {{must be initialized by a constant expression}} \
137 // both-note {{subobject 'a' is not initialized}}
138 class F {
139 public:
140 int f; // both-note 3{{subobject declared here}}
141
142 constexpr F() {}
143 constexpr F(bool b) {
144 if (b)
145 f = 42;
146 }
147 };
148
149 constexpr F foo[2] = {true}; // both-error {{must be initialized by a constant expression}} \
150 // both-note {{subobject 'f' is not initialized}}
151 constexpr F foo2[3] = {true, false, true}; // both-error {{must be initialized by a constant expression}} \
152 // both-note {{subobject 'f' is not initialized}}
153 constexpr F foo3[3] = {true, true, F()}; // both-error {{must be initialized by a constant expression}} \
154 // both-note {{subobject 'f' is not initialized}}
155
156
157
158 class Base {
159 public:
160 bool b;
161 int a; // both-note {{subobject declared here}}
162 constexpr Base() : b(true) {}
163 };
164
165 class Derived : public Base {
166 public:
167 constexpr Derived() : Base() {} };
168
169 constexpr Derived D; // both-error {{must be initialized by a constant expression}} \
170 // both-note {{subobject 'a' is not initialized}}
171
172 class C2 {
173 public:
174 A a;
175 constexpr C2() {} };
176 constexpr C2 c2; // both-error {{must be initialized by a constant expression}} \
177 // both-note {{subobject 'a' is not initialized}}
178
179 class C3 {
180 public:
181 A a[2];
182 constexpr C3() {}
183 };
184 constexpr C3 c3; // both-error {{must be initialized by a constant expression}} \
185 // both-note {{subobject 'a' is not initialized}}
186
187 class C4 {
188 public:
189 bool B[2][3]; // both-note {{subobject declared here}}
190 constexpr C4(){}
191 };
192 constexpr C4 c4; // both-error {{must be initialized by a constant expression}} \
193 // both-note {{subobject 'B' is not initialized}}
194 };
195
196 namespace ConstThis {
197 class Foo {
198 const int T = 12; // both-note {{declared const here}}
199 int a;
200 public:
201 constexpr Foo() {
202 this->a = 10;
203 T = 13; // both-error {{cannot assign to non-static data member 'T' with const-qualified type}}
204 }
205 };
206 constexpr Foo F; // both-error {{must be initialized by a constant expression}}
207
208
209 class FooDtor {
210 int a;
211 public:
212 constexpr FooDtor() {
213 this->a = 10;
214 }
215 constexpr ~FooDtor() {
216 this->a = 12;
217 }
218 };
219
220 constexpr int foo() {
221 const FooDtor f;
222 return 0;
223 }
224 static_assert(foo() == 0);
225
226 template <bool Good>
227 struct ctor_test {
228 int a = 0;
229
230 constexpr ctor_test() {
231 if (Good)
232 a = 10;
233 int local = 100 / a; // both-note {{division by zero}}
234 }
235 };
236
237 template <bool Good>
238 struct dtor_test {
239 int a = 0;
240
241 constexpr dtor_test() = default;
242 constexpr ~dtor_test() {
243 if (Good)
244 a = 10;
245 int local = 100 / a; // both-note {{division by zero}}
246 }
247 };
248
249 constexpr ctor_test<true> good_ctor;
250 constexpr dtor_test<true> good_dtor;
251
252 constexpr ctor_test<false> bad_ctor; // both-error {{must be initialized by a constant expression}} \
253 // both-note {{in call to}}
254 constexpr dtor_test<false> bad_dtor; // both-error {{must have constant destruction}} \
255 // both-note {{in call to}}
256 };
257
258 namespace BaseInit {
259 struct Base {
260 int a;
261 };
262
263 struct Intermediate : Base {
264 int b;
265 };
266
267 struct Final : Intermediate {
268 int c;
269
270 constexpr Final(int a, int b, int c) : c(c) {}
271 };
272
273 static_assert(Final{1, 2, 3}.c == 3, ""); // OK
274 static_assert(Final{1, 2, 3}.a == 0, ""); // both-error {{not an integral constant expression}} \
275 // both-note {{read of uninitialized object}}
276
277
278 struct Mixin {
279 int b;
280
281 constexpr Mixin() = default;
282 constexpr Mixin(int b) : b(b) {}
283 };
284
285 struct Final2 : Base, Mixin {
286 int c;
287
288 constexpr Final2(int a, int b, int c) : Mixin(b), c(c) {}
289 constexpr Final2(int a, int b, int c, bool) : c(c) {}
290 };
291
292 static_assert(Final2{1, 2, 3}.c == 3, ""); // OK
293 static_assert(Final2{1, 2, 3}.b == 2, ""); // OK
294 static_assert(Final2{1, 2, 3}.a == 0, ""); // both-error {{not an integral constant expression}} \
295 // both-note {{read of uninitialized object}}
296
297
298 struct Mixin3 {
299 int b;
300 };
301
302 struct Final3 : Base, Mixin3 {
303 int c;
304
305 constexpr Final3(int a, int b, int c) : c(c) { this->b = b; }
306 constexpr Final3(int a, int b, int c, bool) : c(c) {}
307 };
308
309 static_assert(Final3{1, 2, 3}.c == 3, ""); // OK
310 static_assert(Final3{1, 2, 3}.b == 2, ""); // OK
311 static_assert(Final3{1, 2, 3}.a == 0, ""); // both-error {{not an integral constant expression}} \
312 // both-note {{read of uninitialized object}}
313 };
314
315 namespace Destructors {
316
317 class Inc final {
318 public:
319 int &I;
320 constexpr Inc(int &I) : I(I) {}
321 constexpr ~Inc() {
322 I++;
323 }
324 };
325
326 class Dec final {
327 public:
328 int &I;
329 constexpr Dec(int &I) : I(I) {}
330 constexpr ~Dec() {
331 I--;
332 }
333 };
334
335
336
337 constexpr int m() {
338 int i = 0;
339 {
340 Inc f1(i);
341 Inc f2(i);
342 Inc f3(i);
343 }
344 return i;
345 }
346 static_assert(m() == 3, "");
347
348
349 constexpr int C() {
350 int i = 0;
351
352 while (i < 10) {
353 Inc inc(i);
354 continue;
355 Dec dec(i);
356 }
357 return i;
358 }
359 static_assert(C() == 10, "");
360
361
362 constexpr int D() {
363 int i = 0;
364
365 {
366 Inc i1(i);
367 {
368 Inc i2(i);
369 return i;
370 }
371 }
372
373 return i;
374 }
375 static_assert(D() == 0, "");
376
377 constexpr int E() {
378 int i = 0;
379
380 for(;;) {
381 Inc i1(i);
382 break;
383 }
384 return i;
385 }
386 static_assert(E() == 1, "");
387
388
389 /// FIXME: This should be rejected, since we call the destructor
390 /// twice. However, GCC doesn't care either.
391 constexpr int ManualDtor() {
392 int i = 0;
393 {
394 Inc I(i); // ref-note {{destroying object 'I' whose lifetime has already ended}}
395 I.~Inc();
396 }
397 return i;
398 }
399 static_assert(ManualDtor() == 1, ""); // expected-error {{static assertion failed}} \
400 // expected-note {{evaluates to '2 == 1'}} \
401 // ref-error {{not an integral constant expression}} \
402 // ref-note {{in call to 'ManualDtor()'}}
403
404 constexpr void doInc(int &i) {
405 Inc I(i);
406 return;
407 }
408 constexpr int testInc() {
409 int i = 0;
410 doInc(i);
411 return i;
412 }
413 static_assert(testInc() == 1, "");
414 constexpr void doInc2(int &i) {
415 Inc I(i);
416 // No return statement.
417 }
418 constexpr int testInc2() {
419 int i = 0;
420 doInc2(i);
421 return i;
422 }
423 static_assert(testInc2() == 1, "");
424
425
426 namespace DtorOrder {
427 class A {
428 public:
429 int &I;
430 constexpr A(int &I) : I(I) {}
431 constexpr ~A() {
432 I = 1337;
433 }
434 };
435
436 class B : public A {
437 public:
438 constexpr B(int &I) : A(I) {}
439 constexpr ~B() {
440 I = 42;
441 }
442 };
443
444 constexpr int foo() {
445 int i = 0;
446 {
447 B b(i);
448 }
449 return i;
450 }
451
452 static_assert(foo() == 1337);
453 }
454
455 class FieldDtor1 {
456 public:
457 Inc I1;
458 Inc I2;
459 constexpr FieldDtor1(int &I) : I1(I), I2(I){}
460 };
461
462 constexpr int foo2() {
463 int i = 0;
464 {
465 FieldDtor1 FD1(i);
466 }
467 return i;
468 }
469
470 static_assert(foo2() == 2);
471
472 class FieldDtor2 {
473 public:
474 Inc Incs[3];
475 constexpr FieldDtor2(int &I) : Incs{Inc(I), Inc(I), Inc(I)} {}
476 };
477
478 constexpr int foo3() {
479 int i = 0;
480 {
481 FieldDtor2 FD2(i);
482 }
483 return i;
484 }
485
486 static_assert(foo3() == 3);
487
488 struct ArrD {
489 int index;
490 int *arr;
491 int &p;
492 constexpr ~ArrD() {
493 arr[p] = index;
494 ++p;
495 }
496 };
497 constexpr bool ArrayOrder() {
498 int order[3] = {0, 0, 0};
499 int p = 0;
500 {
501 ArrD ds[3] = {
502 {1, order, p},
503 {2, order, p},
504 {3, order, p},
505 };
506 // ds will be destroyed.
507 }
508 return order[0] == 3 && order[1] == 2 && order[2] == 1;
509 }
510 static_assert(ArrayOrder());
511
512
513 // Static members aren't destroyed.
514 class Dec2 {
515 public:
516 int A = 0;
517 constexpr ~Dec2() {
518 A++;
519 }
520 };
521 class Foo {
522 public:
523 static constexpr Dec2 a;
524 static Dec2 b;
525 };
526 static_assert(Foo::a.A == 0);
527 constexpr bool f() {
528 Foo f;
529 return true;
530 }
531 static_assert(Foo::a.A == 0);
532 static_assert(f());
533 static_assert(Foo::a.A == 0);
534
535
536 struct NotConstexpr {
537 NotConstexpr() {}
538 ~NotConstexpr() {}
539 };
540
541 struct Outer {
542 constexpr Outer() = default;
543 constexpr ~Outer();
544
545 constexpr int foo() {
546 return 12;
547 }
548
549 constexpr int bar()const {
550 return Outer{}.foo();
551 }
552
553 static NotConstexpr Val;
554 };
555
556 constexpr Outer::~Outer() {}
557
558 constexpr Outer O;
559 static_assert(O.bar() == 12);
560 }
561
562 namespace BaseAndFieldInit {
563 struct A {
564 int a;
565 };
566
567 struct B : A {
568 int b;
569 };
570
571 struct C : B {
572 int c;
573 };
574
575 constexpr C c = {1,2,3};
576 static_assert(c.a == 1 && c.b == 2 && c.c == 3);
577 }
578
579 namespace ImplicitFunction {
580 struct A {
581 int a; // ref-note {{subobject declared here}}
582 };
583
584 constexpr int callMe() {
585 A a;
586 A b{12};
587
588 /// The operator= call here will fail and the diagnostics should be fine.
589 b = a; // ref-note {{subobject 'a' is not initialized}} \
590 // expected-note {{read of uninitialized object}} \
591 // both-note {{in call to}}
592
593 return 1;
594 }
595 static_assert(callMe() == 1, ""); // both-error {{not an integral constant expression}} \
596 // both-note {{in call to 'callMe()'}}
597 }
598
599 /// FIXME: Unfortunately, the similar tests in test/SemaCXX/{compare-cxx2a.cpp use member pointers,
600 /// which we don't support yet.
601 namespace std {
602 class strong_ordering {
603 public:
604 int n;
605 static const strong_ordering less, equal, greater;
606 constexpr bool operator==(int n) const noexcept { return this->n == n;}
607 constexpr bool operator!=(int n) const noexcept { return this->n != n;}
608 };
609 constexpr strong_ordering strong_ordering::less = {-1};
610 constexpr strong_ordering strong_ordering::equal = {0};
611 constexpr strong_ordering strong_ordering::greater = {1};
612
613 class partial_ordering {
614 public:
615 long n;
616 static const partial_ordering less, equal, greater, equivalent, unordered;
617 constexpr bool operator==(long n) const noexcept { return this->n == n;}
618 constexpr bool operator!=(long n) const noexcept { return this->n != n;}
619 };
620 constexpr partial_ordering partial_ordering::less = {-1};
621 constexpr partial_ordering partial_ordering::equal = {0};
622 constexpr partial_ordering partial_ordering::greater = {1};
623 constexpr partial_ordering partial_ordering::equivalent = {0};
624 constexpr partial_ordering partial_ordering::unordered = {-127};
625 } // namespace std
626
627 namespace ThreeWayCmp {
628 static_assert(1 <=> 2 == -1, "");
629 static_assert(1 <=> 1 == 0, "");
630 static_assert(2 <=> 1 == 1, "");
631 static_assert(1.0 <=> 2.f == -1, "");
632 static_assert(1.0 <=> 1.0 == 0, "");
633 static_assert(2.0 <=> 1.0 == 1, "");
634 constexpr int k = (1 <=> 1, 0); // both-warning {{comparison result unused}}
635 static_assert(k== 0, "");
636
637 /// Pointers.
638 constexpr int a[] = {1,2,3};
639 constexpr int b[] = {1,2,3};
640 constexpr const int *pa1 = &a[1];
641 constexpr const int *pa2 = &a[2];
642 constexpr const int *pb1 = &b[1];
643 static_assert(pa1 <=> pb1 != 0, ""); // both-error {{not an integral constant expression}} \
644 // both-note {{has unspecified value}} \
645 static_assert(pa1 <=> pa1 == 0, "");
646 static_assert(pa1 <=> pa2 == -1, "");
647 static_assert(pa2 <=> pa1 == 1, "");
648 }
649
650 namespace ConstexprArrayInitLoopExprDestructors
651 {
652 struct Highlander {
653 int *p = 0;
654 constexpr Highlander() {}
655 constexpr void set(int *p) { this->p = p; ++*p; if (*p != 1) throw "there can be only one"; }
656 constexpr ~Highlander() { --*p; }
657 };
658
659 struct X {
660 int *p;
661 constexpr X(int *p) : p(p) {}
662 constexpr X(const X &x, Highlander &&h = Highlander()) : p(x.p) {
663 h.set(p);
664 }
665 };
666
667 constexpr int f() {
668 int n = 0;
669 X x[3] = {&n, &n, &n};
670 auto [a, b, c] = x;
671 return n;
672 }
673
674 static_assert(f() == 0);
675 }
676
677 namespace NonPrimitiveOpaqueValue
678 {
679 struct X {
680 int x;
681 constexpr operator bool() const { return x != 0; }
682 };
683
684 constexpr int ternary() { return X(0) ?: X(0); }
685
686 static_assert(!ternary(), "");
687 }
688
689 namespace TryCatch {
690 constexpr int foo() {
691 int a = 10;
692 try {
693 ++a;
694 } catch(int m) {
695 --a;
696 }
697 return a;
698 }
699 static_assert(foo() == 11);
700 }
701
702 namespace IgnoredConstantExpr {
703 consteval int immediate() { return 0;}
704 struct ReferenceToNestedMembers {
705 int m;
706 int a = ((void)immediate(), m);
707 int b = ((void)immediate(), this->m);
708 };
709 struct ReferenceToNestedMembersTest {
710 void* m = nullptr;
711 ReferenceToNestedMembers j{0};
712 } test_reference_to_nested_members;
713 }
714
715 namespace RewrittenBinaryOperators {
716 template <class T, T Val>
717 struct Conv {
718 constexpr operator T() const { return Val; }
719 operator T() { return Val; }
720 };
721
722 struct X {
723 constexpr const Conv<int, -1> operator<=>(X) { return {}; }
724 };
725 static_assert(X() < X(), "");
726 }
727
728 namespace GH61417 {
729 struct A {
730 unsigned x : 1;
731 unsigned : 0;
732 unsigned y : 1;
733
734 constexpr A() : x(0), y(0) {}
735 bool operator==(const A& rhs) const noexcept = default;
736 };
737
738 void f1() {
739 constexpr A a, b;
740 constexpr bool c = (a == b); // no diagnostic, we should not be comparing the
741 // unnamed bit-field which is indeterminate
742 }
743
744 void f2() {
745 A a, b;
746 bool c = (a == b); // no diagnostic nor crash during codegen attempting to
747 // access info for unnamed bit-field
748 }
749 }
750
751 namespace FailingDestructor {
752 struct D {
753 int n;
754 bool can_destroy;
755
756 constexpr ~D() {
757 if (!can_destroy)
758 throw "oh no";
759 }
760 };
761 template<D d>
762 void f() {} // both-note {{invalid explicitly-specified argument}}
763
764 void g() {
765 f<D{0, false}>(); // both-error {{no matching function}}
766 }
767 }
768
769
770 void overflowInSwitchCase(int n) {
771 switch (n) {
772 case (int)(float)1e300: // both-error {{constant expression}} \
773 // both-note {{value +Inf is outside the range of representable values of type 'int'}}
774 break;
775 }
776 }
777
778 namespace APValues {
779 int g;
780 struct A { union { int n, m; }; int *p; int A::*q; char buffer[32]; };
781 template<A a> constexpr const A &get = a;
782 constexpr const A &v = get<A{}>;
783 constexpr const A &w = get<A{1, &g, &A::n, "hello"}>;
784 }
785
786 namespace self_referencing {
787 struct S {
788 S* ptr = nullptr;
789 constexpr S(int i) : ptr(this) {
790 if (this == ptr && i)
791 ptr = nullptr;
792 }
793 constexpr ~S() {}
794 };
795
796 void test() {
797 S s(1);
798 }
799 }
800
801 namespace GH64949 {
802 struct f {
803 int g; // both-note {{subobject declared here}}
804 constexpr ~f() {}
805 };
806
807 class h {
808 public:
809 consteval h(char *) {}
810 f i;
811 };
812
813 void test() { h{nullptr}; } // both-error {{call to consteval function 'GH64949::h::h' is not a constant expression}} \
814 // both-note {{subobject 'g' is not initialized}} \
815 // both-warning {{expression result unused}}
816 }
817
818 /// This used to cause an assertion failure inside EvaluationResult::checkFullyInitialized.
819 namespace CheckingNullPtrForInitialization {
820 struct X {
821 consteval operator const char *() const {
822 return nullptr;
823 }
824 };
825 const char *f() {
826 constexpr X x;
827 return x;
828 }
829 }