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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
// RUN: %clang_cc1 -std=c++2c -fcxx-exceptions -fexperimental-new-constant-interpreter -verify=expected,both %s -DBYTECODE
// RUN: %clang_cc1 -std=c++2c -fcxx-exceptions -verify=ref,both %s
typedef __INT64_TYPE__ int64_t;
namespace std {
using size_t = decltype(sizeof(0));
template<typename T> struct allocator {
constexpr T *allocate(size_t N) {
return (T*)operator new(sizeof(T) * N);
}
constexpr void deallocate(void *p) {
operator delete(p);
}
};
template<typename T, typename ...Args>
constexpr void construct_at(void *p, Args &&...args) {
new (p) T((Args&&)args...); // both-note {{in call to}} \
// both-note {{placement new would change type of storage from 'int' to 'float'}} \
// both-note {{construction of subobject of member 'x' of union with active member 'a' is not allowed in a constant expression}} \
// both-note {{construction of temporary is not allowed}} \
// both-note {{construction of heap allocated object that has been deleted}} \
// both-note {{construction of subobject of object outside its lifetime is not allowed in a constant expression}}
}
}
void *operator new(std::size_t, void *p) { return p; }
void* operator new[] (std::size_t, void* p) {return p;}
constexpr int no_lifetime_start = (*std::allocator<int>().allocate(1) = 1); // both-error {{constant expression}} \
// both-note {{assignment to object outside its lifetime}}
consteval auto ok1() {
bool b;
new (&b) bool(true);
return b;
}
static_assert(ok1());
consteval auto ok2() {
int b;
new (&b) int(12);
return b;
}
static_assert(ok2() == 12);
consteval auto ok3() {
float b;
new (&b) float(12.0);
return b;
}
static_assert(ok3() == 12.0);
consteval auto ok4() {
_BitInt(11) b;
new (&b) _BitInt(11)(37);
return b;
}
static_assert(ok4() == 37);
consteval int ok5() {
int i;
new (&i) int[1]{1};
struct S {
int a; int b;
} s;
new (&s) S[1]{{12, 13}};
return 25;
// return s.a + s.b; FIXME: Broken in the current interpreter.
}
static_assert(ok5() == 25);
/// FIXME: Broken in both interpreters.
#if 0
consteval int ok5() {
int i;
new (&i) int[1]{1}; // expected-note {{assignment to dereferenced one-past-the-end pointer}}
return i;
}
static_assert(ok5() == 1); // expected-error {{not an integral constant expression}} \
// expected-note {{in call to}}
#endif
/// FIXME: Crashes the current interpreter.
#if 0
consteval int ok6() {
int i[2];
new (&i) int(100);
return i[0];
}
static_assert(ok6() == 100);
#endif
consteval int ok6() {
int i[2];
new (i) int(100);
new (i + 1) int(200);
return i[0] + i[1];
}
static_assert(ok6() == 300);
consteval auto fail1() {
int b;
new (&b) float(1.0); // both-note {{placement new would change type of storage from 'int' to 'float'}}
return b;
}
static_assert(fail1() == 0); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}
consteval int fail2() {
int i;
new (static_cast<void*>(&i)) float(0); // both-note {{placement new would change type of storage from 'int' to 'float'}}
return 0;
}
static_assert(fail2() == 0); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}
consteval int indeterminate() {
int * indeterminate;
new (indeterminate) int(0); // both-note {{read of uninitialized object is not allowed in a constant expression}}
return 0;
}
static_assert(indeterminate() == 0); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}
consteval int array1() {
int i[2];
new (&i) int[]{1,2};
return i[0] + i[1];
}
static_assert(array1() == 3);
consteval int array2() {
int i[2];
new (static_cast<void*>(&i)) int[]{1,2};
return i[0] + i[1];
}
static_assert(array2() == 3);
consteval int array3() {
int i[1];
new (&i) int[2]; // both-note {{placement new would change type of storage from 'int[1]' to 'int[2]'}}
return 0;
}
static_assert(array3() == 0); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}
consteval int array4() {
int i[2];
new (&i) int[]{12};
return i[0];
}
static_assert(array4() == 12);
constexpr int *intptr() {
return new int;
}
constexpr bool yay() {
int *ptr = new (intptr()) int(42);
bool ret = *ptr == 42;
delete ptr;
return ret;
}
static_assert(yay());
constexpr bool blah() {
int *ptr = new (intptr()) int[3]{ 1, 2, 3 }; // both-note {{placement new would change type of storage from 'int' to 'int[3]'}}
bool ret = ptr[0] == 1 && ptr[1] == 2 && ptr[2] == 3;
delete [] ptr;
return ret;
}
static_assert(blah()); // both-error {{not an integral constant expression}} \
// both-note {{in call to 'blah()'}}
constexpr int *get_indeterminate() {
int *evil;
return evil; // both-note {{read of uninitialized object is not allowed in a constant expression}}
}
constexpr bool bleh() {
int *ptr = new (get_indeterminate()) int; // both-note {{in call to 'get_indeterminate()'}}
return true;
}
static_assert(bleh()); // both-error {{not an integral constant expression}} \
// both-note {{in call to 'bleh()'}}
namespace records {
class S {
public:
float f;
};
constexpr bool record1() {
S s(13);
new (&s) S(42);
return s.f == 42;
}
static_assert(record1());
S GlobalS;
constexpr bool record2() {
new (&GlobalS) S(42); // both-note {{a constant expression cannot modify an object that is visible outside that expression}}
return GlobalS.f == 42;
}
static_assert(record2()); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}
constexpr bool record3() {
S ss[3];
new (&ss) S[]{{1}, {2}, {3}};
return ss[0].f == 1 && ss[1].f == 2 && ss[2].f == 3;
}
static_assert(record3());
struct F {
float f;
};
struct R {
F f;
int a;
};
constexpr bool record4() {
R r;
new (&r.f) F{42.0};
new (&r.a) int(12);
return r.f.f == 42.0 && r.a == 12;
}
static_assert(record4());
/// Destructor is NOT called.
struct A {
bool b;
constexpr ~A() { if (b) throw; }
};
constexpr int foo() {
A a;
new (&a) A(true);
new (&a) A(false);
return 0;
}
static_assert(foo() == 0);
}
namespace ConstructAt {
struct S {
int a = 10;
float b = 1.0;
};
constexpr bool ok1() {
S s;
std::construct_at<S>(&s);
return s.a == 10 && s.b == 1.0;
}
static_assert(ok1());
struct S2 {
constexpr S2() {
(void)(1/0); // both-note {{division by zero}} \
// both-warning {{division by zero is undefined}}
}
};
constexpr bool ctorFail() { //
S2 *s = std::allocator<S2>().allocate(1);
std::construct_at<S2>(s); // both-note {{in call to}}
return true;
}
static_assert(ctorFail()); // both-error {{not an integral constant expression}} \
// both-note {{in call to 'ctorFail()'}}
constexpr bool bad_construct_at_type() {
int a;
std::construct_at<float>(&a, 1.0f); // both-note {{in call to}}
return true;
}
static_assert(bad_construct_at_type()); // both-error {{not an integral constant expression}} \
// both-note {{in call}}
constexpr bool bad_construct_at_subobject() {
struct X { int a, b; };
union A {
int a;
X x;
};
A a = {1};
std::construct_at<int>(&a.x.a, 1); // both-note {{in call}}
return true;
}
static_assert(bad_construct_at_subobject()); // both-error{{not an integral constant expression}} \
// both-note {{in call}}
}
namespace UsedToCrash {
struct S {
int* i;
constexpr S() : i(new int(42)) {} // #no-deallocation
constexpr ~S() {delete i;}
};
consteval void alloc() {
S* s = new S();
s->~S();
new (s) S();
delete s;
}
int alloc1 = (alloc(), 0);
}
constexpr bool change_union_member() {
union U {
int a;
int b;
};
U u = {.a = 1};
std::construct_at<int>(&u.b, 2);
return u.b == 2;
}
static_assert(change_union_member());
namespace PR48606 {
struct A { mutable int n = 0; };
constexpr bool f() {
A a;
A *p = &a;
p->~A();
std::construct_at<A>(p);
return true;
}
static_assert(f());
}
/// This used to crash because of an assertion in the implementation
/// of the This instruction.
namespace ExplicitThisOnArrayElement {
struct S {
int a = 12;
constexpr S(int a) {
this->a = a;
}
};
template <class _Tp, class... _Args>
constexpr void construct_at(_Tp *__location, _Args &&...__args) {
new (__location) _Tp(__args...);
}
constexpr bool foo() {
auto *M = std::allocator<S>().allocate(13); // both-note {{allocation performed here was not deallocated}}
construct_at(M, 12);
return true;
}
static_assert(foo()); // both-error {{not an integral constant expression}}
}
#ifdef BYTECODE
constexpr int N = [] // expected-error {{must be initialized by a constant expression}} \
// expected-note {{assignment to dereferenced one-past-the-end pointer is not allowed in a constant expression}} \
// expected-note {{in call to}}
{
struct S {
int a[1];
};
S s;
::new (s.a) int[1][2][3][4]();
return s.a[0];
}();
#endif
namespace MemMove {
constexpr int foo() {
int *a = std::allocator<int>{}.allocate(1);
new(a) int{123};
int b;
__builtin_memmove(&b, a, sizeof(int));
std::allocator<int>{}.deallocate(a);
return b;
}
static_assert(foo() == 123);
}
namespace Temp {
constexpr int &&temporary = 0; // both-note {{created here}}
static_assert((std::construct_at<int>(&temporary, 1), true)); // both-error{{not an integral constant expression}} \
// both-note {{in call}}
}
namespace PlacementNewAfterDelete {
constexpr bool construct_after_lifetime() {
int *p = new int;
delete p;
std::construct_at<int>(p); // both-note {{in call}}
return true;
}
static_assert(construct_after_lifetime()); // both-error {{}} \
// both-note {{in call}}
}
namespace SubObj {
constexpr bool construct_after_lifetime_2() {
struct A { struct B {} b; };
A a;
a.~A();
std::construct_at<A::B>(&a.b); // both-note {{in call}}
return true;
}
static_assert(construct_after_lifetime_2()); // both-error {{}} both-note {{in call}}
}
namespace RecursiveLifetimeStart {
struct B {
int b;
};
struct A {
B b;
int a;
};
constexpr int foo() {
A a;
a.~A();
new (&a) A();
a.a = 10;
a.b.b = 12;
return a.a;
}
static_assert(foo() == 10);
}
namespace ArrayRoot {
struct S {
int a;
};
constexpr int foo() {
S* ss = std::allocator<S>().allocate(2);
new (ss) S{};
new (ss + 1) S{};
S* ps = &ss[2];
ps = ss;
ps->~S();
std::allocator<S>().deallocate(ss);
return 0;
}
static_assert(foo() == 0);
}
namespace bitcast {
template <typename F, typename T>
constexpr T bit_cast(const F &f) {
return __builtin_bit_cast(T, f);
}
constexpr int foo() {
double *d = std::allocator<double>{}.allocate(2);
std::construct_at<double>(d, 0);
double &dd = *d;
int64_t i = bit_cast<double, int64_t>(*d);
std::allocator<double>{}.deallocate(d);
return i;
}
static_assert(foo() == 0);
}
constexpr int modify_const_variable() {
const int a = 10;
new ((int *)&a) int(12); // both-note {{modification of object of const-qualified type 'const int' is not allowed in a constant expression}}
return a;
}
static_assert(modify_const_variable()); // both-error {{not an integral constant expression}} \
// both-note {{in call to}}
|