[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / static-assert-cxx26.cpp
blob7d896d8b365b740e3dec35971f7cb47eabccca6b
1 // RUN: %clang_cc1 -std=c++2c -triple=x86_64-linux -fsyntax-only %s -verify
3 static_assert(true, "");
4 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}}
5 struct Empty{};
6 static_assert(true, Empty{}); // expected-error {{the message object in this static assertion is missing 'data()' and 'size()' member functions}}
7 struct NoData {
8 unsigned long size() const;
9 };
10 struct NoSize {
11 const char* data() const;
13 static_assert(true, NoData{}); // expected-error {{the message object in this static assertion is missing a 'data()' member function}}
14 static_assert(true, NoSize{}); // expected-error {{the message object in this static assertion is missing a 'size()' member function}}
16 struct InvalidSize {
17 const char* size() const;
18 const char* data() const;
20 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'}} \
21 // expected-error {{value of type 'const char *' is not implicitly convertible to 'unsigned long'}}
22 struct InvalidData {
23 unsigned long size() const;
24 unsigned long data() const;
26 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 *'}} \
27 // expected-error {{value of type 'unsigned long' is not implicitly convertible to 'const char *'}}
29 struct NonConstexprSize {
30 unsigned long size() const; // expected-note 2{{declared here}}
31 constexpr const char* data() const;
34 static_assert(true, NonConstexprSize{}); // expected-error {{the message in this static assertion is not a constant expression}} \
35 // expected-note {{non-constexpr function 'size' cannot be used in a constant expression}}
37 static_assert(false, NonConstexprSize{}); // expected-error {{the message in a static assertion must be produced by a constant expression}} \
38 // expected-error {{static assertion failed}} \
39 // expected-note {{non-constexpr function 'size' cannot be used in a constant expression}}
41 struct NonConstexprData {
42 constexpr unsigned long size() const {
43 return 32;
45 const char* data() const; // expected-note 2{{declared here}}
48 static_assert(true, NonConstexprData{}); // expected-error {{the message in this static assertion is not a constant expression}} \
49 // expected-note {{non-constexpr function 'data' cannot be used in a constant expression}}
51 static_assert(false, NonConstexprData{}); // expected-error {{the message in a static assertion must be produced by a constant expression}} \
52 // expected-error {{static assertion failed}} \
53 // expected-note {{non-constexpr function 'data' cannot be used in a constant expression}}
55 struct string_view {
56 int S;
57 const char* D;
58 constexpr string_view(const char* Str) : S(__builtin_strlen(Str)), D(Str) {}
59 constexpr string_view(int Size, const char* Str) : S(Size), D(Str) {}
60 constexpr int size() const {
61 return S;
63 constexpr const char* data() const {
64 return D;
68 constexpr string_view operator+(auto, string_view S) {
69 return S;
72 constexpr const char g_[] = "long string";
74 template <typename T, int S>
75 struct array {
76 constexpr unsigned long size() const {
77 return S;
79 constexpr const char* data() const {
80 return d_;
82 const char d_[S];
85 static_assert(false, string_view("test")); // expected-error {{static assertion failed: test}}
86 static_assert(false, "Literal" + string_view("test")); // expected-error {{static assertion failed: test}}
87 static_assert(false, L"Wide Literal" + string_view("test")); // expected-error {{static assertion failed: test}}
88 static_assert(false, "Wild" "Literal" "Concatenation" + string_view("test")); // expected-error {{static assertion failed: test}}
89 static_assert(false, "Wild" "Literal" L"Concatenation" + string_view("test")); // expected-error {{static assertion failed: test}}
90 static_assert(false, "Wild" u"Literal" L"Concatenation" + string_view("test")); // expected-error {{unsupported non-standard concatenation of string literals}}
91 static_assert(false, string_view("😀")); // expected-error {{static assertion failed: 😀}}
92 static_assert(false, string_view(0, nullptr)); // expected-error {{static assertion failed:}}
93 static_assert(false, string_view(1, "ABC")); // expected-error {{static assertion failed: A}}
94 static_assert(false, string_view(42, "ABC")); // expected-error {{static assertion failed: ABC}} \
95 // expected-error {{the message in a static assertion must be produced by a constant expression}} \
96 // expected-note {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
97 static_assert(false, array<char, 2>{'a', 'b'}); // expected-error {{static assertion failed: ab}}
101 struct ConvertibleToInt {
102 constexpr operator int() {
103 return 4;
106 struct ConvertibleToCharPtr {
107 constexpr operator const char*() {
108 return "test";
111 struct MessageFromConvertible {
112 constexpr ConvertibleToInt size() const {
113 return {};
115 constexpr ConvertibleToCharPtr data() const {
116 return {};
120 static_assert(true, MessageFromConvertible{});
121 static_assert(false, MessageFromConvertible{}); // expected-error{{static assertion failed: test}}
125 struct Leaks {
126 constexpr unsigned long size() const {
127 return 2;
129 constexpr const char* data() const {
130 return new char[2]{'u', 'b'}; // expected-note {{allocation performed here was not deallocated}}
134 static_assert(false, Leaks{}); //expected-error {{the message in a static assertion must be produced by a constant expression}} \
135 // expected-error {{static assertion failed: ub}}
137 struct RAII {
138 const char* d = new char[2]{'o', 'k'};
139 constexpr unsigned long size() const {
140 return 2;
143 constexpr const char* data() const {
144 return d;
147 constexpr ~RAII() {
148 delete[] d;
151 static_assert(false, RAII{}); // expected-error {{static assertion failed: ok}}
153 namespace MoreTemporary {
155 struct Data{
156 constexpr operator const char*() const {
157 return d;
159 char d[6] = { "Hello" };
162 struct Size {
163 constexpr operator int() const {
164 return 5;
168 struct Message {
169 constexpr auto size() const {
170 return Size{};
172 constexpr auto data() const {
173 return Data{};
177 static_assert(false, Message{}); // expected-error {{static assertion failed: Hello}}
181 struct MessageInvalidSize {
182 constexpr unsigned long size(int) const; // expected-note {{'size' declared here}}
183 constexpr const char* data() const;
185 struct MessageInvalidData {
186 constexpr unsigned long size() const;
187 constexpr const char* data(int) const; // expected-note {{'data' declared here}}
190 static_assert(false, MessageInvalidSize{}); // expected-error {{static assertion failed}} \
191 // expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}} \
192 // expected-error {{too few arguments to function call, expected 1, have 0}}
193 static_assert(false, MessageInvalidData{}); // expected-error {{static assertion failed}} \
194 // expected-error {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}} \
195 // expected-error {{too few arguments to function call, expected 1, have 0}}
197 struct NonConstMembers {
198 constexpr int size() {
199 return 1;
201 constexpr const char* data() {
202 return "A";
206 static_assert(false, NonConstMembers{}); // expected-error {{static assertion failed: A}}
208 struct DefaultArgs {
209 constexpr int size(int i = 0) {
210 return 2;
212 constexpr const char* data(int i =0, int j = 42) {
213 return "OK";
217 static_assert(false, DefaultArgs{}); // expected-error {{static assertion failed: OK}}
219 struct Variadic {
220 constexpr int size(auto...) {
221 return 2;
223 constexpr const char* data(auto...) {
224 return "OK";
228 static_assert(false, Variadic{}); // expected-error {{static assertion failed: OK}}
230 template <typename T>
231 struct DeleteAndRequires {
232 constexpr int size() = delete; // expected-note {{'size' has been explicitly marked deleted here}}
233 constexpr const char* data() requires false; // expected-note {{because 'false' evaluated to false}}
235 static_assert(false, DeleteAndRequires<void>{});
236 // expected-error@-1 {{static assertion failed}} \
237 // expected-error@-1 {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}}\
238 // expected-error@-1 {{invalid reference to function 'data': constraints not satisfied}} \
239 // expected-error@-1 {{attempt to use a deleted function}}
241 class Private {
242 constexpr int size(int i = 0) { // expected-note {{implicitly declared private here}}
243 return 2;
245 constexpr const char* data(int i =0, int j = 42) { // expected-note {{implicitly declared private here}}
246 return "OK";
250 static_assert(false, Private{}); // expected-error {{'data' is a private member of 'Private'}}\
251 // expected-error {{'size' is a private member of 'Private'}}\
252 // expected-error {{static assertion failed: OK}}
254 struct MessageOverload {
255 constexpr int size() {
256 return 1;
258 constexpr int size() const;
260 constexpr const char* data() {
261 return "A";
263 constexpr const char* data() const;
266 static_assert(false, MessageOverload{}); // expected-error {{static assertion failed: A}}
268 struct InvalidPtr {
269 consteval auto size() {
270 return 42;
272 consteval const char *data() {
273 const char *ptr; // Garbage
274 return ptr; // expected-note {{read of uninitialized object is not allowed in a constant expression}}
278 static_assert(false, InvalidPtr{}); // expected-error{{the message in a static assertion must be produced by a constant expression}} \
279 //expected-error {{static assertion failed}} \
280 // expected-note {{in call to 'InvalidPtr{}.data()'}}
282 namespace DependentMessage {
283 template <typename Ty>
284 struct Good {
285 static_assert(false, Ty{}); // expected-error {{static assertion failed: hello}}
288 template <typename Ty>
289 struct Bad {
290 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}} \
291 // expected-error {{static assertion failed}}
294 struct Frobble {
295 constexpr int size() const { return 5; }
296 constexpr const char *data() const { return "hello"; }
299 constexpr Frobble operator ""_myd (const char *, unsigned long) { return Frobble{}; }
300 static_assert (false, "foo"_myd); // expected-error {{static assertion failed: hello}}
302 Good<Frobble> a; // expected-note {{in instantiation}}
303 Bad<int> b; // expected-note {{in instantiation}}
307 namespace EscapeInDiagnostic {
308 static_assert('\u{9}' == (char)1, ""); // expected-error {{failed}} \
309 // expected-note {{evaluates to ''\t' (0x09, 9) == '<U+0001>' (0x01, 1)'}}
310 static_assert((char8_t)-128 == (char8_t)-123, ""); // expected-error {{failed}} \
311 // expected-note {{evaluates to 'u8'<80>' (0x80, 128) == u8'<85>' (0x85, 133)'}}
312 static_assert((char16_t)0xFEFF == (char16_t)0xDB93, ""); // expected-error {{failed}} \
313 // expected-note {{evaluates to 'u'' (0xFEFF, 65279) == u'\xDB93' (0xDB93, 56211)'}}
316 struct Static {
317 static constexpr int size() { return 5; }
318 static constexpr const char *data() { return "hello"; }
320 static_assert(false, Static{}); // expected-error {{static assertion failed: hello}}
322 struct Data {
323 unsigned long size = 0;
324 const char* data = "hello";
326 static_assert(false, Data{}); // expected-error {{called object type 'unsigned long' is not a function or function pointer}} \
327 // expected-error {{called object type 'const char *' is not a function or function pointer}} \
328 // expected-error {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}} \
329 // expected-error {{static assertion failed}}
331 struct Callable {
332 struct {
333 constexpr auto operator()() const {
334 return 5;
336 } size;
337 struct {
338 constexpr auto operator()() const {
339 return "hello";
341 } data;
343 static_assert(false, Callable{}); // expected-error {{static assertion failed: hello}}
345 namespace GH89407 {
346 struct A {
347 constexpr __SIZE_TYPE__ size() const { return -1; }
348 constexpr const char* data() const { return ""; }
351 struct B {
352 constexpr long long size() const { return 18446744073709551615U; }
353 constexpr const char* data() const { return ""; }
356 struct C {
357 constexpr __int128 size() const { return -1; }
358 constexpr const char* data() const { return ""; }
361 struct D {
362 constexpr unsigned __int128 size() const { return -1; }
363 constexpr const char* data() const { return ""; }
366 struct E {
367 constexpr __SIZE_TYPE__ size() const { return 18446744073709551615U; }
368 constexpr const char* data() const { return ""; }
371 static_assert(true, A{}); // expected-error {{the message in this static assertion is not a constant expression}}
372 // expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
373 static_assert(true, B{}); // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type 'unsigned long'}}
374 // expected-error@-1 {{the message in this static assertion is not a constant expression}}
375 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
376 static_assert(true, C{}); // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type 'unsigned long'}}
377 // expected-error@-1 {{the message in this static assertion is not a constant expression}}
378 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
379 static_assert(true, D{}); // expected-error {{call to 'size()' evaluates to 340282366920938463463374607431768211455, which cannot be narrowed to type 'unsigned long'}}
380 // expected-error@-1 {{the message in this static assertion is not a constant expression}}
381 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
382 static_assert(true, E{}); // expected-error {{the message in this static assertion is not a constant expression}}
383 // expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
385 static_assert(
386 false, // expected-error {{static assertion failed}}
387 A{} // expected-error {{the message in a static assertion must be produced by a constant expression}}
388 // expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
391 static_assert(
392 false, // expected-error {{static assertion failed}}
393 B{} // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type 'unsigned long'}}
394 // expected-error@-1 {{the message in a static assertion must be produced by a constant expression}}
395 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
398 static_assert(
399 false, // expected-error {{static assertion failed}}
400 C{} // expected-error {{call to 'size()' evaluates to -1, which cannot be narrowed to type 'unsigned long'}}
401 // expected-error@-1 {{the message in a static assertion must be produced by a constant expression}}
402 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
405 static_assert(
406 false, // expected-error {{static assertion failed}}
407 D{} // expected-error {{call to 'size()' evaluates to 340282366920938463463374607431768211455, which cannot be narrowed to type 'unsigned long'}}
408 // expected-error@-1 {{the message in a static assertion must be produced by a constant expression}}
409 // expected-note@-2 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}
412 static_assert(
413 false, // expected-error {{static assertion failed}}
414 E{} // expected-error {{the message in a static assertion must be produced by a constant expression}}
415 // expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}}