Debian Codegraph @

clang/test/AST/Interp/cxx23.cpp

raw
1 // UNSUPPORTED: target={{.*}}-zos{{.*}}
2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -fcxx-exceptions -verify=ref,ref20,all,all20 %s
3 // RUN: %clang_cc1 -std=c++23 -fsyntax-only -fcxx-exceptions -verify=ref,ref23,all,all23 %s
4 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -fcxx-exceptions -verify=expected20,all,all20 %s -fexperimental-new-constant-interpreter
5 // RUN: %clang_cc1 -std=c++23 -fsyntax-only -fcxx-exceptions -verify=expected23,all,all23 %s -fexperimental-new-constant-interpreter
6
7 constexpr int f(int n) { // all20-error {{constexpr function never produces a constant expression}}
8 static const int m = n; // all-note {{control flows through the definition of a static variable}} \
9 // all20-note {{control flows through the definition of a static variable}} \
10 // all20-warning {{is a C++23 extension}}
11
12 return m;
13 }
14 static_assert(f(0) == 0, ""); // all-error {{not an integral constant expression}} \
15 // all-note {{in call to}}
16
17 constexpr int g(int n) { // all20-error {{constexpr function never produces a constant expression}}
18 thread_local const int m = n; // all-note {{control flows through the definition of a thread_local variable}} \
19 // all20-note {{control flows through the definition of a thread_local variable}} \
20 // all20-warning {{is a C++23 extension}}
21 return m;
22 }
23 static_assert(g(0) == 0, ""); // all-error {{not an integral constant expression}} \
24 // all-note {{in call to}}
25
26 constexpr int c_thread_local(int n) { // all20-error {{constexpr function never produces a constant expression}}
27 static _Thread_local int m = 0; // all20-note 2{{control flows through the definition of a thread_local variable}} \
28 // all23-note {{control flows through the definition of a thread_local variable}} \
29 // all20-warning {{is a C++23 extension}}
30 return m;
31 }
32 static_assert(c_thread_local(0) == 0, ""); // all-error {{not an integral constant expression}} \
33 // all-note {{in call to}}
34
35
36 constexpr int gnu_thread_local(int n) { // all20-error {{constexpr function never produces a constant expression}}
37 static __thread 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(gnu_thread_local(0) == 0, ""); // all-error {{not an integral constant expression}} \
43 // all-note {{in call to}}
44
45 constexpr int h(int n) { // all20-error {{constexpr function never produces a constant expression}}
46 static const int m = n; // all20-note {{control flows through the definition of a static variable}} \
47 // all20-warning {{is a C++23 extension}}
48 return &m - &m;
49 }
50
51 constexpr int i(int n) { // all20-error {{constexpr function never produces a constant expression}}
52 thread_local const int m = n; // all20-note {{control flows through the definition of a thread_local variable}} \
53 // all20-warning {{is a C++23 extension}}
54 return &m - &m;
55 }
56
57 constexpr int j(int n) {
58 if (!n)
59 return 0;
60 static const int m = n; // ref20-warning {{is a C++23 extension}} \
61 // expected20-warning {{is a C++23 extension}}
62 return m;
63 }
64 constexpr int j0 = j(0);
65
66 constexpr int k(int n) {
67 if (!n)
68 return 0;
69 thread_local const int m = n; // ref20-warning {{is a C++23 extension}} \
70 // expected20-warning {{is a C++23 extension}}
71
72 return m;
73 }
74 constexpr int k0 = k(0);
75
76 namespace StaticLambdas {
77 constexpr auto static_capture_constexpr() {
78 char n = 'n';
79 return [n] static { return n; }(); // expected23-error {{a static lambda cannot have any captures}} \
80 // expected20-error {{a static lambda cannot have any captures}} \
81 // expected20-warning {{are a C++23 extension}} \
82 // expected20-warning {{is a C++23 extension}} \
83 // ref23-error {{a static lambda cannot have any captures}} \
84 // ref20-error {{a static lambda cannot have any captures}} \
85 // ref20-warning {{are a C++23 extension}} \
86 // ref20-warning {{is a C++23 extension}}
87 }
88 static_assert(static_capture_constexpr()); // expected23-error {{static assertion expression is not an integral constant expression}} \
89 // expected20-error {{static assertion expression is not an integral constant expression}} \
90 // ref23-error {{static assertion expression is not an integral constant expression}} \
91 // ref20-error {{static assertion expression is not an integral constant expression}}
92
93 constexpr auto capture_constexpr() {
94 char n = 'n';
95 return [n] { return n; }();
96 }
97 static_assert(capture_constexpr());
98 }
99
100 namespace StaticOperators {
101 auto lstatic = []() static { return 3; }; // ref20-warning {{C++23 extension}} \
102 // expected20-warning {{C++23 extension}}
103 static_assert(lstatic() == 3, "");
104 constexpr int (*f2)(void) = lstatic;
105 static_assert(f2() == 3);
106
107 struct S1 {
108 constexpr S1() { // all20-error {{never produces a constant expression}}
109 throw; // all-note {{not valid in a constant expression}} \
110 // all20-note {{not valid in a constant expression}}
111 }
112 static constexpr int operator()() { return 3; } // ref20-warning {{C++23 extension}} \
113 // expected20-warning {{C++23 extension}}
114 };
115 static_assert(S1{}() == 3, ""); // all-error {{not an integral constant expression}} \
116 // all-note {{in call to}}
117
118
119
120 }
121
122 int test_in_lambdas() {
123 auto c = [](int n) constexpr {
124 if (n == 0)
125 return 0;
126 else
127 goto test; // all-note {{subexpression not valid in a constant expression}} \
128 // all20-warning {{use of this statement in a constexpr function is a C++23 extension}}
129 test:
130 return 1;
131 };
132 c(0);
133 constexpr auto A = c(1); // all-error {{must be initialized by a constant expression}} \
134 // all-note {{in call to}}
135 return 0;
136 }
137
138 /// PackIndexExpr.
139 template <auto... p>
140 struct check_ice {
141 enum e {
142 x = p...[0] // all-warning {{is a C++2c extension}}
143 };
144 };
145 static_assert(check_ice<42>::x == 42);
146
147
148 namespace VirtualBases {
149 namespace One {
150 struct U { int n; };
151 struct V : U { int n; };
152 struct A : virtual V { int n; };
153 struct Aa { int n; };
154 struct B : virtual A, Aa {};
155 struct C : virtual A, Aa {};
156 struct D : B, C {};
157
158 /// Calls the constructor of D.
159 D d;
160 }
161 }
162
163 namespace LabelGoto {
164 constexpr int foo() { // all20-error {{never produces a constant expression}}
165 a: // all20-warning {{use of this statement in a constexpr function is a C++23 extension}}
166 goto a; // all20-note 2{{subexpression not valid in a constant expression}} \
167 // ref23-note {{subexpression not valid in a constant expression}} \
168 // expected23-note {{subexpression not valid in a constant expression}}
169
170 return 1;
171 }
172 static_assert(foo() == 1, ""); // all-error {{not an integral constant expression}} \
173 // all-note {{in call to}}
174 }
175
176 namespace ExplicitLambdaThis {
177 constexpr auto f = [x = 3]<typename Self>(this Self self) { // all20-error {{explicit object parameters are incompatible with C++ standards before C++2b}}
178 return x;
179 };
180 static_assert(f());
181 }
182
183 namespace std {
184 struct strong_ordering {
185 int n;
186 constexpr operator int() const { return n; }
187 static const strong_ordering less, equal, greater;
188 };
189 constexpr strong_ordering strong_ordering::less = {-1};
190 constexpr strong_ordering strong_ordering::equal = {0};
191 constexpr strong_ordering strong_ordering::greater = {1};
192 }
193
194 namespace UndefinedThreeWay {
195 struct A {
196 friend constexpr std::strong_ordering operator<=>(const A&, const A&) = default; // all-note {{declared here}}
197 };
198
199 constexpr std::strong_ordering operator<=>(const A&, const A&) noexcept;
200 constexpr std::strong_ordering (*test_a_threeway)(const A&, const A&) = &operator<=>;
201 static_assert(!(*test_a_threeway)(A(), A())); // all-error {{static assertion expression is not an integral constant expression}} \
202 // all-note {{undefined function 'operator<=>' cannot be used in a constant expression}}
203 }
204
205 /// FIXME: The new interpreter is missing the "initializer of q is not a constant expression" diagnostics.a
206 /// That's because the cast from void* to int* is considered fine, but diagnosed. So we don't consider
207 /// q to be uninitialized.
208 namespace VoidCast {
209 constexpr void* p = nullptr;
210 constexpr int* q = static_cast<int*>(p); // all-error {{must be initialized by a constant expression}} \
211 // all-note {{cast from 'void *' is not allowed in a constant expression}} \
212 // ref-note {{declared here}}
213 static_assert(q == nullptr); // ref-error {{not an integral constant expression}} \
214 // ref-note {{initializer of 'q' is not a constant expression}}
215 }
216
217 namespace ExplicitLambdaInstancePointer {
218 struct C {
219 constexpr C(auto) { }
220 };
221 void foo() {
222 constexpr auto b = [](this C) { return 1; }; // all20-error {{explicit object parameters are incompatible with C++ standards before C++2b}}
223 constexpr int (*fp)(C) = b;
224 static_assert(fp(1) == 1, "");
225 }
226 }