Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / static-assert-cxx26.cpp
blobb19aac6cabd1324210d11473ad742ff673186ff2
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 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() {
197 return 1;
199 constexpr const char* data() {
200 return "A";
204 static_assert(false, NonConstMembers{}); // expected-error {{static assertion failed: A}}
206 struct DefaultArgs {
207 constexpr int size(int i = 0) {
208 return 2;
210 constexpr const char* data(int i =0, int j = 42) {
211 return "OK";
215 static_assert(false, DefaultArgs{}); // expected-error {{static assertion failed: OK}}
217 struct Variadic {
218 constexpr int size(auto...) {
219 return 2;
221 constexpr const char* data(auto...) {
222 return "OK";
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 *'}}
239 class Private {
240 constexpr int size(int i = 0) { // expected-note {{implicitly declared private here}}
241 return 2;
243 constexpr const char* data(int i =0, int j = 42) { // expected-note {{implicitly declared private here}}
244 return "OK";
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() {
254 return 1;
256 constexpr int size() const;
258 constexpr const char* data() {
259 return "A";
261 constexpr const char* data() const;
264 static_assert(false, MessageOverload{}); // expected-error {{static assertion failed: A}}
266 struct InvalidPtr {
267 consteval auto size() {
268 return 42;
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>
282 struct Good {
283 static_assert(false, Ty{}); // expected-error {{static assertion failed: hello}}
286 template <typename Ty>
287 struct Bad {
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}}
292 struct Frobble {
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)'}}