Debian Codegraph @

clang/test/AST/ByteCode/new-delete.cpp

raw
1 // RUN: %clang_cc1 -verify=expected,both -fexperimental-new-constant-interpreter %s
2 // RUN: %clang_cc1 -std=c++20 -verify=expected,both -fexperimental-new-constant-interpreter %s
3 // RUN: %clang_cc1 -std=c++20 -verify=expected,both -triple=i686-linux-gnu -fexperimental-new-constant-interpreter %s
4 // RUN: %clang_cc1 -verify=ref,both %s
5 // RUN: %clang_cc1 -std=c++20 -verify=ref,both %s
6 // RUN: %clang_cc1 -std=c++20 -verify=ref,both -triple=i686-linux-gnu %s
7
8 #if __cplusplus >= 202002L
9
10 constexpr int *Global = new int(12); // both-error {{must be initialized by a constant expression}} \
11 // both-note {{pointer to heap-allocated object}} \
12 // both-note {{heap allocation performed here}}
13
14 static_assert(*(new int(12)) == 12); // both-error {{not an integral constant expression}} \
15 // both-note {{allocation performed here was not deallocated}}
16
17 static_assert((delete[] (new int[3] + 1), true)); // both-error {{not an integral constant expression}} \
18 // both-note {{delete of pointer to subobject '&{*new int[3]#0}[1]'}}
19
20
21 constexpr int a() {
22 new int(12); // both-note {{allocation performed here was not deallocated}}
23 return 1;
24 }
25 static_assert(a() == 1, ""); // both-error {{not an integral constant expression}}
26
27 constexpr int b() {
28 int *i = new int(12);
29 int m = *i;
30 delete(i);
31 return m;
32 }
33 static_assert(b() == 12, "");
34
35
36 struct S {
37 int a;
38 int b;
39
40 static constexpr S *create(int a, int b) {
41 return new S(a, b);
42 }
43 };
44
45 constexpr int c() {
46 S *s = new S(12, 13);
47
48 int i = s->a;
49 delete s;
50
51 return i;
52 }
53 static_assert(c() == 12, "");
54
55 /// Dynamic allocation in function ::create(), freed in function d().
56 constexpr int d() {
57 S* s = S::create(12, 14);
58
59 int sum = s->a + s->b;
60 delete s;
61 return sum;
62 }
63 static_assert(d() == 26);
64
65
66 /// Test we emit the right diagnostic for several allocations done on
67 /// the same site.
68 constexpr int loop() {
69 for (int i = 0; i < 10; ++i) {
70 int *a = new int[10]; // both-note {{not deallocated (along with 9 other memory leaks)}}
71 }
72
73 return 1;
74 }
75 static_assert(loop() == 1, ""); // both-error {{not an integral constant expression}}
76
77 /// No initializer.
78 constexpr int noInit() {
79 int *i = new int;
80 delete i;
81 return 0;
82 }
83 static_assert(noInit() == 0, "");
84
85 /// Try to delete a pointer that hasn't been heap allocated.
86 constexpr int notHeapAllocated() { // both-error {{never produces a constant expression}}
87 int A = 0; // both-note 2{{declared here}}
88 delete &A; // both-note 2{{delete of pointer '&A' that does not point to a heap-allocated object}}
89
90 return 1;
91 }
92 static_assert(notHeapAllocated() == 1, ""); // both-error {{not an integral constant expression}} \
93 // both-note {{in call to 'notHeapAllocated()'}}
94
95 consteval int deleteNull() {
96 int *A = nullptr;
97 delete A;
98 return 1;
99 }
100 static_assert(deleteNull() == 1, "");
101
102 consteval int doubleDelete() { // both-error {{never produces a constant expression}}
103 int *A = new int;
104 delete A;
105 delete A; // both-note 2{{delete of pointer that has already been deleted}}
106 return 1;
107 }
108 static_assert(doubleDelete() == 1); // both-error {{not an integral constant expression}} \
109 // both-note {{in call to 'doubleDelete()'}}
110
111 constexpr int AutoArray() {
112 auto array = new int[]{0, 1, 2, 3};
113 int ret = array[3];
114 delete [] array;
115 return ret;
116 }
117
118 static_assert(AutoArray() == 3);
119
120 #if 0
121 consteval int largeArray1(bool b) {
122 if (b) {
123 int *a = new int[1ull<<32]; // both-note {{cannot allocate array; evaluated array bound 4294967296 is too large}}
124 delete[] a;
125 }
126 return 1;
127 }
128 static_assert(largeArray1(false) == 1, "");
129 static_assert(largeArray1(true) == 1, ""); // both-error {{not an integral constant expression}} \
130 // both-note {{in call to 'largeArray1(true)'}}
131
132 consteval int largeArray2(bool b) {
133 if (b) {
134 S *a = new S[1ull<<32]; // both-note {{cannot allocate array; evaluated array bound 4294967296 is too large}}
135 delete[] a;
136 }
137 return 1;
138 }
139 static_assert(largeArray2(false) == 1, "");
140 static_assert(largeArray2(true) == 1, ""); // both-error {{not an integral constant expression}} \
141 // both-note {{in call to 'largeArray2(true)'}}
142 #endif
143 namespace Arrays {
144 constexpr int d() {
145 int *Arr = new int[12];
146
147 Arr[0] = 1;
148 Arr[1] = 5;
149
150 int sum = Arr[0] + Arr[1];
151 delete[] Arr;
152 return sum;
153 }
154 static_assert(d() == 6);
155
156
157 constexpr int mismatch1() { // both-error {{never produces a constant expression}}
158 int *i = new int(12); // both-note {{allocated with 'new' here}} \
159 // both-note 2{{heap allocation performed here}}
160 delete[] i; // both-warning {{'delete[]' applied to a pointer that was allocated with 'new'}} \
161 // both-note 2{{array delete used to delete pointer to non-array object of type 'int'}}
162 return 6;
163 }
164 static_assert(mismatch1() == 6); // both-error {{not an integral constant expression}} \
165 // both-note {{in call to 'mismatch1()'}}
166
167 constexpr int mismatch2() { // both-error {{never produces a constant expression}}
168 int *i = new int[12]; // both-note {{allocated with 'new[]' here}} \
169 // both-note 2{{heap allocation performed here}}
170 delete i; // both-warning {{'delete' applied to a pointer that was allocated with 'new[]'}} \
171 // both-note 2{{non-array delete used to delete pointer to array object of type 'int[12]'}}
172 return 6;
173 }
174 static_assert(mismatch2() == 6); // both-error {{not an integral constant expression}} \
175 // both-note {{in call to 'mismatch2()'}}
176
177 constexpr int mismatch3() { // both-error {{never produces a constant expression}}
178 int a = 0;
179 struct S {};
180 struct T : S {};
181 T *p = new T[3]{}; // both-note 2{{heap allocation performed here}}
182 delete (S*)p; // both-note 2{{non-array delete used to delete pointer to array object of type 'T[3]'}}
183
184 return 0;
185
186 }
187 static_assert(mismatch3() == 0); // both-error {{not an integral constant expression}} \
188 // both-note {{in call to}}
189
190 /// Array of composite elements.
191 constexpr int foo() {
192 S *ss = new S[12];
193
194 ss[0].a = 12;
195
196 int m = ss[0].a;
197
198 delete[] ss;
199 return m;
200 }
201 static_assert(foo() == 12);
202
203
204
205 constexpr int ArrayInit() {
206 auto array = new int[4]{0, 1, 2, 3};
207 int ret = array[0];
208 delete [] array;
209 return ret;
210 }
211 static_assert(ArrayInit() == 0, "");
212
213 struct S {
214 float F;
215 };
216 constexpr float ArrayInit2() {
217 auto array = new S[4]{};
218 float ret = array[0].F;
219 delete [] array;
220 return ret;
221 }
222 static_assert(ArrayInit2() == 0.0f, "");
223 }
224
225 namespace std {
226 struct type_info;
227 struct destroying_delete_t {
228 explicit destroying_delete_t() = default;
229 } inline constexpr destroying_delete{};
230 struct nothrow_t {
231 explicit nothrow_t() = default;
232 } inline constexpr nothrow{};
233 using size_t = decltype(sizeof(0));
234 enum class align_val_t : size_t {};
235 };
236
237 [[nodiscard]] void *operator new(std::size_t, const std::nothrow_t&) noexcept;
238 [[nodiscard]] void *operator new(std::size_t, std::align_val_t, const std::nothrow_t&) noexcept;
239 [[nodiscard]] void *operator new[](std::size_t, const std::nothrow_t&) noexcept;
240 [[nodiscard]] void *operator new[](std::size_t, std::align_val_t, const std::nothrow_t&) noexcept;
241 [[nodiscard]] void *operator new[](std::size_t, std::align_val_t);
242 void operator delete(void*, const std::nothrow_t&) noexcept;
243 void operator delete(void*, std::align_val_t, const std::nothrow_t&) noexcept;
244 void operator delete[](void*, const std::nothrow_t&) noexcept;
245 void operator delete[](void*, std::align_val_t, const std::nothrow_t&) noexcept;
246
247 struct placement_new_arg {};
248 void *operator new(std::size_t, placement_new_arg);
249 void operator delete(void*, placement_new_arg);
250
251
252 constexpr void *operator new(std::size_t, void *p) { return p; }
253 namespace std {
254 template<typename T> constexpr T *construct(T *p) { return new (p) T; }
255 template<typename T> constexpr void destroy(T *p) { p->~T(); }
256 }
257
258
259
260 namespace PlacementNew {
261 constexpr int foo() { // both-error {{never produces a constant expression}}
262 char c[sizeof(int)];
263 new (c) int{12}; // both-note {{this placement new expression is not supported in constant expressions before C++2c}}
264 return 0;
265 }
266 }
267
268 namespace NowThrowNew {
269 constexpr bool erroneous_array_bound_nothrow(long long n) {
270 int *p = new (std::nothrow) int[n];
271 bool result = p != nullptr;
272 delete[] p;
273 return result;
274 }
275 static_assert(erroneous_array_bound_nothrow(3));
276 static_assert(erroneous_array_bound_nothrow(0));
277 static_assert(erroneous_array_bound_nothrow(-1) == 0);
278 static_assert(!erroneous_array_bound_nothrow(1LL << 62));
279
280 struct S { int a; };
281 constexpr bool erroneous_array_bound_nothrow2(long long n) {
282 S *p = new (std::nothrow) S[n];
283 bool result = p != nullptr;
284 delete[] p;
285 return result;
286 }
287 static_assert(erroneous_array_bound_nothrow2(3));
288 static_assert(erroneous_array_bound_nothrow2(0));
289 static_assert(erroneous_array_bound_nothrow2(-1) == 0);
290 static_assert(!erroneous_array_bound_nothrow2(1LL << 62));
291
292 constexpr bool erroneous_array_bound(long long n) {
293 delete[] new int[n]; // both-note {{array bound -1 is negative}} both-note {{array bound 4611686018427387904 is too large}}
294 return true;
295 }
296 static_assert(erroneous_array_bound(3));
297 static_assert(erroneous_array_bound(0));
298 static_assert(erroneous_array_bound(-1)); // both-error {{constant expression}} both-note {{in call}}
299 static_assert(erroneous_array_bound(1LL << 62)); // both-error {{constant expression}} both-note {{in call}}
300
301 constexpr bool evaluate_nothrow_arg() {
302 bool ok = false;
303 delete new ((ok = true, std::nothrow)) int;
304 return ok;
305 }
306 static_assert(evaluate_nothrow_arg());
307 }
308
309 namespace placement_new_delete {
310 struct ClassSpecificNew {
311 void *operator new(std::size_t);
312 };
313 struct ClassSpecificDelete {
314 void operator delete(void*);
315 };
316 struct DestroyingDelete {
317 void operator delete(DestroyingDelete*, std::destroying_delete_t);
318 };
319 struct alignas(64) Overaligned {};
320
321 constexpr bool ok() {
322 delete new Overaligned;
323 delete ::new ClassSpecificNew;
324 ::delete new ClassSpecificDelete;
325 ::delete new DestroyingDelete;
326 return true;
327 }
328 static_assert(ok());
329
330 constexpr bool bad(int which) {
331 switch (which) {
332 case 0:
333 delete new (placement_new_arg{}) int; // both-note {{this placement new expression is not supported in constant expressions}}
334 break;
335
336 case 1:
337 delete new ClassSpecificNew; // both-note {{call to class-specific 'operator new'}}
338 break;
339
340 case 2:
341 delete new ClassSpecificDelete; // both-note {{call to class-specific 'operator delete'}}
342 break;
343
344 case 3:
345 delete new DestroyingDelete; // both-note {{call to class-specific 'operator delete'}}
346 break;
347
348 case 4:
349 // FIXME: This technically follows the standard's rules, but it seems
350 // unreasonable to expect implementations to support this.
351 delete new (std::align_val_t{64}) Overaligned; // both-note {{this placement new expression is not supported in constant expressions}}
352 break;
353 }
354
355 return true;
356 }
357 static_assert(bad(0)); // both-error {{constant expression}} \
358 // both-note {{in call}}
359 static_assert(bad(1)); // both-error {{constant expression}} both-note {{in call}}
360 static_assert(bad(2)); // both-error {{constant expression}} both-note {{in call}}
361 static_assert(bad(3)); // both-error {{constant expression}} both-note {{in call}}
362 static_assert(bad(4)); // both-error {{constant expression}} \
363 // both-note {{in call}}
364 }
365
366
367
368
369 namespace delete_random_things {
370 static_assert((delete new int, true));
371 static_assert((delete (int*)0, true));
372 int n; // both-note {{declared here}}
373 static_assert((delete &n, true)); // both-error {{}} \
374 // both-note {{delete of pointer '&n' that does not point to a heap-allocated object}}
375 struct A { int n; };
376 static_assert((delete &(new A)->n, true)); // both-error {{}} \
377 // both-note {{delete of pointer to subobject }}
378 static_assert((delete (new int + 1), true)); // both-error {{}} \
379 // both-note {{delete of pointer '&{*new int#0} + 1' that does not point to complete object}}
380 static_assert((delete[] (new int[3] + 1), true)); // both-error {{}} \
381 // both-note {{delete of pointer to subobject}}
382 static_assert((delete &(int&)(int&&)0, true)); // both-error {{}} \
383 // both-note {{delete of pointer '&0' that does not point to a heap-allocated object}} \
384 // both-note {{temporary created here}}
385 }
386
387 namespace value_dependent_delete {
388 template<typename T> void f(T *p) {
389 int arr[(delete p, 0)];
390 }
391 }
392
393 namespace memory_leaks {
394 static_assert(*new bool(true)); // both-error {{}} both-note {{allocation performed here was not deallocated}}
395
396 constexpr bool *f() { return new bool(true); } // both-note {{allocation performed here was not deallocated}}
397 static_assert(*f()); // both-error {{}}
398
399 struct UP {
400 bool *p;
401 constexpr ~UP() { delete p; }
402 constexpr bool &operator*() { return *p; }
403 };
404 constexpr UP g() { return {new bool(true)}; }
405 static_assert(*g()); // ok
406
407 constexpr bool h(UP p) { return *p; }
408 static_assert(h({new bool(true)})); // ok
409 }
410
411 /// From test/SemaCXX/cxx2a-consteval.cpp
412
413 namespace std {
414 template <typename T> struct remove_reference { using type = T; };
415 template <typename T> struct remove_reference<T &> { using type = T; };
416 template <typename T> struct remove_reference<T &&> { using type = T; };
417 template <typename T>
418 constexpr typename std::remove_reference<T>::type&& move(T &&t) noexcept {
419 return static_cast<typename std::remove_reference<T>::type &&>(t);
420 }
421 }
422
423 namespace cxx2a {
424 struct A {
425 int* p = new int(42); // both-note 3{{heap allocation performed here}}
426 consteval int ret_i() const { return p ? *p : 0; }
427 consteval A ret_a() const { return A{}; }
428 constexpr ~A() { delete p; }
429 };
430
431 consteval int by_value_a(A a) { return a.ret_i(); }
432
433 consteval int const_a_ref(const A &a) {
434 return a.ret_i();
435 }
436
437 consteval int rvalue_ref(const A &&a) {
438 return a.ret_i();
439 }
440
441 consteval const A &to_lvalue_ref(const A &&a) {
442 return a;
443 }
444
445 void test() {
446 constexpr A a{ nullptr };
447 { int k = A().ret_i(); }
448
449 { A k = A().ret_a(); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \
450 // both-note {{heap-allocated object is not a constant expression}}
451 { A k = to_lvalue_ref(A()); } // both-error {{'cxx2a::to_lvalue_ref' is not a constant expression}} \
452 // both-note {{reference to temporary is not a constant expression}} \
453 // both-note {{temporary created here}}
454 { A k = to_lvalue_ref(A().ret_a()); } // both-error {{'cxx2a::to_lvalue_ref' is not a constant expression}} \
455 // both-note {{reference to temporary is not a constant expression}} \
456 // both-note {{temporary created here}}
457 { int k = A().ret_a().ret_i(); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \
458 // both-note {{heap-allocated object is not a constant expression}}
459 { int k = by_value_a(A()); }
460 { int k = const_a_ref(A()); }
461 { int k = const_a_ref(a); }
462 { int k = rvalue_ref(A()); }
463 { int k = rvalue_ref(std::move(a)); }
464 { int k = const_a_ref(A().ret_a()); }
465 { int k = const_a_ref(to_lvalue_ref(A().ret_a())); }
466 { int k = const_a_ref(to_lvalue_ref(std::move(a))); }
467 { int k = by_value_a(A().ret_a()); }
468 { int k = by_value_a(to_lvalue_ref(static_cast<const A&&>(a))); }
469 { int k = (A().ret_a(), A().ret_i()); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \
470 // both-note {{is not a constant expression}} \
471 // both-warning {{left operand of comma operator has no effect}}
472 { int k = (const_a_ref(A().ret_a()), A().ret_i()); } // both-warning {{left operand of comma operator has no effect}}
473 }
474 }
475
476 constexpr int *const &p = new int; // both-error {{must be initialized by a constant expression}} \
477 // both-note {{pointer to heap-allocated object}} \
478 // both-note {{allocation performed here}}
479
480 constexpr const int *A[] = {nullptr, nullptr, new int{12}}; // both-error {{must be initialized by a constant expression}} \
481 // both-note {{pointer to heap-allocated object}} \
482 // both-note {{allocation performed here}}
483
484 struct Sp {
485 const int *p;
486 };
487 constexpr Sp ss[] = {Sp{new int{154}}}; // both-error {{must be initialized by a constant expression}} \
488 // both-note {{pointer to heap-allocated object}} \
489 // both-note {{allocation performed here}}
490
491 namespace DeleteRunsDtors {
492 struct InnerFoo {
493 int *mem;
494 constexpr ~InnerFoo() {
495 delete mem;
496 }
497 };
498
499 struct Foo {
500 int *a;
501 InnerFoo IF;
502
503 constexpr Foo() {
504 a = new int(13);
505 IF.mem = new int(100);
506 }
507 constexpr ~Foo() { delete a; }
508 };
509
510 constexpr int abc() {
511 Foo *F = new Foo();
512 int n = *F->a;
513 delete F;
514
515 return n;
516 }
517 static_assert(abc() == 13);
518
519 constexpr int abc2() {
520 Foo *f = new Foo[3];
521
522 delete[] f;
523
524 return 1;
525 }
526 static_assert(abc2() == 1);
527 }
528
529 namespace FaultyDtorCalledByDelete {
530 struct InnerFoo {
531 int *mem;
532 constexpr ~InnerFoo() {
533 if (mem) {
534 (void)(1/0); // both-warning {{division by zero is undefined}} \
535 // both-note {{division by zero}}
536 }
537 delete mem;
538 }
539 };
540
541 struct Foo {
542 int *a;
543 InnerFoo IF;
544
545 constexpr Foo() {
546 a = new int(13);
547 IF.mem = new int(100);
548 }
549 constexpr ~Foo() { delete a; }
550 };
551
552 constexpr int abc() {
553 Foo *F = new Foo();
554 int n = *F->a;
555 delete F; // both-note 2{{in call to}}
556
557 return n;
558 }
559 static_assert(abc() == 13); // both-error {{not an integral constant expression}} \
560 // both-note {{in call to 'abc()'}}
561 }
562
563 namespace DeleteThis {
564 constexpr bool super_secret_double_delete() {
565 struct A {
566 constexpr ~A() { delete this; } // both-note {{destruction of object that is already being destroyed}} \
567 // ref-note {{in call to}}
568 };
569 delete new A; // both-note {{in call to}}
570 return true;
571 }
572 static_assert(super_secret_double_delete()); // both-error {{not an integral constant expression}} \
573 // both-note {{in call to 'super_secret_double_delete()'}}
574
575 struct B {
576 constexpr void reset() { delete this; }
577 };
578 static_assert(((new B)->reset(), true));
579 }
580
581 namespace CastedDelete {
582 struct S {
583 constexpr S(int *p) : p(p) {}
584 constexpr virtual ~S() { *p = 1; }
585 int *p;
586 };
587 struct T: S {
588 // implicit destructor defined eagerly because it is constexpr and virtual
589 using S::S;
590 };
591
592 constexpr int vdtor_1() {
593 int a;
594 delete (S*)new T(&a);
595 return a;
596 }
597 static_assert(vdtor_1() == 1);
598
599 constexpr int foo() { // both-error {{never produces a constant expression}}
600 struct S {};
601 struct T : S {};
602 S *p = new T();
603 delete p; // both-note 2{{delete of object with dynamic type 'T' through pointer to base class type 'S' with non-virtual destructor}}
604 return 1;
605 }
606 static_assert(foo() == 1); // both-error {{not an integral constant expression}} \
607 // both-note {{in call to}}
608 }
609
610 constexpr void use_after_free_2() { // both-error {{never produces a constant expression}}
611 struct X { constexpr void f() {} };
612 X *p = new X;
613 delete p;
614 p->f(); // both-note {{member call on heap allocated object that has been deleted}}
615 }
616
617 /// std::allocator definition
618 namespace std {
619 using size_t = decltype(sizeof(0));
620 template<typename T> struct allocator {
621 constexpr T *allocate(size_t N) {
622 return (T*)__builtin_operator_new(sizeof(T) * N); // #alloc
623 }
624 constexpr void deallocate(void *p) {
625 __builtin_operator_delete(p); // both-note 2{{std::allocator<...>::deallocate' used to delete pointer to object allocated with 'new'}} \
626 // both-note {{used to delete a null pointer}} \
627 // both-note {{delete of pointer '&no_deallocate_nonalloc' that does not point to a heap-allocated object}}
628 }
629 };
630 template<typename T, typename ...Args>
631 constexpr void construct_at(void *p, Args &&...args) { // #construct
632 new (p) T((Args&&)args...);
633 }
634 }
635
636 constexpr int *escape = std::allocator<int>().allocate(3); // both-error {{constant expression}} \
637 // both-note {{pointer to subobject of heap-allocated}} \
638 // both-note {{heap allocation performed here}}
639
640 /// Specialization for float, using operator new/delete.
641 namespace std {
642 using size_t = decltype(sizeof(0));
643 template<> struct allocator<float> {
644 constexpr float *allocate(size_t N) {
645 return (float*)operator new (sizeof(float) * N);
646 }
647 constexpr void deallocate(void *p) {
648 operator delete(p);
649 }
650 };
651 }
652
653 namespace OperatorNewDelete {
654
655 constexpr bool mismatched(int alloc_kind, int dealloc_kind) {
656 int *p;
657 switch (alloc_kind) {
658 case 0:
659 p = new int; // both-note {{heap allocation performed here}}
660 break;
661 case 1:
662 p = new int[1]; // both-note {{heap allocation performed here}}
663 break;
664 case 2:
665 p = std::allocator<int>().allocate(1); // both-note 2{{heap allocation performed here}}
666 break;
667 }
668 switch (dealloc_kind) {
669 case 0:
670 delete p; // both-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
671 break;
672 case 1:
673 delete[] p; // both-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
674 break;
675 case 2:
676 std::allocator<int>().deallocate(p); // both-note 2{{in call}}
677 break;
678 }
679 return true;
680 }
681 static_assert(mismatched(0, 2)); // both-error {{constant expression}} \
682 // both-note {{in call to}}
683 static_assert(mismatched(1, 2)); // both-error {{constant expression}} \
684 // both-note {{in call to}}
685 static_assert(mismatched(2, 0)); // both-error {{constant expression}} \
686 // both-note {{in call}}
687 static_assert(mismatched(2, 1)); // both-error {{constant expression}} \
688 // both-note {{in call}}
689 static_assert(mismatched(2, 2));
690
691 constexpr bool zeroAlloc() {
692 int *F = std::allocator<int>().allocate(0);
693 std::allocator<int>().deallocate(F);
694 return true;
695 }
696 static_assert(zeroAlloc());
697
698 constexpr int arrayAlloc() {
699 int *F = std::allocator<int>().allocate(2);
700 F[0] = 10; // both-note {{assignment to object outside its lifetime is not allowed in a constant expression}}
701 F[1] = 13;
702 int Res = F[1] + F[0];
703 std::allocator<int>().deallocate(F);
704 return Res;
705 }
706 static_assert(arrayAlloc() == 23); // both-error {{not an integral constant expression}} \
707 // both-note {{in call to}}
708
709 struct S {
710 int i;
711 constexpr S(int i) : i(i) {}
712 constexpr ~S() { }
713 };
714
715 /// FIXME: This is broken in the current interpreter.
716 constexpr bool structAlloc() {
717 S *s = std::allocator<S>().allocate(1);
718
719 s->i = 12; // ref-note {{assignment to object outside its lifetime is not allowed in a constant expression}}
720
721 bool Res = (s->i == 12);
722 std::allocator<S>().deallocate(s);
723
724 return Res;
725 }
726 static_assert(structAlloc()); // ref-error {{not an integral constant expression}} \
727 // ref-note {{in call to}}
728
729 constexpr bool structAllocArray() {
730 S *s = std::allocator<S>().allocate(9);
731
732 s[2].i = 12; // ref-note {{assignment to object outside its lifetime is not allowed in a constant expression}}
733 bool Res = (s[2].i == 12);
734 std::allocator<S>().deallocate(s);
735
736 return Res;
737 }
738 static_assert(structAllocArray()); // ref-error {{not an integral constant expression}} \
739 // ref-note {{in call to}}
740
741 constexpr bool alloc_from_user_code() {
742 void *p = __builtin_operator_new(sizeof(int)); // both-note {{cannot allocate untyped memory in a constant expression; use 'std::allocator<T>::allocate'}}
743 __builtin_operator_delete(p);
744 return true;
745 }
746 static_assert(alloc_from_user_code()); // both-error {{constant expression}} \
747 // both-note {{in call to}}
748
749
750 constexpr int no_deallocate_nullptr = (std::allocator<int>().deallocate(nullptr), 1); // both-error {{constant expression}} \
751 // both-note {{in call}}
752
753 static_assert((std::allocator<float>().deallocate(std::allocator<float>().allocate(10)), 1) == 1);
754 }
755
756 namespace Limits {
757 template<typename T>
758 constexpr T dynarray(int elems, int i) {
759 T *p;
760 if constexpr (sizeof(T) == 1)
761 p = new T[elems]{"fox"};
762 else
763 p = new T[elems]{1, 2, 3};
764 T n = p[i];
765 delete [] p;
766 return n;
767 }
768 static_assert(dynarray<char>(5, 0) == 'f');
769 static_assert(dynarray<char>(5, 4) == 0);
770
771 #if __LP64__
772 template <typename T>
773 struct S {
774 constexpr S(unsigned long long N)
775 : data(nullptr){
776 data = alloc.allocate(N); // both-note {{in call to 'this->alloc.allocate(18446744073709551615)}}
777 }
778 constexpr T operator[](std::size_t i) const {
779 return data[i];
780 }
781
782 constexpr ~S() {
783 alloc.deallocate(data);
784 }
785 std::allocator<T> alloc;
786 T* data;
787 };
788
789 constexpr std::size_t s = S<std::size_t>(~0UL)[42]; // both-error {{constexpr variable 's' must be initialized by a constant expression}} \
790 // both-note@#alloc {{cannot allocate array; evaluated array bound 2305843009213693951 is too large}} \
791 // both-note {{in call to}}
792 #endif
793 }
794
795 /// Just test that we reject placement-new expressions before C++2c.
796 /// Tests for successful expressions are in placement-new.cpp
797 namespace Placement {
798 consteval auto ok1() { // both-error {{never produces a constant expression}}
799 bool b;
800 new (&b) bool(true); // both-note 2{{this placement new expression is not supported in constant expressions before C++2c}}
801 return b;
802 }
803 static_assert(ok1()); // both-error {{not an integral constant expression}} \
804 // both-note {{in call to}}
805
806 /// placement-new should be supported before C++26 in std functions.
807 constexpr int ok2() {
808 int *I = new int;
809 std::construct_at<int>(I);
810 int r = *I;
811 delete I;
812 return r;
813 }
814 static_assert(ok2()== 0);
815 }
816
817 constexpr bool virt_delete(bool global) {
818 struct A {
819 virtual constexpr ~A() {}
820 };
821 struct B : A {
822 void operator delete(void *);
823 constexpr ~B() {}
824 };
825
826 A *p = new B;
827 if (global)
828 ::delete p;
829 else
830 delete p; // both-note {{call to class-specific 'operator delete'}}
831 return true;
832 }
833 static_assert(virt_delete(true));
834 static_assert(virt_delete(false)); // both-error {{not an integral constant expression}} \
835 // both-note {{in call to}}
836
837
838 namespace ToplevelScopeInTemplateArg {
839 class string {
840 public:
841 char *mem;
842 constexpr string() {
843 this->mem = new char(1);
844 }
845 constexpr ~string() {
846 delete this->mem;
847 }
848 constexpr unsigned size() const { return 4; }
849 };
850
851
852 template <unsigned N>
853 void test() {};
854
855 void f() {
856 test<string().size()>();
857 static_assert(string().size() == 4);
858 }
859 }
860
861 template <typename T>
862 struct SS {
863 constexpr SS(unsigned long long N)
864 : data(nullptr){
865 data = alloc.allocate(N);
866 for(std::size_t i = 0; i < N; i ++)
867 std::construct_at<T>(data + i, i);
868 }
869
870 constexpr SS()
871 : data(nullptr){
872 data = alloc.allocate(1);
873 std::construct_at<T>(data);
874 }
875
876 constexpr T operator[](std::size_t i) const {
877 return data[i];
878 }
879
880 constexpr ~SS() {
881 alloc.deallocate(data);
882 }
883 std::allocator<T> alloc;
884 T* data;
885 };
886 constexpr unsigned short ssmall = SS<unsigned short>(100)[42];
887 constexpr auto Ss = SS<S>()[0];
888
889
890 namespace IncompleteArray {
891 struct A {
892 int b = 10;
893 };
894 constexpr int test1() {
895 int n = 5;
896 int* a = new int[n];
897 int c = a[0]; // both-note {{read of uninitialized object}}
898 delete[] a;
899 return c;
900 }
901 static_assert(test1() == 10); // both-error {{not an integral constant expression}} \
902 // both-note {{in call to}}
903
904 constexpr int test2() {
905 int n = 0;
906 int* a = new int[n];
907 delete[] a;
908 return 10;
909 }
910 static_assert(test2() == 10);
911
912 /// In this case, the type of the initializer is A[2], while the full size of the
913 /// allocated array is of course 5. The remaining 3 elements need to be initialized
914 /// using A's constructor.
915 constexpr int test3() {
916 int n = 3;
917 A* a = new A[n]{5, 1};
918 int c = a[0].b + a[1].b + a[2].b;
919 delete[] a;
920 return c;
921 }
922 static_assert(test3() == (5 + 1 + 10));
923
924 constexpr int test4() {
925 auto n = 3;
926 int *a = new int[n]{12};
927 int c = a[0] + a[1];
928 delete[] a;
929 return c;
930 }
931 static_assert(test4() == 12);
932
933
934 constexpr char *f(int n) {
935 return new char[n]();
936 }
937 static_assert((delete[] f(2), true));
938 }
939
940 namespace NonConstexprArrayCtor {
941 struct S {
942 S() {} // both-note 2{{declared here}}
943 };
944
945 constexpr bool test() { // both-error {{never produces a constant expression}}
946 auto s = new S[1]; // both-note 2{{non-constexpr constructor}}
947 return true;
948 }
949 static_assert(test()); // both-error {{not an integral constant expression}} \
950 // both-note {{in call to}}
951 }
952
953 namespace ArrayBaseCast {
954 struct A {};
955 struct B : A {};
956 constexpr bool test() {
957 B *b = new B[2];
958
959 A* a = b;
960
961 delete[] b;
962 return true;
963 }
964 static_assert(test());
965 }
966
967 namespace PR45350 {
968 int q;
969 struct V { int n; int *p = &n; constexpr ~V() { *p = *p * 10 + n; }};
970 constexpr int f(int n) {
971 int k = 0;
972 V *p = new V[n];
973 for (int i = 0; i != n; ++i) {
974 if (p[i].p != &p[i].n) return -1;
975 p[i].n = i;
976 p[i].p = &k;
977 }
978 delete[] p;
979 return k;
980 }
981 // [expr.delete]p6:
982 // In the case of an array, the elements will be destroyed in order of
983 // decreasing address
984 static_assert(f(6) == 543210);
985 }
986
987 namespace ZeroSizeSub {
988 consteval unsigned ptr_diff1() {
989 int *b = new int[0];
990 unsigned d = 0;
991 d = b - b;
992 delete[] b;
993
994 return d;
995 }
996 static_assert(ptr_diff1() == 0);
997
998
999 consteval unsigned ptr_diff2() { // both-error {{never produces a constant expression}}
1000 int *a = new int[0];
1001 int *b = new int[0];
1002
1003 unsigned d = a - b; // both-note 2{{arithmetic involving unrelated objects}}
1004 delete[] b;
1005 delete[] a;
1006 return d;
1007 }
1008 static_assert(ptr_diff2() == 0); // both-error {{not an integral constant expression}} \
1009 // both-note {{in call to}}
1010 }
1011
1012 namespace WrongFrame {
1013 constexpr int foo() {
1014 int *p = nullptr;
1015 __builtin_operator_delete(p); // both-note {{subexpression not valid in a constant expression}}
1016
1017 return 1;
1018 }
1019 static_assert(foo()); // both-error {{not an integral constant expression}} \
1020 // both-note {{in call to}}
1021
1022 }
1023
1024 constexpr int no_deallocate_nonalloc = (std::allocator<int>().deallocate((int*)&no_deallocate_nonalloc), 1); // both-error {{constant expression}} \
1025 // both-note {{in call}} \
1026 // both-note {{declared here}}
1027
1028 namespace OpNewNothrow {
1029 constexpr int f() {
1030 int *v = (int*)operator new(sizeof(int), std::align_val_t(2), std::nothrow); // both-note {{cannot allocate untyped memory in a constant expression; use 'std::allocator<T>::allocate' to allocate memory of type 'T'}}
1031 operator delete(v, std::align_val_t(2), std::nothrow);
1032 return 1;
1033 }
1034 static_assert(f()); // both-error {{not an integral constant expression}} \
1035 // both-note {{in call to}}
1036 }
1037
1038 namespace BaseCompare {
1039 struct Cmp {
1040 void *p;
1041
1042 template<typename T>
1043 constexpr Cmp(T *t) : p(t) {}
1044
1045 constexpr friend bool operator==(Cmp a, Cmp b) {
1046 return a.p == b.p;
1047 }
1048 };
1049
1050 class Base {};
1051 class Derived : public Base {};
1052 constexpr bool foo() {
1053 Derived *D = std::allocator<Derived>{}.allocate(1);;
1054 std::construct_at<Derived>(D);
1055
1056 Derived *d = D;
1057 Base *b = D;
1058
1059 Cmp ca(d);
1060 Cmp cb(b);
1061
1062 if (ca == cb) {
1063 std::allocator<Derived>{}.deallocate(D);
1064 return true;
1065 }
1066 std::allocator<Derived>{}.deallocate(D);
1067
1068 return false;
1069
1070 }
1071 static_assert(foo());
1072 }
1073
1074
1075 namespace NegativeArraySize {
1076 constexpr void f() { // both-error {{constexpr function never produces a constant expression}}
1077 int x = -1;
1078 int *p = new int[x]; //both-note {{cannot allocate array; evaluated array bound -1 is negative}}
1079 }
1080 } // namespace NegativeArraySize
1081
1082 namespace NewNegSizeNothrow {
1083 constexpr int get_neg_size() {
1084 return -1;
1085 }
1086
1087 constexpr bool test_nothrow_neg_size() {
1088 int x = get_neg_size();
1089 int* p = new (std::nothrow) int[x];
1090 return p == nullptr;
1091 }
1092
1093 static_assert(test_nothrow_neg_size(), "expected nullptr");
1094 } // namespace NewNegSizeNothrow
1095
1096 #if __SIZEOF_SIZE_T == 8
1097 /// We can't allocate the array here as it is too big.
1098 /// Make sure we're not crashing by assuming an non-null
1099 /// Descriptor.
1100 namespace HugeAllocation {
1101 void *p;
1102 void foo ()
1103 {
1104 p = new char [256][256][256][256][256];
1105 }
1106 }
1107 #endif
1108
1109 namespace ZeroSizeArray {
1110 constexpr int foo() {
1111 int *A = new int[0];
1112 int diff = A - (&A[0]);
1113 delete[] A;
1114 return diff;
1115 }
1116 static_assert(foo() == 0);
1117 }
1118
1119 namespace NonLiteralType {
1120 /// This used to crash.
1121 constexpr void foo() {
1122 struct O {};
1123
1124 struct S {
1125 O *s;
1126 constexpr S() : s{std::allocator<O>{}.allocate(1)} {}
1127 };
1128 }
1129 }
1130
1131 #else
1132 /// Make sure we reject this prior to C++20
1133 constexpr int a() { // both-error {{never produces a constant expression}}
1134 delete new int(12); // both-note 2{{dynamic memory allocation is not permitted in constant expressions until C++20}}
1135 return 1;
1136 }
1137 static_assert(a() == 1, ""); // both-error {{not an integral constant expression}} \
1138 // both-note {{in call to 'a()'}}
1139
1140
1141 static_assert(true ? *new int : 4, ""); // both-error {{expression is not an integral constant expression}} \
1142 // both-note {{read of uninitialized object is not allowed in a constant expression}}
1143
1144 #endif