xref: /llvm-project/clang/test/SemaCXX/cxx2a-constexpr-dynalloc.cpp (revision 886adf8fb3ba5ec358a9a4fbe87e4da6b3ae8973)
1 // RUN: %clang_cc1 -std=c++2a -verify=expected,cxx20 %s -DNEW=__builtin_operator_new -DDELETE=__builtin_operator_delete
2 // RUN: %clang_cc1 -std=c++2a -verify=expected,cxx20 %s "-DNEW=operator new" "-DDELETE=operator delete"
3 // RUN: %clang_cc1 -std=c++2a -verify=expected,cxx20 %s "-DNEW=::operator new" "-DDELETE=::operator delete"
4 // RUN: %clang_cc1 -std=c++2c -verify=expected,cxx26 %s "-DNEW=::operator new" "-DDELETE=::operator delete"
5 
6 constexpr bool alloc_from_user_code() {
7   void *p = NEW(sizeof(int)); // expected-note {{cannot allocate untyped memory in a constant expression; use 'std::allocator<T>::allocate'}}
8   DELETE(p);
9   return true;
10 }
11 static_assert(alloc_from_user_code()); // expected-error {{constant expression}} expected-note {{in call}}
12 
13 namespace std {
14   using size_t = decltype(sizeof(0));
15   template<typename T> struct allocator {
16     constexpr T *allocate(size_t N) {
17       return (T*)NEW(sizeof(T) * N);
18     }
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'}}
21     }
22   };
23 }
24 
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()> {
34   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> {
40   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> {
47   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 
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); // expected-note 2{{heap allocation}}
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                                                            // expected-note {{heap allocation performed here}}
85 constexpr int leak = (std::allocator<int>().allocate(3), 0); // expected-error {{constant expression}} \
86                                                              // expected-note {{not deallocated}}
87 constexpr int no_lifetime_start = (*std::allocator<int>().allocate(1) = 1); // expected-error {{constant expression}} expected-note {{assignment to object outside its lifetime}}
88 constexpr int no_deallocate_nullptr = (std::allocator<int>().deallocate(nullptr), 1); // expected-error {{constant expression}} expected-note {{in call}}
89 // expected-note@#dealloc {{'std::allocator<...>::deallocate' used to delete a null pointer}}
90 constexpr int no_deallocate_nonalloc = (std::allocator<int>().deallocate((int*)&no_deallocate_nonalloc), 1); // expected-error {{constant expression}} expected-note {{in call}}
91 // expected-note@#dealloc {{delete of pointer '&no_deallocate_nonalloc' that does not point to a heap-allocated object}}
92 // expected-note@-2 {{declared here}}
93 
94 void *operator new(std::size_t, void *p) { return p; }
95 void* operator new[] (std::size_t, void* p) {return p;}
96 constexpr bool no_placement_new_in_user_code() { // cxx20-error {{constexpr function never produces a constant expression}}
97   int a;
98   new (&a) int(42); // cxx20-note {{this placement new expression is not supported in constant expressions before C++2c}}
99   return a == 42;
100 }
101 
102 namespace std {
103   constexpr bool placement_new_in_stdlib() {
104     int a;
105     new (&a) int(42);
106     return a == 42;
107   }
108 }
109 static_assert(std::placement_new_in_stdlib());
110 
111 namespace std {
112   template<typename T, typename ...Args>
113   constexpr void construct_at(void *p, Args &&...args) {
114     new (p) T((Args&&)args...); // #new
115   }
116 }
117 
118 constexpr bool call_std_construct_at() {
119   int *p = std::allocator<int>().allocate(3);
120   std::construct_at<int>(p, 1);
121   std::construct_at<int>(p + 1, 2);
122   std::construct_at<int>(p + 2, 3);
123   bool good = p[0] + p[1] + p[2] == 6;
124   std::allocator<int>().deallocate(p);
125   return good;
126 }
127 static_assert(call_std_construct_at());
128 
129 constexpr bool bad_construct_at_type() {
130   int a;
131   // expected-note@#new {{placement new would change type of storage from 'int' to 'float'}}
132   std::construct_at<float>(&a, 1.0f); // expected-note {{in call}}
133   return true;
134 }
135 static_assert(bad_construct_at_type()); // expected-error{{}} expected-note {{in call}}
136 
137 constexpr bool bad_construct_at_subobject() {
138   struct X { int a, b; };
139   union A {
140     int a;
141     X x;
142   };
143   A a = {1};
144   // expected-note@#new {{construction of subobject of member 'x' of union with active member 'a' is not allowed in a constant expression}}
145   std::construct_at<int>(&a.x.a, 1); // expected-note {{in call}}
146   return true;
147 }
148 static_assert(bad_construct_at_subobject()); // expected-error{{}} expected-note {{in call}}
149 
150 constexpr bool change_union_member() {
151   union U {
152     int a;
153     int b;
154   };
155   U u = {.a = 1};
156   std::construct_at<int>(&u.b, 2);
157   return u.b == 2;
158 }
159 static_assert(change_union_member());
160 
161 int external;
162 // expected-note@#new {{visible outside}}
163 static_assert((std::construct_at<int>(&external, 1), true)); // expected-error{{}} expected-note {{in call}}
164 
165 constexpr int &&temporary = 0; // expected-note {{created here}}
166 // expected-note@#new {{construction of temporary is not allowed in a constant expression outside the expression that created the temporary}}
167 static_assert((std::construct_at<int>(&temporary, 1), true)); // expected-error{{}} expected-note {{in call}}
168 
169 constexpr bool construct_after_lifetime() {
170   int *p = new int;
171   delete p;
172   // expected-note@#new {{construction of heap allocated object that has been deleted}}
173   std::construct_at<int>(p); // expected-note {{in call}}
174   return true;
175 }
176 static_assert(construct_after_lifetime()); // expected-error {{}} expected-note {{in call}}
177 
178 constexpr bool construct_after_lifetime_2() {
179   struct A { struct B {} b; };
180   A a;
181   a.~A();
182   std::construct_at<A::B>(&a.b); // expected-note {{in call}}
183   // expected-note@#new {{construction of subobject of object outside its lifetime is not allowed in a constant expression}}
184   return true;
185 }
186 static_assert(construct_after_lifetime_2()); // expected-error {{}} expected-note {{in call}}
187 
188 namespace PR48606 {
189   struct A { mutable int n = 0; };
190 
191   constexpr bool f() {
192     A a;
193     A *p = &a;
194     p->~A();
195     std::construct_at<A>(p);
196     return true;
197   }
198   static_assert(f());
199 
200   constexpr bool g() {
201     A *p = new A;
202     p->~A();
203     std::construct_at<A>(p);
204     delete p;
205     return true;
206   }
207   static_assert(g());
208 
209   constexpr bool h() {
210     std::allocator<A> alloc;
211     A *p = alloc.allocate(1);
212     std::construct_at<A>(p);
213     p->~A();
214     std::construct_at<A>(p);
215     p->~A();
216     alloc.deallocate(p);
217     return true;
218   }
219   static_assert(h());
220 }
221 
222 namespace GH62462 {
223 
224 class string {
225 public:
226   char *mem;
227   constexpr string() {
228     this->mem = new char(1);
229   }
230   constexpr ~string() {
231     delete this->mem;
232   }
233   constexpr unsigned size() const { return 4; }
234 };
235 
236 
237 template <unsigned N>
238 void test() {};
239 
240 void f() {
241     test<string().size()>();
242 }
243 
244 }
245