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}}
6 static_assert(true, Empty
{}); // expected-error {{the message object in this static assertion is missing 'data()' and 'size()' member functions}}
8 unsigned long size() const;
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}}
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'}}
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 {
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}}
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 {
63 constexpr const char* data() const {
68 constexpr string_view
operator+(auto, string_view S
) {
72 constexpr const char g_
[] = "long string";
74 template <typename T
, int S
>
76 constexpr unsigned long size() const {
79 constexpr const char* data() const {
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() {
106 struct ConvertibleToCharPtr
{
107 constexpr operator const char*() {
111 struct MessageFromConvertible
{
112 constexpr ConvertibleToInt
size() const {
115 constexpr ConvertibleToCharPtr
data() const {
120 static_assert(true, MessageFromConvertible
{});
121 static_assert(false, MessageFromConvertible
{}); // expected-error{{static assertion failed: test}}
126 constexpr unsigned long size() const {
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}}
138 const char* d
= new char[2]{'o', 'k'};
139 constexpr unsigned long size() const {
143 constexpr const char* data() const {
151 static_assert(false, RAII
{}); // expected-error {{static assertion failed: ok}}
153 namespace MoreTemporary
{
156 constexpr operator const char*() const {
159 char d
[6] = { "Hello" };
163 constexpr operator int() const {
169 constexpr auto size() const {
172 constexpr auto data() const {
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() {
201 constexpr const char* data() {
206 static_assert(false, NonConstMembers
{}); // expected-error {{static assertion failed: A}}
209 constexpr int size(int i
= 0) {
212 constexpr const char* data(int i
=0, int j
= 42) {
217 static_assert(false, DefaultArgs
{}); // expected-error {{static assertion failed: OK}}
220 constexpr int size(auto...) {
223 constexpr const char* data(auto...) {
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}}
242 constexpr int size(int i
= 0) { // expected-note {{implicitly declared private here}}
245 constexpr const char* data(int i
=0, int j
= 42) { // expected-note {{implicitly declared private here}}
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() {
258 constexpr int size() const;
260 constexpr const char* data() {
263 constexpr const char* data() const;
266 static_assert(false, MessageOverload
{}); // expected-error {{static assertion failed: A}}
269 consteval
auto size() {
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
>
285 static_assert(false, Ty
{}); // expected-error {{static assertion failed: hello}}
288 template <typename Ty
>
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}}
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)'}}
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}}
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}}
333 constexpr auto operator()() const {
338 constexpr auto operator()() const {
343 static_assert(false, Callable
{}); // expected-error {{static assertion failed: hello}}
347 constexpr __SIZE_TYPE__
size() const { return -1; }
348 constexpr const char* data() const { return ""; }
352 constexpr long long size() const { return 18446744073709551615U; }
353 constexpr const char* data() const { return ""; }
357 constexpr __int128
size() const { return -1; }
358 constexpr const char* data() const { return ""; }
362 constexpr unsigned __int128
size() const { return -1; }
363 constexpr const char* data() const { return ""; }
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}}
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}}
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}}
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}}
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}}
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}}