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
4 void test_alignas_operand() {
6 static_assert(__alignof(dummy
) == 8);
9 constexpr int getMinus5() {
15 static_assert(getMinus5() == -5, "");
17 constexpr int assign() {
25 static_assert(assign() == 20, "");
28 constexpr int pointerAssign() {
32 *p
= 12; // modifies m
36 static_assert(pointerAssign() == 12, "");
38 constexpr int pointerDeref() {
44 static_assert(pointerDeref() == 12, "");
46 constexpr int pointerAssign2() {
57 static_assert(pointerAssign2() == 12, "");
59 constexpr int unInitLocal() {
61 return a
; // both-note {{read of uninitialized object}}
63 static_assert(unInitLocal() == 0, ""); // both-error {{not an integral constant expression}} \
64 // both-note {{in call to 'unInitLocal()'}}
66 constexpr int initializedLocal() {
71 static_assert(initializedLocal() == 20);
73 constexpr int initializedLocal2() {
75 return *a
; // both-note {{read of uninitialized object is not allowed in a constant expression}}
77 static_assert(initializedLocal2() == 20); // both-error {{not an integral constant expression}} \
78 // both-note {{in call to}}
81 struct Int
{ int a
; };
82 constexpr int initializedLocal3() {
84 return i
.a
; // both-note {{read of uninitialized object is not allowed in a constant expression}}
86 static_assert(initializedLocal3() == 20); // both-error {{not an integral constant expression}} \
87 // both-note {{in call to}}
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; }
102 /// Distinct literals have distinct 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";
108 constexpr bool b1
= foo(p1
) == foo(p1
);
111 constexpr bool b2
= foo(p1
) == foo(p2
);
114 constexpr auto name1() { return "name1"; }
115 constexpr auto name2() { return "name2"; }
117 constexpr auto b3
= name1() == name1(); // ref-error {{must be initialized by a constant expression}} \
118 // ref-note {{comparison of addresses of literals}}
119 constexpr auto b4
= name1() == name2();
122 namespace UninitializedFields
{
125 int a
; // both-note 4{{subobject declared here}}
128 constexpr A a
; // both-error {{must be initialized by a constant expression}} \
129 // both-note {{subobject 'a' is not initialized}}
130 constexpr A aarr
[2]; // both-error {{must be initialized by a constant expression}} \
131 // both-note {{subobject 'a' is not initialized}}
134 int f
; // both-note 3{{subobject declared here}}
137 constexpr F(bool b
) {
143 constexpr F foo
[2] = {true}; // both-error {{must be initialized by a constant expression}} \
144 // both-note {{subobject 'f' is not initialized}}
145 constexpr F foo2
[3] = {true, false, true}; // both-error {{must be initialized by a constant expression}} \
146 // both-note {{subobject 'f' is not initialized}}
147 constexpr F foo3
[3] = {true, true, F()}; // both-error {{must be initialized by a constant expression}} \
148 // both-note {{subobject 'f' is not initialized}}
155 int a
; // both-note {{subobject declared here}}
156 constexpr Base() : b(true) {}
159 class Derived
: public Base
{
161 constexpr Derived() : Base() {} };
163 constexpr Derived D
; // both-error {{must be initialized by a constant expression}} \
164 // both-note {{subobject 'a' is not initialized}}
170 constexpr C2 c2
; // both-error {{must be initialized by a constant expression}} \
171 // both-note {{subobject 'a' is not initialized}}
178 constexpr C3 c3
; // both-error {{must be initialized by a constant expression}} \
179 // both-note {{subobject 'a' is not initialized}}
183 bool B
[2][3]; // both-note {{subobject declared here}}
186 constexpr C4 c4
; // both-error {{must be initialized by a constant expression}} \
187 // both-note {{subobject 'B' is not initialized}}
190 namespace ConstThis
{
192 const int T
= 12; // both-note {{declared const here}}
197 T
= 13; // both-error {{cannot assign to non-static data member 'T' with const-qualified type}}
200 constexpr Foo F
; // both-error {{must be initialized by a constant expression}}
206 constexpr FooDtor() {
209 constexpr ~FooDtor() {
214 constexpr int foo() {
218 static_assert(foo() == 0);
224 constexpr ctor_test() {
227 int local
= 100 / a
; // both-note {{division by zero}}
235 constexpr dtor_test() = default;
236 constexpr ~dtor_test() {
239 int local
= 100 / a
; // both-note {{division by zero}}
243 constexpr ctor_test
<true> good_ctor
;
244 constexpr dtor_test
<true> good_dtor
;
246 constexpr ctor_test
<false> bad_ctor
; // both-error {{must be initialized by a constant expression}} \
247 // both-note {{in call to}}
248 constexpr dtor_test
<false> bad_dtor
; // both-error {{must have constant destruction}} \
249 // both-note {{in call to}}
257 struct Intermediate
: Base
{
261 struct Final
: Intermediate
{
264 constexpr Final(int a
, int b
, int c
) : c(c
) {}
267 static_assert(Final
{1, 2, 3}.c
== 3, ""); // OK
268 static_assert(Final
{1, 2, 3}.a
== 0, ""); // both-error {{not an integral constant expression}} \
269 // both-note {{read of uninitialized object}}
275 constexpr Mixin() = default;
276 constexpr Mixin(int b
) : b(b
) {}
279 struct Final2
: Base
, Mixin
{
282 constexpr Final2(int a
, int b
, int c
) : Mixin(b
), c(c
) {}
283 constexpr Final2(int a
, int b
, int c
, bool) : c(c
) {}
286 static_assert(Final2
{1, 2, 3}.c
== 3, ""); // OK
287 static_assert(Final2
{1, 2, 3}.b
== 2, ""); // OK
288 static_assert(Final2
{1, 2, 3}.a
== 0, ""); // both-error {{not an integral constant expression}} \
289 // both-note {{read of uninitialized object}}
296 struct Final3
: Base
, Mixin3
{
299 constexpr Final3(int a
, int b
, int c
) : c(c
) { this->b
= b
; }
300 constexpr Final3(int a
, int b
, int c
, bool) : c(c
) {}
303 static_assert(Final3
{1, 2, 3}.c
== 3, ""); // OK
304 static_assert(Final3
{1, 2, 3}.b
== 2, ""); // OK
305 static_assert(Final3
{1, 2, 3}.a
== 0, ""); // both-error {{not an integral constant expression}} \
306 // both-note {{read of uninitialized object}}
309 namespace Destructors
{
314 constexpr Inc(int &I
) : I(I
) {}
323 constexpr Dec(int &I
) : I(I
) {}
340 static_assert(m() == 3, "");
353 static_assert(C() == 10, "");
369 static_assert(D() == 0, "");
380 static_assert(E() == 1, "");
383 /// FIXME: This should be rejected, since we call the destructor
384 /// twice. However, GCC doesn't care either.
385 constexpr int ManualDtor() {
388 Inc
I(i
); // ref-note {{destroying object 'I' whose lifetime has already ended}}
393 static_assert(ManualDtor() == 1, ""); // expected-error {{static assertion failed}} \
394 // expected-note {{evaluates to '2 == 1'}} \
395 // ref-error {{not an integral constant expression}} \
396 // ref-note {{in call to 'ManualDtor()'}}
398 constexpr void doInc(int &i
) {
402 constexpr int testInc() {
407 static_assert(testInc() == 1, "");
408 constexpr void doInc2(int &i
) {
410 // No return statement.
412 constexpr int testInc2() {
417 static_assert(testInc2() == 1, "");
420 namespace DtorOrder
{
424 constexpr A(int &I
) : I(I
) {}
432 constexpr B(int &I
) : A(I
) {}
438 constexpr int foo() {
446 static_assert(foo() == 1337);
453 constexpr FieldDtor1(int &I
) : I1(I
), I2(I
){}
456 constexpr int foo2() {
464 static_assert(foo2() == 2);
469 constexpr FieldDtor2(int &I
) : Incs
{Inc(I
), Inc(I
), Inc(I
)} {}
472 constexpr int foo3() {
480 static_assert(foo3() == 3);
491 constexpr bool ArrayOrder() {
492 int order
[3] = {0, 0, 0};
500 // ds will be destroyed.
502 return order
[0] == 3 && order
[1] == 2 && order
[2] == 1;
504 static_assert(ArrayOrder());
507 // Static members aren't destroyed.
517 static constexpr Dec2 a
;
520 static_assert(Foo::a
.A
== 0);
525 static_assert(Foo::a
.A
== 0);
527 static_assert(Foo::a
.A
== 0);
530 struct NotConstexpr
{
536 constexpr Outer() = default;
539 constexpr int foo() {
543 constexpr int bar()const {
544 return Outer
{}.foo();
547 static NotConstexpr Val
;
550 constexpr Outer::~Outer() {}
553 static_assert(O
.bar() == 12);
556 namespace BaseAndFieldInit
{
569 constexpr C c
= {1,2,3};
570 static_assert(c
.a
== 1 && c
.b
== 2 && c
.c
== 3);
573 namespace ImplicitFunction
{
575 int a
; // ref-note {{subobject declared here}}
578 constexpr int callMe() {
582 /// The operator= call here will fail and the diagnostics should be fine.
583 b
= a
; // ref-note {{subobject 'a' is not initialized}} \
584 // expected-note {{read of uninitialized object}} \
585 // both-note {{in call to}}
589 static_assert(callMe() == 1, ""); // both-error {{not an integral constant expression}} \
590 // both-note {{in call to 'callMe()'}}
594 class strong_ordering
{
597 static const strong_ordering less
, equal
, greater
;
598 constexpr bool operator==(int n
) const noexcept
{ return this->n
== n
;}
599 constexpr bool operator!=(int n
) const noexcept
{ return this->n
!= n
;}
601 constexpr strong_ordering
strong_ordering::less
= {-1};
602 constexpr strong_ordering
strong_ordering::equal
= {0};
603 constexpr strong_ordering
strong_ordering::greater
= {1};
605 class partial_ordering
{
608 static const partial_ordering less
, equal
, greater
, equivalent
, unordered
;
609 constexpr bool operator==(long n
) const noexcept
{ return this->n
== n
;}
610 constexpr bool operator!=(long n
) const noexcept
{ return this->n
!= n
;}
612 constexpr partial_ordering
partial_ordering::less
= {-1};
613 constexpr partial_ordering
partial_ordering::equal
= {0};
614 constexpr partial_ordering
partial_ordering::greater
= {1};
615 constexpr partial_ordering
partial_ordering::equivalent
= {0};
616 constexpr partial_ordering
partial_ordering::unordered
= {-127};
619 namespace ThreeWayCmp
{
620 static_assert(1 <=> 2 == -1, "");
621 static_assert(1 <=> 1 == 0, "");
622 static_assert(2 <=> 1 == 1, "");
623 static_assert(1.0 <=> 2.f
== -1, "");
624 static_assert(1.0 <=> 1.0 == 0, "");
625 static_assert(2.0 <=> 1.0 == 1, "");
626 constexpr int k
= (1 <=> 1, 0); // both-warning {{comparison result unused}}
627 static_assert(k
== 0, "");
630 constexpr int a
[] = {1,2,3};
631 constexpr int b
[] = {1,2,3};
632 constexpr const int *pa1
= &a
[1];
633 constexpr const int *pa2
= &a
[2];
634 constexpr const int *pb1
= &b
[1];
635 static_assert(pa1
<=> pb1
!= 0, ""); // both-error {{not an integral constant expression}} \
636 // both-note {{has unspecified value}}
637 static_assert(pa1
<=> pa1
== 0, "");
638 static_assert(pa1
<=> pa2
== -1, "");
639 static_assert(pa2
<=> pa1
== 1, "");
642 namespace ConstexprArrayInitLoopExprDestructors
646 constexpr Highlander() {}
647 constexpr void set(int *p
) { this->p
= p
; ++*p
; if (*p
!= 1) throw "there can be only one"; }
648 constexpr ~Highlander() { --*p
; }
653 constexpr X(int *p
) : p(p
) {}
654 constexpr X(const X
&x
, Highlander
&&h
= Highlander()) : p(x
.p
) {
661 X x
[3] = {&n
, &n
, &n
};
666 static_assert(f() == 0);
669 namespace NonPrimitiveOpaqueValue
673 constexpr operator bool() const { return x
!= 0; }
676 constexpr int ternary() { return X(0) ?: X(0); }
678 static_assert(!ternary(), "");
682 constexpr int foo() {
691 static_assert(foo() == 11);
694 namespace IgnoredConstantExpr
{
695 consteval
int immediate() { return 0;}
696 struct ReferenceToNestedMembers
{
698 int a
= ((void)immediate(), m
);
699 int b
= ((void)immediate(), this->m
);
701 struct ReferenceToNestedMembersTest
{
703 ReferenceToNestedMembers j
{0};
704 } test_reference_to_nested_members
;
707 namespace RewrittenBinaryOperators
{
708 template <class T
, T Val
>
710 constexpr operator T() const { return Val
; }
711 operator T() { return Val
; }
715 constexpr const Conv
<int, -1> operator<=>(X
) { return {}; }
717 static_assert(X() < X(), "");
726 constexpr A() : x(0), y(0) {}
727 bool operator==(const A
& rhs
) const noexcept
= default;
732 constexpr bool c
= (a
== b
); // no diagnostic, we should not be comparing the
733 // unnamed bit-field which is indeterminate
738 bool c
= (a
== b
); // no diagnostic nor crash during codegen attempting to
739 // access info for unnamed bit-field
743 namespace FailingDestructor
{
754 void f() {} // both-note {{invalid explicitly-specified argument}}
757 f
<D
{0, false}>(); // both-error {{no matching function}}
762 void overflowInSwitchCase(int n
) {
764 case (int)(float)1e300
: // both-error {{constant expression}} \
765 // both-note {{value +Inf is outside the range of representable values of type 'int'}}
772 struct A
{ union { int n
, m
; }; int *p
; int A::*q
; char buffer
[32]; };
773 template<A a
> constexpr const A
&get
= a
;
774 constexpr const A
&v
= get
<A
{}>;
775 constexpr const A
&w
= get
<A
{1, &g
, &A::n
, "hello"}>;
778 namespace self_referencing
{
781 constexpr S(int i
) : ptr(this) {
782 if (this == ptr
&& i
)
795 int g
; // both-note {{subobject declared here}}
801 consteval
h(char *) {}
805 void test() { h
{nullptr}; } // both-error {{call to consteval function 'GH64949::h::h' is not a constant expression}} \
806 // both-note {{subobject 'g' is not initialized}} \
807 // both-warning {{expression result unused}}
810 /// This used to cause an assertion failure inside EvaluationResult::checkFullyInitialized.
811 namespace CheckingNullPtrForInitialization
{
813 consteval
operator const char *() const {
823 namespace VariadicCallOperator
{
826 constexpr void operator()(int a
, int b
, ...) {}
828 constexpr int foo() {
834 constexpr int A
= foo();
837 namespace DefinitionLoc
{
839 struct NonConstexprCopy
{
840 constexpr NonConstexprCopy() = default;
841 NonConstexprCopy(const NonConstexprCopy
&);
842 constexpr NonConstexprCopy(NonConstexprCopy
&&) = default;
847 NonConstexprCopy::NonConstexprCopy(const NonConstexprCopy
&) = default; // both-note {{here}}
849 constexpr NonConstexprCopy ncc1
= NonConstexprCopy(NonConstexprCopy());
850 constexpr NonConstexprCopy ncc2
= ncc1
; // both-error {{constant expression}} \
851 // both-note {{non-constexpr constructor}}
857 constexpr B(char *p
) : p(p
) {}
858 virtual constexpr ~B() {
868 constexpr C(char *p
) : B(p
) {}
869 virtual constexpr ~C() override
{
876 constexpr U(char *p
) : c(p
) {}
882 constexpr int test(char a
, char b
) {
886 /// U is a union, so it won't call the destructor of its fields.
887 /// We do this manually here. Explicitly calling ~C() here should
888 /// also call the destructor of the base classes however.
891 return buff
[0] == a
&& buff
[1] == b
;
894 static_assert(test('C', 'B'));