Debian Codegraph @

clang/test/SemaCXX/static-assert-cxx26.cpp

raw
1 // RUN: %clang_cc1 -std=c++2c -triple=x86_64-linux -fsyntax-only %s -verify
2 // RUN: %clang_cc1 -std=c++2c -triple=x86_64-linux -fsyntax-only %s -verify -fexperimental-new-constant-interpreter
3
4 static_assert(true, "");
5 static_assert(true, 0); // expected-error {{the message in a static assertion must be a string literal or an object with 'data()' and 'size()' member functions}}
6 struct Empty{};
7 static_assert(true, Empty{}); // expected-error {{the message object in this static assertion is missing 'data()' and 'size()' member functions}}
8 struct NoData {
9 unsigned long size() const;
10 };
11 struct NoSize {
12 const char* data() const;
13 };
14 static_assert(true, NoData{}); // expected-error {{the message object in this static assertion is missing a 'data()' member function}}
15 static_assert(true, NoSize{}); // expected-error {{the message object in this static assertion is missing a 'size()' member function}}
16
17 struct InvalidSize {
18 const char* size() const;
19 const char* data() const;
20 };
21 static_assert(true, InvalidSize{}); // expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}} \
22 // expected-error {{value of type 'const char *' is not implicitly convertible to '__size_t' (aka 'unsigned long')}}
23 struct InvalidData {
24 unsigned long size() const;
25 unsigned long data() const;
26 };
27 static_assert(true, InvalidData{}); // expected-error {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}} \
28 // expected-error {{value of type 'unsigned long' is not implicitly convertible to 'const char *'}}
29
30 struct NonConstexprSize {
31 unsigned long size() const; // expected-note 2{{declared here}}
32 constexpr const char* data() const;
33 };
34
35 static_assert(true, NonConstexprSize{}); // expected-error {{the message in this static assertion is not a constant expression}} \
36 // expected-note {{non-constexpr function 'size' cannot be used in a constant expression}}
37
38 static_assert(false, NonConstexprSize{}); // expected-error {{the message in a static assertion must be produced by a constant expression}} \
39 // expected-error {{static assertion failed}} \
40 // expected-note {{non-constexpr function 'size' cannot be used in a constant expression}}
41
42 struct NonConstexprData {
43 constexpr unsigned long size() const {
44 return 32;
45 }
46 const char* data() const; // expected-note 2{{declared here}}
47 };
48
49 static_assert(true, NonConstexprData{}); // expected-error {{the message in this static assertion is not a constant expression}} \
50 // expected-note {{non-constexpr function 'data' cannot be used in a constant expression}}
51
52 static_assert(false, NonConstexprData{}); // expected-error {{the message in a static assertion must be produced by a constant expression}} \
53 // expected-error {{static assertion failed}} \
54 // expected-note {{non-constexpr function 'data' cannot be used in a constant expression}}
55
56 struct string_view {
57 int S;
58 const char* D;
59 constexpr string_view(const char* Str) : S(__builtin_strlen(Str)), D(Str) {}
60 constexpr string_view(int Size, const char* Str) : S(Size), D(Str) {}
61 constexpr int size() const {
62 return S;
63 }
64 constexpr const char* data() const {
65 return D;
66 }
67 };
68
69 constexpr string_view operator+(auto, string_view S) {
70 return S;
71 }
72
73 constexpr const char g_[] = "long string";
74
75 template <typename T, int S>
76 struct array {
77 constexpr unsigned long size() const {
78 return S;
79 }
80 constexpr const char* data() const {
81 return d_;
82 }
83 const char d_[S];
84 };
85
86 static_assert(false, string_view("test")); // expected-error {{static assertion failed: test}}
87 static_assert(false, "Literal" + string_view("test")); // expected-error {{static assertion failed: test}}
88 static_assert(false, L"Wide Literal" + string_view("test")); // expected-error {{static assertion failed: test}}
89 static_assert(false, "Wild" "Literal" "Concatenation" + string_view("test")); // expected-error {{static assertion failed: test}}
90 static_assert(false, "Wild" "Literal" L"Concatenation" + string_view("test")); // expected-error {{static assertion failed: test}}
91 static_assert(false, "Wild" u"Literal" L"Concatenation" + string_view("test")); // expected-error {{unsupported non-standard concatenation of string literals}}
92 static_assert(false, string_view("😀")); // expected-error {{static assertion failed: 😀}}
93 static_assert(false, string_view(0, nullptr)); // expected-error {{static assertion failed:}}
94 static_assert(false, string_view(1, "ABC")); // expected-error {{static assertion failed: A}}
95 static_assert(false, string_view(42, "ABC")); // expected-error {{static assertion failed: ABC}} \
96 // expected-error {{the message in a static assertion must be produced by a constant expression}} \
97 // expected-note {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
98 static_assert(false, array<char, 2>{'a', 'b'}); // expected-error {{static assertion failed: ab}}
99
100
101
102 struct ConvertibleToInt {
103 constexpr operator int() {
104 return 4;
105 }
106 };
107 struct ConvertibleToCharPtr {
108 constexpr operator const char*() {
109 return "test";
110 }
111 };
112 struct MessageFromConvertible {
113 constexpr ConvertibleToInt size() const {
114 return {};
115 }
116 constexpr ConvertibleToCharPtr data() const {
117 return {};
118 }
119 };
120
121 static_assert(true, MessageFromConvertible{});
122 static_assert(false, MessageFromConvertible{}); // expected-error{{static assertion failed: test}}
123
124
125
126 struct Leaks {
127 constexpr unsigned long size() const {
128 return 2;
129 }
130 constexpr const char* data() const {
131 return new char[2]{'u', 'b'}; // expected-note {{allocation performed here was not deallocated}}
132 }
133 };
134
135 static_assert(false, Leaks{}); //expected-error {{the message in a static assertion must be produced by a constant expression}} \
136 // expected-error {{static assertion failed: ub}}
137
138 struct RAII {
139 const char* d = new char[2]{'o', 'k'};
140 constexpr unsigned long size() const {
141 return 2;
142 }
143
144 constexpr const char* data() const {
145 return d;
146 }
147
148 constexpr ~RAII() {
149 delete[] d;
150 }
151 };
152 static_assert(false, RAII{}); // expected-error {{static assertion failed: ok}}
153
154 namespace MoreTemporary {
155
156 struct Data{
157 constexpr operator const char*() const {
158 return d;
159 }
160 char d[6] = { "Hello" };
161 };
162
163 struct Size {
164 constexpr operator int() const {
165 return 5;
166 }
167 };
168
169 struct Message {
170 constexpr auto size() const {
171 return Size{};
172 }
173 constexpr auto data() const {
174 return Data{};
175 }
176 };
177
178 static_assert(false, Message{}); // expected-error {{static assertion failed: Hello}}
179
180 }
181
182 struct MessageInvalidSize {
183 constexpr unsigned long size(int) const; // expected-note {{'size' declared here}}
184 constexpr const char* data() const;
185 };
186 struct MessageInvalidData {
187 constexpr unsigned long size() const;
188 constexpr const char* data(int) const; // expected-note {{'data' declared here}}
189 };
190
191 static_assert(false, MessageInvalidSize{}); // expected-error {{static assertion failed}} \
192 // expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}} \
193 // expected-error {{too few arguments to function call, expected 1, have 0}}
194 static_assert(false, MessageInvalidData{}); // expected-error {{static assertion failed}} \
195 // expected-error {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}} \
196 // expected-error {{too few arguments to function call, expected 1, have 0}}
197
198 struct NonConstMembers {
199 constexpr int size() {
200 return 1;
201 }
202 constexpr const char* data() {
203 return "A";
204 }
205 };
206
207 static_assert(false, NonConstMembers{}); // expected-error {{static assertion failed: A}}
208
209 struct DefaultArgs {
210 constexpr int size(int i = 0) {
211 return 2;
212 }
213 constexpr const char* data(int i =0, int j = 42) {
214 return "OK";
215 }
216 };
217
218 static_assert(false, DefaultArgs{}); // expected-error {{static assertion failed: OK}}
219
220 struct Variadic {
221 constexpr int size(auto...) {
222 return 2;
223 }
224 constexpr const char* data(auto...) {
225 return "OK";
226 }
227 };
228
229 static_assert(false, Variadic{}); // expected-error {{static assertion failed: OK}}
230
231 template <typename T>
232 struct DeleteAndRequires {
233 constexpr int size() = delete; // expected-note {{'size' has been explicitly marked deleted here}}
234 constexpr const char* data() requires false; // expected-note {{because 'false' evaluated to false}}
235 };
236 static_assert(false, DeleteAndRequires<void>{});
237 // expected-error@-1 {{static assertion failed}} \
238 // expected-error@-1 {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}}\
239 // expected-error@-1 {{invalid reference to function 'data': constraints not satisfied}} \
240 // expected-error@-1 {{attempt to use a deleted function}}
241
242 class Private {
243 constexpr int size(int i = 0) { // expected-note {{implicitly declared private here}}
244 return 2;
245 }
246 constexpr const char* data(int i =0, int j = 42) { // expected-note {{implicitly declared private here}}
247 return "OK";
248 }
249 };
250
251 static_assert(false, Private{}); // expected-error {{'data' is a private member of 'Private'}}\
252 // expected-error {{'size' is a private member of 'Private'}}\
253 // expected-error {{static assertion failed: OK}}
254
255 struct MessageOverload {
256 constexpr int size() {
257 return 1;
258 }
259 constexpr int size() const;
260
261 constexpr const char* data() {
262 return "A";
263 }
264 constexpr const char* data() const;
265 };
266
267 static_assert(false, MessageOverload{}); // expected-error {{static assertion failed: A}}
268
269 struct InvalidPtr {
270 consteval auto size() {
271 return 42;
272 }
273 consteval const char *data() {
274 const char *ptr; // Garbage
275 return ptr; // expected-note {{read of uninitialized object is not allowed in a constant expression}}
276 }
277 };
278
279 static_assert(false, InvalidPtr{}); // expected-error{{the message in a static assertion must be produced by a constant expression}} \
280 //expected-error {{static assertion failed}} \
281 // expected-note {{in call to 'InvalidPtr{}.data()'}}
282
283 namespace DependentMessage {
284 template <typename Ty>
285 struct Good {
286 static_assert(false, Ty{}); // expected-error {{static assertion failed: hello}}
287 };
288
289 template <typename Ty>
290 struct Bad {
291 static_assert(false, Ty{}); // expected-error {{the message in a static assertion must be a string literal or an object with 'data()' and 'size()' member functions}} \
292 // expected-error {{static assertion failed}}
293 };
294
295 struct Frobble {
296 constexpr int size() const { return 5; }
297 constexpr const char *data() const { return "hello"; }
298 };
299
300 constexpr Frobble operator ""_myd (const char *, unsigned long) { return Frobble{}; }
301 static_assert (false, "foo"_myd); // expected-error {{static assertion failed: hello}}
302
303 Good<Frobble> a; // expected-note {{in instantiation}}
304 Bad<int> b; // expected-note {{in instantiation}}
305
306 }
307
308 namespace EscapeInDiagnostic {
309 static_assert('\u{9}' == (char)1, ""); // expected-error {{failed}} \
310 // expected-note {{evaluates to ''\t' (0x09, 9) == '<U+0001>' (0x01, 1)'}}
311 static_assert((char8_t)-128 == (char8_t)-123, ""); // expected-error {{failed}} \
312 // expected-note {{evaluates to 'u8'<80>' (0x80, 128) == u8'<85>' (0x85, 133)'}}
313 static_assert((char16_t)0xFEFF == (char16_t)0xDB93, ""); // expected-error {{failed}} \
314 // expected-note {{evaluates to 'u'' (0xFEFF, 65279) == u'\xDB93' (0xDB93, 56211)'}}
315 }
316
317 struct Static {
318 static constexpr int size() { return 5; }
319 static constexpr const char *data() { return "hello"; }
320 };
321 static_assert(false, Static{}); // expected-error {{static assertion failed: hello}}
322
323 struct Data {
324 unsigned long size = 0;
325 const char* data = "hello";
326 };
327 static_assert(false, Data{}); // expected-error {{called object type 'unsigned long' is not a function or function pointer}} \
328 // expected-error {{called object type 'const char *' is not a function or function pointer}} \
329 // expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}} \
330 // expected-error {{static assertion failed}}
331
332 struct Callable {
333 struct {
334 constexpr auto operator()() const {
335 return 5;
336 };
337 } size;
338 struct {
339 constexpr auto operator()() const {
340 return "hello";
341 };
342 } data;
343 };
344 static_assert(false, Callable{}); // expected-error {{static assertion failed: hello}}
345
346 namespace GH89407 {
347 struct A {
348 constexpr __SIZE_TYPE__ size() const { return -1; }
349 constexpr const char* data() const { return ""; }
350 };
351
352 struct B {
353 constexpr long long size() const { return 18446744073709551615U; }
354 constexpr const char* data() const { return ""; }
355 };
356
357 struct C {
358 constexpr __int128 size() const { return -1; }
359 constexpr const char* data() const { return ""; }
360 };
361
362 struct D {
363 constexpr unsigned __int128 size() const { return -1; }
364 constexpr const char* data() const { return ""; }
365 };
366
367 struct E {
368 constexpr __SIZE_TYPE__ size() const { return 18446744073709551615U; }
369 constexpr const char* data() const { return ""; }
370 };
371
372 static_assert(true, A{}); // expected-error {{the message in this static assertion is not a constant expression}}
373 // expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
374 static_assert(true, B{}); // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type '__size_t' (aka 'unsigned long')}}
375 // expected-error@-1 {{the message in this static assertion is not a constant expression}}
376 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
377 static_assert(true, C{}); // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type '__size_t' (aka 'unsigned long')}}
378 // expected-error@-1 {{the message in this static assertion is not a constant expression}}
379 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
380 static_assert(true, D{}); // expected-error {{call to 'size()' evaluates to 340282366920938463463374607431768211455, which cannot be narrowed to type '__size_t' (aka 'unsigned long')}}
381 // expected-error@-1 {{the message in this static assertion is not a constant expression}}
382 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
383 static_assert(true, E{}); // expected-error {{the message in this static assertion is not a constant expression}}
384 // expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
385
386 static_assert(
387 false, // expected-error {{static assertion failed}}
388 A{} // expected-error {{the message in a static assertion must be produced by a constant expression}}
389 // expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
390 );
391
392 static_assert(
393 false, // expected-error {{static assertion failed}}
394 B{} // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type '__size_t' (aka 'unsigned long')}}
395 // expected-error@-1 {{the message in a static assertion must be produced by a constant expression}}
396 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
397 );
398
399 static_assert(
400 false, // expected-error {{static assertion failed}}
401 C{} // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type '__size_t' (aka 'unsigned long')}}
402 // expected-error@-1 {{the message in a static assertion must be produced by a constant expression}}
403 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
404 );
405
406 static_assert(
407 false, // expected-error {{static assertion failed}}
408 D{} // expected-error {{call to 'size()' evaluates to 340282366920938463463374607431768211455, which cannot be narrowed to type '__size_t' (aka 'unsigned long')}}
409 // expected-error@-1 {{the message in a static assertion must be produced by a constant expression}}
410 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
411 );
412
413 static_assert(
414 false, // expected-error {{static assertion failed}}
415 E{} // expected-error {{the message in a static assertion must be produced by a constant expression}}
416 // expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
417 );
418 }