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"
4
alloc_from_user_code()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'}}
7 DELETE(p);
8 return true;
9 }
10 static_assert(alloc_from_user_code()); // expected-error {{constant expression}} expected-note {{in call}}
11
12 namespace std {
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 {
allocatestd::allocator16 constexpr T *allocate(size_t N) {
17 return (T*)NEW(sizeof(T) * N); // expected-note 3{{heap allocation}} expected-note {{not deallocated}}
18 }
deallocatestd::allocator19 constexpr void deallocate(void *p) {
20 DELETE(p); // expected-note 2{{'std::allocator<...>::deallocate' used to delete pointer to object allocated with 'new'}}
21 }
22 };
23 }
24
alloc_via_std_allocator()25 constexpr bool alloc_via_std_allocator() {
26 std::allocator<int> alloc;
27 int *p = alloc.allocate(1);
28 alloc.deallocate(p);
29 return true;
30 }
31 static_assert(alloc_via_std_allocator());
32
33 template<> struct std::allocator<void()> {
allocatestd::allocator34 constexpr void *allocate() { return NEW(8); } // expected-note {{cannot allocate memory of function type 'void ()'}}
35 };
36 constexpr void *fn = std::allocator<void()>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
37
38 struct Incomplete;
39 template<> struct std::allocator<Incomplete> {
allocatestd::allocator40 constexpr void *allocate() { return NEW(8); } // expected-note {{cannot allocate memory of incomplete type 'Incomplete'}}
41 };
42 constexpr void *incomplete = std::allocator<Incomplete>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
43
44 struct WrongSize { char x[5]; };
45 static_assert(sizeof(WrongSize) == 5);
46 template<> struct std::allocator<WrongSize> {
allocatestd::allocator47 constexpr void *allocate() { return NEW(7); } // expected-note {{allocated size 7 is not a multiple of size 5 of element type 'WrongSize'}}
48 };
49 constexpr void *wrong_size = std::allocator<WrongSize>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
50
mismatched(int alloc_kind,int dealloc_kind)51 constexpr bool mismatched(int alloc_kind, int dealloc_kind) {
52 int *p;
53 switch (alloc_kind) {
54 case 0:
55 p = new int; // expected-note {{heap allocation}}
56 break;
57 case 1:
58 p = new int[1]; // expected-note {{heap allocation}}
59 break;
60 case 2:
61 p = std::allocator<int>().allocate(1);
62 break;
63 }
64 switch (dealloc_kind) {
65 case 0:
66 delete p; // expected-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
67 break;
68 case 1:
69 delete[] p; // expected-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
70 break;
71 case 2:
72 std::allocator<int>().deallocate(p); // expected-note 2{{in call}}
73 break;
74 }
75 return true;
76 }
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));
82
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
operator new(std::size_t,void * p)87 void *operator new(std::size_t, void *p) { return p; }
no_placement_new_in_user_code()88 constexpr bool no_placement_new_in_user_code() { // expected-error {{never produces a constant expression}}
89 int a;
90 new (&a) int(42); // expected-note {{call to placement 'operator new'}}
91 return a == 42;
92 }
93
94 namespace std {
placement_new_in_stdlib()95 constexpr bool placement_new_in_stdlib() {
96 int a;
97 new (&a) int(42);
98 return a == 42;
99 }
100 }
101 static_assert(std::placement_new_in_stdlib());
102
103 namespace std {
104 template<typename T, typename ...Args>
construct_at(void * p,Args &&...args)105 constexpr void construct_at(void *p, Args &&...args) {
106 new (p) T((Args&&)args...); // #new
107 }
108 }
109
call_std_construct_at()110 constexpr bool call_std_construct_at() {
111 int *p = std::allocator<int>().allocate(3);
112 std::construct_at<int>(p, 1);
113 std::construct_at<int>(p + 1, 2);
114 std::construct_at<int>(p + 2, 3);
115 bool good = p[0] + p[1] + p[2] == 6;
116 std::allocator<int>().deallocate(p);
117 return good;
118 }
119 static_assert(call_std_construct_at());
120
bad_construct_at_type()121 constexpr bool bad_construct_at_type() {
122 int a;
123 // expected-note@#new {{placement new would change type of storage from 'int' to 'float'}}
124 std::construct_at<float>(&a, 1.0f); // expected-note {{in call}}
125 return true;
126 }
127 static_assert(bad_construct_at_type()); // expected-error{{}} expected-note {{in call}}
128
bad_construct_at_subobject()129 constexpr bool bad_construct_at_subobject() {
130 struct X { int a, b; };
131 union A {
132 int a;
133 X x;
134 };
135 A a = {1};
136 // expected-note@#new {{construction of subobject of member 'x' of union with active member 'a' is not allowed in a constant expression}}
137 std::construct_at<int>(&a.x.a, 1); // expected-note {{in call}}
138 return true;
139 }
140 static_assert(bad_construct_at_subobject()); // expected-error{{}} expected-note {{in call}}
141
change_union_member()142 constexpr bool change_union_member() {
143 union U {
144 int a;
145 int b;
146 };
147 U u = {.a = 1};
148 std::construct_at<int>(&u.b, 2);
149 return u.b == 2;
150 }
151 static_assert(change_union_member());
152
153 int external;
154 // expected-note@#new {{visible outside}}
155 static_assert((std::construct_at<int>(&external, 1), true)); // expected-error{{}} expected-note {{in call}}
156
157 constexpr int &&temporary = 0; // expected-note {{created here}}
158 // expected-note@#new {{construction of temporary is not allowed in a constant expression outside the expression that created the temporary}}
159 static_assert((std::construct_at<int>(&temporary, 1), true)); // expected-error{{}} expected-note {{in call}}
160
construct_after_lifetime()161 constexpr bool construct_after_lifetime() {
162 int *p = new int;
163 delete p;
164 // expected-note@#new {{construction of heap allocated object that has been deleted}}
165 std::construct_at<int>(p); // expected-note {{in call}}
166 return true;
167 }
168 static_assert(construct_after_lifetime()); // expected-error {{}} expected-note {{in call}}
169
construct_after_lifetime_2()170 constexpr bool construct_after_lifetime_2() {
171 struct A { struct B {} b; };
172 A a;
173 a.~A();
174 std::construct_at<A::B>(&a.b); // expected-note {{in call}}
175 // expected-note@#new {{construction of subobject of object outside its lifetime is not allowed in a constant expression}}
176 return true;
177 }
178 static_assert(construct_after_lifetime_2()); // expected-error {{}} expected-note {{in call}}
179