Debian Codegraph @

gcc/testsuite/g++.dg/cpp26/static_assert1.C

raw
1 // C++26 P2741R3 - user-generated static_assert messages
2 // { dg-do compile { target c++11 } }
3 // { dg-options "" }
4 // Override any default-'-fno-exceptions':
5 // { dg-additional-options -fexceptions }
6
7 static_assert (true, "");
8 static_assert (true, ("")); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
9 // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" "" { target *-*-* } .-1 }
10 // { dg-error "request for member 'size' in '\\\(\\\"\\\"\\\)', which is of non-class type 'const char \\\[1\\\]'" "" { target *-*-* } .-2 }
11 static_assert (true, "" + 0); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
12 // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" "" { target *-*-* } .-1 }
13 // { dg-error "request for member 'size' in '\\\(const char\\\*\\\)\\\"\\\"', which is of non-class type 'const char\\\*'" "" { target *-*-* } .-2 }
14 static_assert (true, 0); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
15 // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" "" { target *-*-* } .-1 }
16 // { dg-error "request for member 'size' in '0', which is of non-class type 'int'" "" { target *-*-* } .-2 }
17 struct A {};
18 static_assert (true, A {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
19 // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" "" { target *-*-* } .-1 }
20 // { dg-error "'struct A' has no member named 'size'" "" { target *-*-* } .-2 }
21 struct B { int size; };
22 static_assert (true, B {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
23 // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" "" { target *-*-* } .-1 }
24 // { dg-error "'struct B' has no member named 'data'" "" { target *-*-* } .-2 }
25 struct C { constexpr int size () const { return 0; } };
26 static_assert (true, C {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
27 // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" "" { target *-*-* } .-1 }
28 // { dg-error "'struct C' has no member named 'data'" "" { target *-*-* } .-2 }
29 struct D { constexpr int size () const { return 0; } int data; };
30 static_assert (true, D {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
31 // { dg-error "'D\\\(\\\).D::data' cannot be used as a function" "" { target *-*-* } .-1 }
32 struct E { int size = 0;
33 constexpr const char *data () const { return ""; } };
34 static_assert (true, E {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
35 // { dg-error "'E\\\(\\\).E::size' cannot be used as a function" "" { target c++11_only } .-1 }
36 // { dg-error "'E\\\{0\\\}.E::size' cannot be used as a function" "" { target c++14 } .-2 }
37 struct F { constexpr const char *size () const { return ""; }
38 constexpr const char *data () const { return ""; } };
39 static_assert (true, F {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
40 // { dg-error "constexpr string 'size\\\(\\\)' must be implicitly convertible to 'std::size_t'" "" { target *-*-* } .-1 }
41 // { dg-error "could not convert 'F\\\(\\\).F::size\\\(\\\)' from 'const char\\\*' to '\[^']*'" "" { target *-*-* } .-2 }
42 // { dg-error "conversion from 'const char\\\*' to '\[^']*' in a converted constant expression" "" { target *-*-* } .-3 }
43 struct G { constexpr long size () const { return 0; }
44 constexpr float data () const { return 0.0f; } };
45 static_assert (true, G {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
46 // { dg-error "constexpr string 'data\\\(\\\)' must be implicitly convertible to 'const char\\\*'" "" { target *-*-* } .-1 }
47 // { dg-error "could not convert 'G\\\(\\\).G::data\\\(\\\)' from 'float' to 'const char\\\*'" "" { target *-*-* } .-2 }
48 struct H { short size () const { return 0; }
49 constexpr const char *data () const { return ""; } };
50 static_assert (true, H {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
51 struct I { constexpr signed char size () const { return 0; }
52 const char *data () const { return ""; } };
53 static_assert (true, I {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
54 struct J { constexpr int size () const { return j ? throw 1 : 0; } // { dg-error "expression '<throw-expression>' is not a constant expression" }
55 constexpr const char *data () const { return ""; };
56 constexpr J (int x) : j (x) {}
57 int j; };
58 static_assert (true, J (1)); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
59 static_assert (false, J (0)); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
60 // { dg-error "static assertion failed" "" { target *-*-* } .-1 }
61 static_assert (false, J (1)); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
62 // { dg-error "constexpr string 'size\\\(\\\)' must be a constant expression" "" { target *-*-* } .-1 }
63 struct K { constexpr operator int () { return 4; } };
64 struct L { constexpr operator const char * () { return "test"; } };
65 struct M { constexpr K size () const { return {}; }
66 constexpr L data () const { return {}; } };
67 static_assert (true, M {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
68 static_assert (false, M {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
69 // { dg-error "static assertion failed: test" "" { target *-*-* } .-1 }
70 #if __cpp_constexpr_dynamic_alloc >= 201907L
71 struct N { constexpr int size () const { return 3; }
72 constexpr const char *data () const { return new char[3] { 'b', 'a', 'd' }; } }; // { dg-error "'\\\* N\\\(\\\).N::data\\\(\\\)' is not a constant expression because allocated storage has not been deallocated" "" { target c++20 } }
73 static_assert (true, N {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target { c++20 && c++23_down } } }
74 static_assert (false, N {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target { c++20 && c++23_down } } }
75 // { dg-error "constexpr string 'data\\\(\\\)\\\[0\\\]' must be a constant expression" "" { target c++20 } .-1 }
76 #endif
77 constexpr const char a[] = { 't', 'e', 's', 't' };
78 struct O { constexpr int size () const { return 4; }
79 constexpr const char *data () const { return a; } };
80 static_assert (false, O {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
81 // { dg-error "static assertion failed: test" "" { target *-*-* } .-1 }
82 struct P { constexpr int size () const { return 4 - p; }
83 constexpr const char *data () const { return &a[p]; }
84 constexpr P (int x) : p (x) {}
85 int p; };
86 static_assert (false, P (0)); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
87 // { dg-error "static assertion failed: test" "" { target *-*-* } .-1 }
88 static_assert (false, P (2)); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
89 // { dg-error "static assertion failed: st" "" { target *-*-* } .-1 }
90 struct Q { constexpr int size () const { return 4 - q; }
91 constexpr const char *data () const { return &"test"[q]; }
92 constexpr Q (int x) : q (x) {}
93 int q; };
94 static_assert (false, Q (0)); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
95 // { dg-error "static assertion failed: test" "" { target *-*-* } .-1 }
96 static_assert (false, Q (1)); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
97 // { dg-error "static assertion failed: est" "" { target *-*-* } .-1 }
98 struct R { constexpr int size () const { return 4 - r; }
99 constexpr const char *d () const { return "test"; }
100 constexpr const char *data () const { return d () + r; }
101 constexpr R (int x) : r (x) {}
102 int r; };
103 static_assert (false, R (0)); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
104 // { dg-error "static assertion failed: test" "" { target *-*-* } .-1 }
105 static_assert (false, R (2)); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
106 // { dg-error "static assertion failed: st" "" { target *-*-* } .-1 }
107 struct S { constexpr float size (float) const { return 42.0f; }
108 constexpr int size (void * = nullptr) const { return 4; }
109 constexpr double data (double) const { return 42.0; }
110 constexpr const char *data (int = 0) const { return "test"; } };
111 static_assert (true, S {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
112 static_assert (false, S {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
113 // { dg-error "static assertion failed: test" "" { target *-*-* } .-1 }
114
115 using size_t = decltype (sizeof (0));
116 struct string_view {
117 size_t s;
118 const char *d;
119 constexpr string_view () : s (0), d (nullptr) {}
120 constexpr string_view (const char *p) : s (__builtin_strlen (p)), d (p) {}
121 constexpr string_view (size_t l, const char *p) : s (l), d (p) {}
122 constexpr size_t size () const noexcept { return s; }
123 constexpr const char *data () const noexcept { return d; }
124 };
125 static_assert (true, string_view{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
126 static_assert (false, string_view ("test")); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
127 // { dg-error "static assertion failed: test" "" { target *-*-* } .-1 }
128 static_assert (false, string_view ("א")); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
129 // { dg-error "static assertion failed: א" "" { target *-*-* } .-1 }
130 static_assert (false, string_view (0, nullptr)); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
131 // { dg-error "static assertion failed" "" { target *-*-* } .-1 }
132 static_assert (false, string_view (4, "testwithextrachars")); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
133 // { dg-error "static assertion failed: test" "" { target *-*-* } .-1 }
134 static_assert (false, string_view (42, "test")); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
135 // { dg-error "array subscript value '41' is outside the bounds of array type 'const char \\\[5\\\]'" "" { target *-*-* } .-1 }
136 // { dg-error "constexpr string 'data\\\(\\\)\\\[41\\\]' must be a constant expression" "" { target *-*-* } .-2 }
137
138 template <typename T, size_t N>
139 struct array {
140 constexpr size_t size () const { return N; }
141 constexpr const T *data () const { return a; }
142 const T a[N];
143 };
144 static_assert (true, array<char, 2> { 'O', 'K' }); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
145 static_assert (true, array<wchar_t, 2> { L'O', L'K' }); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
146 // { dg-error "constexpr string 'data\\\(\\\)' must be implicitly convertible to 'const char\\\*'" "" { target *-*-* } .-1 }
147 // { dg-error "could not convert 'array<wchar_t, 2>{const wchar_t \\\[2\\\]{\[0-9]+, \[0-9]+}}.array<wchar_t, 2>::data\\\(\\\)' from 'const wchar_t\\\*' to 'const char\\\*'" "" { target *-*-* } .-2 }
148 static_assert (false, array<char, 4> { 't', 'e', 's', 't' }); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
149 // { dg-error "static assertion failed: test" "" { target *-*-* } .-1 }
150
151 void
152 foo ()
153 {
154 constexpr auto a = array<char, 4> { 't', 'e', 's', 't' };
155 static_assert (false, a); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
156 } // { dg-error "static assertion failed: test" "" { target *-*-* } .-1 }
157
158 #if __cpp_constexpr_dynamic_alloc >= 201907L
159 struct T {
160 const char *d = init ();
161 constexpr int size () const { return 4; }
162 constexpr const char *data () const { return d; }
163 constexpr const char *init () const { return new char[4] { 't', 'e', 's', 't' }; }
164 constexpr ~T () { delete[] d; }
165 };
166 static_assert (false, T{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target { c++20 && c++23_down } } }
167 // { dg-error "static assertion failed: test" "" { target c++20 } .-1 }
168 #endif
169 struct U { constexpr operator const char * () const { return u; }
170 char u[5] = "test"; };
171 #if __cplusplus >= 201402L
172 struct V { constexpr auto size () const { return K{}; }
173 constexpr auto data () const { return U{}; } };
174 static_assert (false, V{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target { c++14 && c++23_down } } }
175 // { dg-error "static assertion failed: test" "" { target c++14 } .-1 }
176 #endif
177 struct W { constexpr int size (int) const { return 4; }
178 constexpr const char *data () const { return "test"; } };
179 static_assert (true, W{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
180 // { dg-error "no matching function for call to 'W::size\\\(\\\)'" "" { target *-*-* } .-1 }
181 struct X { constexpr int size () const { return 4; }
182 constexpr const char *data (int) const { return "test"; } };
183 static_assert (true, X{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
184 // { dg-error "no matching function for call to 'X::data\\\(\\\)'" "" { target *-*-* } .-1 }
185 struct Y { constexpr int size () { return 4; }
186 constexpr const char *data (int) { return "test"; } };
187 static_assert (true, Y{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
188 // { dg-error "no matching function for call to 'Y::data\\\(\\\)'" "" { target *-*-* } .-1 }
189 #if __cpp_concepts >= 201907L
190 struct Z { constexpr int size (auto...) const { return 4; }
191 constexpr const char *data (auto...) const { return "test"; } };
192 static_assert (false, Z{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target { c++20 && c++23_down } } }
193 // { dg-error "static assertion failed: test" "" { target c++20 } .-1 }
194 #endif
195
196 namespace NN
197 {
198 template <typename T>
199 struct A {
200 constexpr int size () const = delete;
201 constexpr const char *data () const { return "test"; } };
202 static_assert (true, A<int>{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
203 // { dg-error "use of deleted function 'constexpr int NN::A<T>::size\\\(\\\) const \\\[with T = int\\\]'" "" { target *-*-* } .-1 }
204 #if __cpp_concepts >= 201907L
205 template <typename T>
206 struct B {
207 constexpr int size () const { return 4; }
208 constexpr const char *data () const requires false { return "test"; } };
209 static_assert (true, B<short>{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target { c++20 && c++23_down } } }
210 // { dg-error "no matching function for call to 'NN::B<short int>::data\\\(\\\)'" "" { target c++20 } .-1 }
211 #endif
212 class C {
213 constexpr int size () const = delete;
214 constexpr const char *data () const { return "test"; } };
215 static_assert (true, C{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
216 // { dg-error "use of deleted function 'constexpr int NN::C::size\\\(\\\) const'" "" { target *-*-* } .-1 }
217 // { dg-error "'constexpr const char\\\* NN::C::data\\\(\\\) const' is private within this context" "" { target *-*-* } .-2 }
218 #if __cplusplus >= 201402L
219 struct D {
220 constexpr int size () { return 4; }
221 constexpr int size () const { return 3; }
222 constexpr const char *data () { return "test"; }
223 constexpr const char *data () const { return "ehlo"; } };
224 static_assert (true, D{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target { c++14 && c++23_down } } }
225 static_assert (false, D{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target { c++14 && c++23_down } } }
226 // { dg-error "static assertion failed: test" "" { target c++14 } .-1 }
227 #endif
228 struct E {
229 constexpr int size () const { return 4; }
230 constexpr const char *data () const { return "test"; } };
231 template <typename T>
232 struct F {
233 static_assert (false, T{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
234 }; // { dg-error "static assertion failed: test" "" { target *-*-* } .-1 }
235 template <typename T>
236 struct G {
237 static_assert (false, T{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
238 }; // { dg-error "constexpr string must be a string literal or object with 'size' and 'data' members" "" { target *-*-* } .-1 }
239 // { dg-error "request for member 'size' in '0', which is of non-class type 'long int'" "" { target *-*-* } .-2 }
240 F<E> fe;
241 G<long> gl;
242 constexpr E operator ""_myd (const char *, size_t) { return E{}; }
243 static_assert (false, "foo"_myd); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
244 // { dg-error "static assertion failed: test" "" { target *-*-* } .-1 }
245 constexpr E operator + (const char *, const E &) { return E{}; }
246 static_assert (false, "foo" + E{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
247 // { dg-error "static assertion failed: test" "" { target *-*-* } .-1 }
248 struct H {
249 static constexpr int size () { return 7; }
250 static constexpr const char *data () { return "message"; } };
251 static_assert (true, H{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
252 static_assert (false, H{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
253 // { dg-error "static assertion failed: message" "" { target *-*-* } .-1 }
254 struct I {
255 static constexpr int size () { return 0; }
256 static constexpr const char *data () { return nullptr; } };
257 static_assert (true, I{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
258 static_assert (false, I{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
259 // { dg-error "static assertion failed" "" { target *-*-* } .-1 }
260 #if __cplusplus >= 201402L
261 struct J {
262 static constexpr int size () { return 0; }
263 static constexpr const char *data (int x = 0) { if (x) return nullptr; else throw 1; } }; // { dg-error "expression '<throw-expression>' is not a constant expression" "" { target c++14 } }
264 static_assert (true, J{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target { c++14 && c++23_down } } }
265 static_assert (false, J{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target { c++14 && c++23_down } } }
266 // { dg-error "constexpr string 'data\\\(\\\)' must be a core constant expression" "" { target c++14 } .-1 }
267 #endif
268 #if __cpp_if_consteval >= 202106L
269 struct K {
270 static constexpr int size () { if consteval { return 4; } else { throw 1; } }
271 static constexpr const char *data () { return "test"; }
272 };
273 static_assert (true, K{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_only } }
274 static_assert (false, K{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_only } }
275 // { dg-error "static assertion failed: test" "" { target c++23 } .-1 }
276 struct L {
277 static constexpr int size () { return 4; }
278 static constexpr const char *data () { if consteval { return "test"; } else { throw 1; } }
279 };
280 static_assert (true, L{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_only } }
281 static_assert (false, L{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_only } }
282 // { dg-error "static assertion failed: test" "" { target c++23 } .-1 }
283 struct M {
284 static constexpr int size () { if consteval { throw 1; } else { return 4; } } // { dg-error "expression '<throw-expression>' is not a constant expression" "" { target c++23 } }
285 static constexpr const char *data () { return "test"; }
286 };
287 static_assert (true, M{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_only } }
288 static_assert (false, M{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_only } }
289 // { dg-error "constexpr string 'size\\\(\\\)' must be a constant expression" "" { target c++23 } .-1 }
290 struct N {
291 static constexpr int size () { return 4; }
292 static constexpr const char *data () { if consteval { throw 1; } else { return "test"; } } // { dg-error "expression '<throw-expression>' is not a constant expression" "" { target c++23 } }
293 };
294 static_assert (true, N{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_only } }
295 static_assert (false, N{}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_only } }
296 // { dg-error "constexpr string 'data\\\(\\\)\\\[0\\\]' must be a constant expression" "" { target c++23 } .-1 }
297 #endif
298 struct O { constexpr int operator () () const { return 12; } };
299 struct P { constexpr const char *operator () () const { return "another test"; } };
300 struct Q { O size; P data; };
301 static_assert (true, Q ()); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
302 static_assert (false, Q {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
303 // { dg-error "static assertion failed: another test" "" { target *-*-* } .-1 }
304 constexpr int get_size () { return 16; }
305 constexpr const char *get_data () { return "yet another test"; }
306 struct R { int (*size) () = NN::get_size;
307 const char *(*data) () = NN::get_data; };
308 static_assert (true, R ()); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
309 static_assert (false, R {}); // { dg-warning "'static_assert' with non-string message only available with" "" { target c++23_down } }
310 // { dg-error "static assertion failed: yet another test" "" { target *-*-* } .-1 }
311 }