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