1 // RUN: %clang_cc1 -std=c++2a -verify %s -DNEW=__builtin_operator_new -DDELETE=__builtin_operator_delete
2 // RUN: %clang_cc1 -std=c++2a -verify %s "-DNEW=operator new" "-DDELETE=operator delete"
3 // RUN: %clang_cc1 -std=c++2a -verify %s "-DNEW=::operator new" "-DDELETE=::operator delete"
5 constexpr bool alloc_from_user_code() {
6 void *p
= NEW(sizeof(int)); // expected-note {{cannot allocate untyped memory in a constant expression; use 'std::allocator<T>::allocate'}}
10 static_assert(alloc_from_user_code()); // expected-error {{constant expression}} expected-note {{in call}}
13 using size_t = decltype(sizeof(0));
14 // FIXME: It would be preferable to point these notes at the location of the call to allocator<...>::[de]allocate instead
15 template<typename T
> struct allocator
{
16 constexpr T
*allocate(size_t N
) {
17 return (T
*)NEW(sizeof(T
) * N
); // expected-note 3{{heap allocation}} expected-note {{not deallocated}}
19 constexpr void deallocate(void *p
) {
20 DELETE(p
); // #dealloc expected-note 2{{'std::allocator<...>::deallocate' used to delete pointer to object allocated with 'new'}}
25 constexpr bool alloc_via_std_allocator() {
26 std::allocator
<int> alloc
;
27 int *p
= alloc
.allocate(1);
31 static_assert(alloc_via_std_allocator());
33 template<> struct std::allocator
<void()> {
34 constexpr void *allocate() { return NEW(8); } // expected-note {{cannot allocate memory of function type 'void ()'}}
36 constexpr void *fn
= std::allocator
<void()>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
39 template<> struct std::allocator
<Incomplete
> {
40 constexpr void *allocate() { return NEW(8); } // expected-note {{cannot allocate memory of incomplete type 'Incomplete'}}
42 constexpr void *incomplete
= std::allocator
<Incomplete
>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
44 struct WrongSize
{ char x
[5]; };
45 static_assert(sizeof(WrongSize
) == 5);
46 template<> struct std::allocator
<WrongSize
> {
47 constexpr void *allocate() { return NEW(7); } // expected-note {{allocated size 7 is not a multiple of size 5 of element type 'WrongSize'}}
49 constexpr void *wrong_size
= std::allocator
<WrongSize
>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
51 constexpr bool mismatched(int alloc_kind
, int dealloc_kind
) {
55 p
= new int; // expected-note {{heap allocation}}
58 p
= new int[1]; // expected-note {{heap allocation}}
61 p
= std::allocator
<int>().allocate(1);
64 switch (dealloc_kind
) {
66 delete p
; // expected-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
69 delete[] p
; // expected-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
72 std::allocator
<int>().deallocate(p
); // expected-note 2{{in call}}
77 static_assert(mismatched(0, 2)); // expected-error {{constant expression}} expected-note {{in call}}
78 static_assert(mismatched(1, 2)); // expected-error {{constant expression}} expected-note {{in call}}
79 static_assert(mismatched(2, 0)); // expected-error {{constant expression}} expected-note {{in call}}
80 static_assert(mismatched(2, 1)); // expected-error {{constant expression}} expected-note {{in call}}
81 static_assert(mismatched(2, 2));
83 constexpr int *escape
= std::allocator
<int>().allocate(3); // expected-error {{constant expression}} expected-note {{pointer to subobject of heap-allocated}}
84 constexpr int leak
= (std::allocator
<int>().allocate(3), 0); // expected-error {{constant expression}}
85 constexpr int no_lifetime_start
= (*std::allocator
<int>().allocate(1) = 1); // expected-error {{constant expression}} expected-note {{assignment to object outside its lifetime}}
86 constexpr int no_deallocate_nullptr
= (std::allocator
<int>().deallocate(nullptr), 1); // expected-error {{constant expression}} expected-note {{in call}}
87 // expected-note@#dealloc {{'std::allocator<...>::deallocate' used to delete a null pointer}}
88 constexpr int no_deallocate_nonalloc
= (std::allocator
<int>().deallocate((int*)&no_deallocate_nonalloc
), 1); // expected-error {{constant expression}} expected-note {{in call}}
89 // expected-note@#dealloc {{delete of pointer '&no_deallocate_nonalloc' that does not point to a heap-allocated object}}
90 // expected-note@-2 {{declared here}}
92 void *operator new(std::size_t, void *p
) { return p
; }
93 constexpr bool no_placement_new_in_user_code() { // expected-error {{never produces a constant expression}}
95 new (&a
) int(42); // expected-note {{call to placement 'operator new'}}
100 constexpr bool placement_new_in_stdlib() {
106 static_assert(std::placement_new_in_stdlib());
109 template<typename T
, typename
...Args
>
110 constexpr void construct_at(void *p
, Args
&&...args
) {
111 new (p
) T((Args
&&)args
...); // #new
115 constexpr bool call_std_construct_at() {
116 int *p
= std::allocator
<int>().allocate(3);
117 std::construct_at
<int>(p
, 1);
118 std::construct_at
<int>(p
+ 1, 2);
119 std::construct_at
<int>(p
+ 2, 3);
120 bool good
= p
[0] + p
[1] + p
[2] == 6;
121 std::allocator
<int>().deallocate(p
);
124 static_assert(call_std_construct_at());
126 constexpr bool bad_construct_at_type() {
128 // expected-note@#new {{placement new would change type of storage from 'int' to 'float'}}
129 std::construct_at
<float>(&a
, 1.0f
); // expected-note {{in call}}
132 static_assert(bad_construct_at_type()); // expected-error{{}} expected-note {{in call}}
134 constexpr bool bad_construct_at_subobject() {
135 struct X
{ int a
, b
; };
141 // expected-note@#new {{construction of subobject of member 'x' of union with active member 'a' is not allowed in a constant expression}}
142 std::construct_at
<int>(&a
.x
.a
, 1); // expected-note {{in call}}
145 static_assert(bad_construct_at_subobject()); // expected-error{{}} expected-note {{in call}}
147 constexpr bool change_union_member() {
153 std::construct_at
<int>(&u
.b
, 2);
156 static_assert(change_union_member());
159 // expected-note@#new {{visible outside}}
160 static_assert((std::construct_at
<int>(&external
, 1), true)); // expected-error{{}} expected-note {{in call}}
162 constexpr int &&temporary
= 0; // expected-note {{created here}}
163 // expected-note@#new {{construction of temporary is not allowed in a constant expression outside the expression that created the temporary}}
164 static_assert((std::construct_at
<int>(&temporary
, 1), true)); // expected-error{{}} expected-note {{in call}}
166 constexpr bool construct_after_lifetime() {
169 // expected-note@#new {{construction of heap allocated object that has been deleted}}
170 std::construct_at
<int>(p
); // expected-note {{in call}}
173 static_assert(construct_after_lifetime()); // expected-error {{}} expected-note {{in call}}
175 constexpr bool construct_after_lifetime_2() {
176 struct A
{ struct B
{} b
; };
179 std::construct_at
<A::B
>(&a
.b
); // expected-note {{in call}}
180 // expected-note@#new {{construction of subobject of object outside its lifetime is not allowed in a constant expression}}
183 static_assert(construct_after_lifetime_2()); // expected-error {{}} expected-note {{in call}}
186 struct A
{ mutable int n
= 0; };
192 std::construct_at
<A
>(p
);
200 std::construct_at
<A
>(p
);
207 std::allocator
<A
> alloc
;
208 A
*p
= alloc
.allocate(1);
209 std::construct_at
<A
>(p
);
211 std::construct_at
<A
>(p
);