1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++20 -verify %s
2 // RUN: %clang_cc1 -std=c++20 -verify=ref %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
; // ref-note {{read of uninitialized object}} \
62 // expected-note {{read of object outside its lifetime}}
63 // FIXME: ^^^ Wrong diagnostic.
65 static_assert(unInitLocal() == 0, ""); // ref-error {{not an integral constant expression}} \
66 // ref-note {{in call to 'unInitLocal()'}} \
67 // expected-error {{not an integral constant expression}} \
68 // expected-note {{in call to 'unInitLocal()'}} \
70 constexpr int initializedLocal() {
75 static_assert(initializedLocal() == 20);
77 constexpr int initializedLocal2() {
79 return *a
; // expected-note {{read of object outside its lifetime}} \
80 // ref-note {{read of uninitialized object is not allowed in a constant expression}}
82 static_assert(initializedLocal2() == 20); // expected-error {{not an integral constant expression}} \
83 // expected-note {{in call to}} \
84 // ref-error {{not an integral constant expression}} \
85 // ref-note {{in call to}}
88 struct Int
{ int a
; }; // expected-note {{subobject declared here}}
89 constexpr int initializedLocal3() {
90 Int i
; // expected-note {{subobject of type 'int' is not initialized}}
91 return i
.a
; // ref-note {{read of uninitialized object is not allowed in a constant expression}}
93 static_assert(initializedLocal3() == 20); // expected-error {{not an integral constant expression}} \
94 // expected-note {{in call to}} \
95 // ref-error {{not an integral constant expression}} \
96 // ref-note {{in call to}}
101 // FIXME: This code should be rejected because we pass an uninitialized value
102 // as a function parameter.
103 constexpr int inc(int a
) { return a
+ 1; }
111 /// Distinct literals have disctinct addresses.
112 /// see https://github.com/llvm/llvm-project/issues/58754
113 constexpr auto foo(const char *p
) { return p
; }
114 constexpr auto p1
= "test1";
115 constexpr auto p2
= "test2";
117 constexpr bool b1
= foo(p1
) == foo(p1
);
120 constexpr bool b2
= foo(p1
) == foo(p2
); // ref-error {{must be initialized by a constant expression}} \
121 // ref-note {{comparison of addresses of literals}} \
122 // ref-note {{declared here}}
123 static_assert(!b2
); // ref-error {{not an integral constant expression}} \
124 // ref-note {{not a constant expression}}
126 constexpr auto name1() { return "name1"; }
127 constexpr auto name2() { return "name2"; }
129 constexpr auto b3
= name1() == name1();
131 constexpr auto b4
= name1() == name2(); // ref-error {{must be initialized by a constant expression}} \
132 // ref-note {{has unspecified value}} \
133 // ref-note {{declared here}}
134 static_assert(!b4
); // ref-error {{not an integral constant expression}} \
135 // ref-note {{not a constant expression}}
137 namespace UninitializedFields
{
140 int a
; // expected-note 2{{subobject declared here}} \
141 // ref-note 2{{subobject declared here}}
144 constexpr A a
; // expected-error {{must be initialized by a constant expression}} \
145 // expected-note {{subobject of type 'int' is not initialized}} \
146 // ref-error {{must be initialized by a constant expression}} \
147 // ref-note {{subobject of type 'int' is not initialized}}
153 int a
; // expected-note {{subobject declared here}} \
154 // ref-note {{subobject declared here}}
155 constexpr Base() : b(true) {}
158 class Derived
: public Base
{
160 constexpr Derived() : Base() {} // expected-note {{subobject of type 'int' is not initialized}}
163 constexpr Derived D
; // expected-error {{must be initialized by a constant expression}} \\
164 // expected-note {{in call to 'Derived()'}} \
165 // ref-error {{must be initialized by a constant expression}} \
166 // ref-note {{subobject of type 'int' is not initialized}}
171 constexpr C2() {} // expected-note {{subobject of type 'int' is not initialized}}
173 constexpr C2 c2
; // expected-error {{must be initialized by a constant expression}} \
174 // expected-note {{in call to 'C2()'}} \
175 // ref-error {{must be initialized by a constant expression}} \
176 // ref-note {{subobject of type 'int' is not initialized}}
179 // FIXME: These two are currently disabled because the array fields
180 // cannot be initialized.
187 constexpr C3 c3
; // expected-error {{must be initialized by a constant expression}} \
188 // expected-note {{subobject of type 'int' is not initialized}} \
189 // ref-error {{must be initialized by a constant expression}} \
190 // ref-note {{subobject of type 'int' is not initialized}}
194 bool B
[2][3]; // expected-note {{subobject declared here}} \
195 // ref-note {{subobject declared here}}
198 constexpr C4 c4
; // expected-error {{must be initialized by a constant expression}} \
199 // expected-note {{subobject of type 'bool' is not initialized}} \
200 // ref-error {{must be initialized by a constant expression}} \
201 // ref-note {{subobject of type 'bool' is not initialized}}