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 auto size(int) const; // expected-note {{candidate function not viable: requires 1 argument, but 0 were provided}}
183 constexpr auto data() const;
185 struct MessageInvalidData
{
186 constexpr auto size() const;
187 constexpr auto data(int) const; // expected-note {{candidate function not viable: requires 1 argument, but 0 were provided}}
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 static_assert(false, MessageInvalidData
{}); // expected-error {{static assertion failed}} \
193 // expected-error {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}}
195 struct NonConstMembers
{
196 constexpr int size() {
199 constexpr const char* data() {
204 static_assert(false, NonConstMembers
{}); // expected-error {{static assertion failed: A}}
207 constexpr int size(int i
= 0) {
210 constexpr const char* data(int i
=0, int j
= 42) {
215 static_assert(false, DefaultArgs
{}); // expected-error {{static assertion failed: OK}}
218 constexpr int size(auto...) {
221 constexpr const char* data(auto...) {
226 static_assert(false, Variadic
{}); // expected-error {{static assertion failed: OK}}
228 template <typename T
>
229 struct DeleteAndRequires
{
230 constexpr int size() = delete; // expected-note {{candidate function has been explicitly deleted}}
231 constexpr const char* data() requires
false; // expected-note {{candidate function not viable: constraints not satisfied}} \
232 // expected-note {{because 'false' evaluated to false}}
234 static_assert(false, DeleteAndRequires
<void>{});
235 // expected-error@-1 {{static assertion failed}} \
236 // expected-error@-1 {{the message in a static assertion must have a 'size()' member function returning an object convertible to 'std::size_t'}}\
237 // expected-error@-1 {{the message in a static assertion must have a 'data()' member function returning an object convertible to 'const char *'}}
240 constexpr int size(int i
= 0) { // expected-note {{implicitly declared private here}}
243 constexpr const char* data(int i
=0, int j
= 42) { // expected-note {{implicitly declared private here}}
248 static_assert(false, Private
{}); // expected-error {{'data' is a private member of 'Private'}}\
249 // expected-error {{'size' is a private member of 'Private'}}\
250 // expected-error {{static assertion failed: OK}}
252 struct MessageOverload
{
253 constexpr int size() {
256 constexpr int size() const;
258 constexpr const char* data() {
261 constexpr const char* data() const;
264 static_assert(false, MessageOverload
{}); // expected-error {{static assertion failed: A}}
267 consteval
auto size() {
270 consteval
const char *data() {
271 const char *ptr
; // Garbage
272 return ptr
; // expected-note {{read of uninitialized object is not allowed in a constant expression}}
276 static_assert(false, InvalidPtr
{}); // expected-error{{the message in a static assertion must be produced by a constant expression}} \
277 //expected-error {{static assertion failed}} \
278 // expected-note {{in call to 'InvalidPtr{}.data()'}}
280 namespace DependentMessage
{
281 template <typename Ty
>
283 static_assert(false, Ty
{}); // expected-error {{static assertion failed: hello}}
286 template <typename Ty
>
288 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}} \
289 // expected-error {{static assertion failed}}
293 constexpr int size() const { return 5; }
294 constexpr const char *data() const { return "hello"; }
297 Good
<Frobble
> a
; // expected-note {{in instantiation}}
298 Bad
<int> b
; // expected-note {{in instantiation}}
302 namespace EscapeInDiagnostic
{
303 static_assert('\u{9}' == (char)1, ""); // expected-error {{failed}} \
304 // expected-note {{evaluates to ''\t' (0x09, 9) == '<U+0001>' (0x01, 1)'}}
305 static_assert((char8_t
)-128 == (char8_t
)-123, ""); // expected-error {{failed}} \
306 // expected-note {{evaluates to 'u8'<80>' (0x80, 128) == u8'<85>' (0x85, 133)'}}
307 static_assert((char16_t
)0xFEFF == (char16_t
)0xDB93, ""); // expected-error {{failed}} \
308 // expected-note {{evaluates to 'u'' (0xFEFF, 65279) == u'\xDB93' (0xDB93, 56211)'}}