| 1 |
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both %s |
| 2 |
// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -verify=expected,both %s |
| 3 |
// RUN: %clang_cc1 -triple=i686-linux-gnu -std=c++20 -fexperimental-new-constant-interpreter -verify=expected,both %s |
| 4 |
// RUN: %clang_cc1 -verify=ref,both %s |
| 5 |
// RUN: %clang_cc1 -std=c++20 -verify=ref,both %s |
| 6 |
// RUN: %clang_cc1 -triple=i686-linux-gnu -std=c++20 -verify=ref,both %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 |
|
| 18 |
constexpr int a() { |
| 19 |
new int(12); // both-note {{allocation performed here was not deallocated}} |
| 20 |
return 1; |
| 21 |
} |
| 22 |
static_assert(a() == 1, ""); // both-error {{not an integral constant expression}} |
| 23 |
|
| 24 |
constexpr int b() { |
| 25 |
int *i = new int(12); |
| 26 |
int m = *i; |
| 27 |
delete(i); |
| 28 |
return m; |
| 29 |
} |
| 30 |
static_assert(b() == 12, ""); |
| 31 |
|
| 32 |
|
| 33 |
struct S { |
| 34 |
int a; |
| 35 |
int b; |
| 36 |
|
| 37 |
static constexpr S *create(int a, int b) { |
| 38 |
return new S(a, b); |
| 39 |
} |
| 40 |
}; |
| 41 |
|
| 42 |
constexpr int c() { |
| 43 |
S *s = new S(12, 13); |
| 44 |
|
| 45 |
int i = s->a; |
| 46 |
delete s; |
| 47 |
|
| 48 |
return i; |
| 49 |
} |
| 50 |
static_assert(c() == 12, ""); |
| 51 |
|
| 52 |
/// Dynamic allocation in function ::create(), freed in function d(). |
| 53 |
constexpr int d() { |
| 54 |
S* s = S::create(12, 14); |
| 55 |
|
| 56 |
int sum = s->a + s->b; |
| 57 |
delete s; |
| 58 |
return sum; |
| 59 |
} |
| 60 |
static_assert(d() == 26); |
| 61 |
|
| 62 |
|
| 63 |
/// Test we emit the right diagnostic for several allocations done on |
| 64 |
/// the same site. |
| 65 |
constexpr int loop() { |
| 66 |
for (int i = 0; i < 10; ++i) { |
| 67 |
int *a = new int[10]; // both-note {{not deallocated (along with 9 other memory leaks)}} |
| 68 |
} |
| 69 |
|
| 70 |
return 1; |
| 71 |
} |
| 72 |
static_assert(loop() == 1, ""); // both-error {{not an integral constant expression}} |
| 73 |
|
| 74 |
/// No initializer. |
| 75 |
constexpr int noInit() { |
| 76 |
int *i = new int; |
| 77 |
delete i; |
| 78 |
return 0; |
| 79 |
} |
| 80 |
static_assert(noInit() == 0, ""); |
| 81 |
|
| 82 |
/// Try to delete a pointer that hasn't been heap allocated. |
| 83 |
constexpr int notHeapAllocated() { // both-error {{never produces a constant expression}} |
| 84 |
int A = 0; // both-note 2{{declared here}} |
| 85 |
delete &A; // ref-note 2{{delete of pointer '&A' that does not point to a heap-allocated object}} \ |
| 86 |
// expected-note 2{{delete of pointer '&A' that does not point to a heap-allocated object}} |
| 87 |
|
| 88 |
return 1; |
| 89 |
} |
| 90 |
static_assert(notHeapAllocated() == 1, ""); // both-error {{not an integral constant expression}} \ |
| 91 |
// both-note {{in call to 'notHeapAllocated()'}} |
| 92 |
|
| 93 |
consteval int deleteNull() { |
| 94 |
int *A = nullptr; |
| 95 |
delete A; |
| 96 |
return 1; |
| 97 |
} |
| 98 |
static_assert(deleteNull() == 1, ""); |
| 99 |
|
| 100 |
consteval int doubleDelete() { // both-error {{never produces a constant expression}} |
| 101 |
int *A = new int; |
| 102 |
delete A; |
| 103 |
delete A; // both-note 2{{delete of pointer that has already been deleted}} |
| 104 |
return 1; |
| 105 |
} |
| 106 |
static_assert(doubleDelete() == 1); // both-error {{not an integral constant expression}} \ |
| 107 |
// both-note {{in call to 'doubleDelete()'}} |
| 108 |
|
| 109 |
constexpr int AutoArray() { |
| 110 |
auto array = new int[]{0, 1, 2, 3}; |
| 111 |
int ret = array[3]; |
| 112 |
delete [] array; |
| 113 |
return ret; |
| 114 |
} |
| 115 |
|
| 116 |
static_assert(AutoArray() == 3); |
| 117 |
|
| 118 |
#if 0 |
| 119 |
consteval int largeArray1(bool b) { |
| 120 |
if (b) { |
| 121 |
int *a = new int[1ull<<32]; // both-note {{cannot allocate array; evaluated array bound 4294967296 is too large}} |
| 122 |
delete[] a; |
| 123 |
} |
| 124 |
return 1; |
| 125 |
} |
| 126 |
static_assert(largeArray1(false) == 1, ""); |
| 127 |
static_assert(largeArray1(true) == 1, ""); // both-error {{not an integral constant expression}} \ |
| 128 |
// both-note {{in call to 'largeArray1(true)'}} |
| 129 |
|
| 130 |
consteval int largeArray2(bool b) { |
| 131 |
if (b) { |
| 132 |
S *a = new S[1ull<<32]; // both-note {{cannot allocate array; evaluated array bound 4294967296 is too large}} |
| 133 |
delete[] a; |
| 134 |
} |
| 135 |
return 1; |
| 136 |
} |
| 137 |
static_assert(largeArray2(false) == 1, ""); |
| 138 |
static_assert(largeArray2(true) == 1, ""); // both-error {{not an integral constant expression}} \ |
| 139 |
// both-note {{in call to 'largeArray2(true)'}} |
| 140 |
#endif |
| 141 |
namespace Arrays { |
| 142 |
constexpr int d() { |
| 143 |
int *Arr = new int[12]; |
| 144 |
|
| 145 |
Arr[0] = 1; |
| 146 |
Arr[1] = 5; |
| 147 |
|
| 148 |
int sum = Arr[0] + Arr[1]; |
| 149 |
delete[] Arr; |
| 150 |
return sum; |
| 151 |
} |
| 152 |
static_assert(d() == 6); |
| 153 |
|
| 154 |
|
| 155 |
constexpr int mismatch1() { // both-error {{never produces a constant expression}} |
| 156 |
int *i = new int(12); // both-note {{allocated with 'new' here}} \ |
| 157 |
// both-note 2{{heap allocation performed here}} |
| 158 |
delete[] i; // both-warning {{'delete[]' applied to a pointer that was allocated with 'new'}} \ |
| 159 |
// both-note 2{{array delete used to delete pointer to non-array object of type 'int'}} |
| 160 |
return 6; |
| 161 |
} |
| 162 |
static_assert(mismatch1() == 6); // both-error {{not an integral constant expression}} \ |
| 163 |
// both-note {{in call to 'mismatch1()'}} |
| 164 |
|
| 165 |
constexpr int mismatch2() { // both-error {{never produces a constant expression}} |
| 166 |
int *i = new int[12]; // both-note {{allocated with 'new[]' here}} \ |
| 167 |
// both-note 2{{heap allocation performed here}} |
| 168 |
delete i; // both-warning {{'delete' applied to a pointer that was allocated with 'new[]'}} \ |
| 169 |
// both-note 2{{non-array delete used to delete pointer to array object of type 'int[12]'}} |
| 170 |
return 6; |
| 171 |
} |
| 172 |
static_assert(mismatch2() == 6); // both-error {{not an integral constant expression}} \ |
| 173 |
// both-note {{in call to 'mismatch2()'}} |
| 174 |
/// Array of composite elements. |
| 175 |
constexpr int foo() { |
| 176 |
S *ss = new S[12]; |
| 177 |
|
| 178 |
ss[0].a = 12; |
| 179 |
|
| 180 |
int m = ss[0].a; |
| 181 |
|
| 182 |
delete[] ss; |
| 183 |
return m; |
| 184 |
} |
| 185 |
static_assert(foo() == 12); |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
constexpr int ArrayInit() { |
| 190 |
auto array = new int[4]{0, 1, 2, 3}; |
| 191 |
int ret = array[0]; |
| 192 |
delete [] array; |
| 193 |
return ret; |
| 194 |
} |
| 195 |
static_assert(ArrayInit() == 0, ""); |
| 196 |
|
| 197 |
struct S { |
| 198 |
float F; |
| 199 |
}; |
| 200 |
constexpr float ArrayInit2() { |
| 201 |
auto array = new S[4]{}; |
| 202 |
float ret = array[0].F; |
| 203 |
delete [] array; |
| 204 |
return ret; |
| 205 |
} |
| 206 |
static_assert(ArrayInit2() == 0.0f, ""); |
| 207 |
} |
| 208 |
|
| 209 |
namespace std { |
| 210 |
struct type_info; |
| 211 |
struct destroying_delete_t { |
| 212 |
explicit destroying_delete_t() = default; |
| 213 |
} inline constexpr destroying_delete{}; |
| 214 |
struct nothrow_t { |
| 215 |
explicit nothrow_t() = default; |
| 216 |
} inline constexpr nothrow{}; |
| 217 |
using size_t = decltype(sizeof(0)); |
| 218 |
enum class align_val_t : size_t {}; |
| 219 |
}; |
| 220 |
|
| 221 |
[[nodiscard]] void *operator new(std::size_t, const std::nothrow_t&) noexcept; |
| 222 |
[[nodiscard]] void *operator new(std::size_t, std::align_val_t, const std::nothrow_t&) noexcept; |
| 223 |
[[nodiscard]] void *operator new[](std::size_t, const std::nothrow_t&) noexcept; |
| 224 |
[[nodiscard]] void *operator new[](std::size_t, std::align_val_t, const std::nothrow_t&) noexcept; |
| 225 |
[[nodiscard]] void *operator new[](std::size_t, std::align_val_t); |
| 226 |
void operator delete(void*, const std::nothrow_t&) noexcept; |
| 227 |
void operator delete(void*, std::align_val_t, const std::nothrow_t&) noexcept; |
| 228 |
void operator delete[](void*, const std::nothrow_t&) noexcept; |
| 229 |
void operator delete[](void*, std::align_val_t, const std::nothrow_t&) noexcept; |
| 230 |
|
| 231 |
struct placement_new_arg {}; |
| 232 |
void *operator new(std::size_t, placement_new_arg); |
| 233 |
void operator delete(void*, placement_new_arg); |
| 234 |
|
| 235 |
|
| 236 |
constexpr void *operator new(std::size_t, void *p) { return p; } |
| 237 |
namespace std { |
| 238 |
template<typename T> constexpr T *construct(T *p) { return new (p) T; } |
| 239 |
template<typename T> constexpr void destroy(T *p) { p->~T(); } |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
/// FIXME: The new interpreter produces the wrong diagnostic. |
| 245 |
namespace PlacementNew { |
| 246 |
constexpr int foo() { // both-error {{never produces a constant expression}} |
| 247 |
char c[sizeof(int)]; |
| 248 |
new (c) int{12}; // ref-note {{call to placement 'operator new'}} \ |
| 249 |
// expected-note {{subexpression not valid in a constant expression}} |
| 250 |
return 0; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
namespace NowThrowNew { |
| 255 |
constexpr bool erroneous_array_bound_nothrow(long long n) { |
| 256 |
int *p = new (std::nothrow) int[n]; |
| 257 |
bool result = p != nullptr; |
| 258 |
delete[] p; |
| 259 |
return result; |
| 260 |
} |
| 261 |
static_assert(erroneous_array_bound_nothrow(3)); |
| 262 |
static_assert(erroneous_array_bound_nothrow(0)); |
| 263 |
static_assert(erroneous_array_bound_nothrow(-1) == 0); |
| 264 |
static_assert(!erroneous_array_bound_nothrow(1LL << 62)); |
| 265 |
|
| 266 |
struct S { int a; }; |
| 267 |
constexpr bool erroneous_array_bound_nothrow2(long long n) { |
| 268 |
S *p = new (std::nothrow) S[n]; |
| 269 |
bool result = p != nullptr; |
| 270 |
delete[] p; |
| 271 |
return result; |
| 272 |
} |
| 273 |
/// This needs support for CXXConstrucExprs with non-constant array sizes. |
| 274 |
static_assert(erroneous_array_bound_nothrow2(3)); // expected-error {{not an integral constant expression}} |
| 275 |
static_assert(erroneous_array_bound_nothrow2(0));// expected-error {{not an integral constant expression}} |
| 276 |
static_assert(erroneous_array_bound_nothrow2(-1) == 0);// expected-error {{not an integral constant expression}} |
| 277 |
static_assert(!erroneous_array_bound_nothrow2(1LL << 62));// expected-error {{not an integral constant expression}} |
| 278 |
|
| 279 |
constexpr bool evaluate_nothrow_arg() { |
| 280 |
bool ok = false; |
| 281 |
delete new ((ok = true, std::nothrow)) int; |
| 282 |
return ok; |
| 283 |
} |
| 284 |
static_assert(evaluate_nothrow_arg()); |
| 285 |
} |
| 286 |
|
| 287 |
namespace placement_new_delete { |
| 288 |
struct ClassSpecificNew { |
| 289 |
void *operator new(std::size_t); |
| 290 |
}; |
| 291 |
struct ClassSpecificDelete { |
| 292 |
void operator delete(void*); |
| 293 |
}; |
| 294 |
struct DestroyingDelete { |
| 295 |
void operator delete(DestroyingDelete*, std::destroying_delete_t); |
| 296 |
}; |
| 297 |
struct alignas(64) Overaligned {}; |
| 298 |
|
| 299 |
constexpr bool ok() { |
| 300 |
delete new Overaligned; |
| 301 |
delete ::new ClassSpecificNew; |
| 302 |
::delete new ClassSpecificDelete; |
| 303 |
::delete new DestroyingDelete; |
| 304 |
return true; |
| 305 |
} |
| 306 |
static_assert(ok()); |
| 307 |
|
| 308 |
/// FIXME: Diagnosting placement new. |
| 309 |
constexpr bool bad(int which) { |
| 310 |
switch (which) { |
| 311 |
case 0: |
| 312 |
delete new (placement_new_arg{}) int; // ref-note {{call to placement 'operator new'}} \ |
| 313 |
// expected-note {{subexpression not valid in a constant expression}} |
| 314 |
break; |
| 315 |
|
| 316 |
case 1: |
| 317 |
delete new ClassSpecificNew; // ref-note {{call to class-specific 'operator new'}} |
| 318 |
break; |
| 319 |
|
| 320 |
case 2: |
| 321 |
delete new ClassSpecificDelete; // ref-note {{call to class-specific 'operator delete'}} |
| 322 |
break; |
| 323 |
|
| 324 |
case 3: |
| 325 |
delete new DestroyingDelete; // ref-note {{call to class-specific 'operator delete'}} |
| 326 |
break; |
| 327 |
|
| 328 |
case 4: |
| 329 |
// FIXME: This technically follows the standard's rules, but it seems |
| 330 |
// unreasonable to expect implementations to support this. |
| 331 |
delete new (std::align_val_t{64}) Overaligned; // ref-note {{placement new expression is not yet supported}} \ |
| 332 |
// expected-note {{subexpression not valid in a constant expression}} |
| 333 |
break; |
| 334 |
} |
| 335 |
|
| 336 |
return true; |
| 337 |
} |
| 338 |
static_assert(bad(0)); // both-error {{constant expression}} \ |
| 339 |
// both-note {{in call}} |
| 340 |
static_assert(bad(1)); // ref-error {{constant expression}} ref-note {{in call}} |
| 341 |
static_assert(bad(2)); // ref-error {{constant expression}} ref-note {{in call}} |
| 342 |
static_assert(bad(3)); // ref-error {{constant expression}} ref-note {{in call}} |
| 343 |
static_assert(bad(4)); // both-error {{constant expression}} \ |
| 344 |
// both-note {{in call}} |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
namespace delete_random_things { |
| 351 |
static_assert((delete new int, true)); |
| 352 |
static_assert((delete (int*)0, true)); |
| 353 |
int n; // both-note {{declared here}} |
| 354 |
static_assert((delete &n, true)); // both-error {{}} \ |
| 355 |
// both-note {{delete of pointer '&n' that does not point to a heap-allocated object}} |
| 356 |
struct A { int n; }; |
| 357 |
static_assert((delete &(new A)->n, true)); // both-error {{}} \ |
| 358 |
// both-note {{delete of pointer to subobject }} |
| 359 |
static_assert((delete (new int + 1), true)); // both-error {{}} \ |
| 360 |
// ref-note {{delete of pointer '&{*new int#0} + 1' that does not point to complete object}} \ |
| 361 |
// expected-note {{delete of pointer '&new int + 1' that does not point to complete object}} |
| 362 |
static_assert((delete[] (new int[3] + 1), true)); // both-error {{}} \ |
| 363 |
// both-note {{delete of pointer to subobject}} |
| 364 |
static_assert((delete &(int&)(int&&)0, true)); // both-error {{}} \ |
| 365 |
// both-note {{delete of pointer '&0' that does not point to a heap-allocated object}} \ |
| 366 |
// both-note {{temporary created here}} |
| 367 |
} |
| 368 |
|
| 369 |
namespace value_dependent_delete { |
| 370 |
template<typename T> void f(T *p) { |
| 371 |
int arr[(delete p, 0)]; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
namespace memory_leaks { |
| 376 |
static_assert(*new bool(true)); // both-error {{}} both-note {{allocation performed here was not deallocated}} |
| 377 |
|
| 378 |
constexpr bool *f() { return new bool(true); } // both-note {{allocation performed here was not deallocated}} |
| 379 |
static_assert(*f()); // both-error {{}} |
| 380 |
|
| 381 |
struct UP { |
| 382 |
bool *p; |
| 383 |
constexpr ~UP() { delete p; } |
| 384 |
constexpr bool &operator*() { return *p; } |
| 385 |
}; |
| 386 |
constexpr UP g() { return {new bool(true)}; } |
| 387 |
static_assert(*g()); // ok |
| 388 |
|
| 389 |
constexpr bool h(UP p) { return *p; } |
| 390 |
static_assert(h({new bool(true)})); // ok |
| 391 |
} |
| 392 |
|
| 393 |
/// From test/SemaCXX/cxx2a-consteval.cpp |
| 394 |
|
| 395 |
namespace std { |
| 396 |
template <typename T> struct remove_reference { using type = T; }; |
| 397 |
template <typename T> struct remove_reference<T &> { using type = T; }; |
| 398 |
template <typename T> struct remove_reference<T &&> { using type = T; }; |
| 399 |
template <typename T> |
| 400 |
constexpr typename std::remove_reference<T>::type&& move(T &&t) noexcept { |
| 401 |
return static_cast<typename std::remove_reference<T>::type &&>(t); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
namespace cxx2a { |
| 406 |
struct A { |
| 407 |
int* p = new int(42); // both-note 7{{heap allocation performed here}} |
| 408 |
consteval int ret_i() const { return p ? *p : 0; } |
| 409 |
consteval A ret_a() const { return A{}; } |
| 410 |
constexpr ~A() { delete p; } |
| 411 |
}; |
| 412 |
|
| 413 |
consteval int by_value_a(A a) { return a.ret_i(); } |
| 414 |
|
| 415 |
consteval int const_a_ref(const A &a) { |
| 416 |
return a.ret_i(); |
| 417 |
} |
| 418 |
|
| 419 |
consteval int rvalue_ref(const A &&a) { |
| 420 |
return a.ret_i(); |
| 421 |
} |
| 422 |
|
| 423 |
consteval const A &to_lvalue_ref(const A &&a) { |
| 424 |
return a; |
| 425 |
} |
| 426 |
|
| 427 |
void test() { |
| 428 |
constexpr A a{ nullptr }; |
| 429 |
{ int k = A().ret_i(); } |
| 430 |
|
| 431 |
{ A k = A().ret_a(); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \ |
| 432 |
// both-note {{heap-allocated object is not a constant expression}} |
| 433 |
{ A k = to_lvalue_ref(A()); } // both-error {{'cxx2a::to_lvalue_ref' is not a constant expression}} \ |
| 434 |
// both-note {{reference to temporary is not a constant expression}} \ |
| 435 |
// both-note {{temporary created here}} |
| 436 |
{ A k = to_lvalue_ref(A().ret_a()); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \ |
| 437 |
// both-note {{heap-allocated object is not a constant expression}} \ |
| 438 |
// both-error {{'cxx2a::to_lvalue_ref' is not a constant expression}} \ |
| 439 |
// both-note {{reference to temporary is not a constant expression}} \ |
| 440 |
// both-note {{temporary created here}} |
| 441 |
{ int k = A().ret_a().ret_i(); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \ |
| 442 |
// both-note {{heap-allocated object is not a constant expression}} |
| 443 |
{ int k = by_value_a(A()); } |
| 444 |
{ int k = const_a_ref(A()); } |
| 445 |
{ int k = const_a_ref(a); } |
| 446 |
{ int k = rvalue_ref(A()); } |
| 447 |
{ int k = rvalue_ref(std::move(a)); } |
| 448 |
{ int k = const_a_ref(A().ret_a()); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \ |
| 449 |
// both-note {{is not a constant expression}} |
| 450 |
{ int k = const_a_ref(to_lvalue_ref(A().ret_a())); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \ |
| 451 |
// both-note {{is not a constant expression}} |
| 452 |
{ int k = const_a_ref(to_lvalue_ref(std::move(a))); } |
| 453 |
{ int k = by_value_a(A().ret_a()); } |
| 454 |
{ int k = by_value_a(to_lvalue_ref(static_cast<const A&&>(a))); } |
| 455 |
{ int k = (A().ret_a(), A().ret_i()); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \ |
| 456 |
// both-note {{is not a constant expression}} \ |
| 457 |
// both-warning {{left operand of comma operator has no effect}} |
| 458 |
{ int k = (const_a_ref(A().ret_a()), A().ret_i()); } // both-error {{'cxx2a::A::ret_a' is not a constant expression}} \ |
| 459 |
// both-note {{is not a constant expression}} \ |
| 460 |
// both-warning {{left operand of comma operator has no effect}} |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
constexpr int *const &p = new int; // both-error {{must be initialized by a constant expression}} \ |
| 465 |
// both-note {{pointer to heap-allocated object}} \ |
| 466 |
// both-note {{allocation performed here}} |
| 467 |
|
| 468 |
constexpr const int *A[] = {nullptr, nullptr, new int{12}}; // both-error {{must be initialized by a constant expression}} \ |
| 469 |
// both-note {{pointer to heap-allocated object}} \ |
| 470 |
// both-note {{allocation performed here}} |
| 471 |
|
| 472 |
struct Sp { |
| 473 |
const int *p; |
| 474 |
}; |
| 475 |
constexpr Sp ss[] = {Sp{new int{154}}}; // both-error {{must be initialized by a constant expression}} \ |
| 476 |
// both-note {{pointer to heap-allocated object}} \ |
| 477 |
// both-note {{allocation performed here}} |
| 478 |
|
| 479 |
namespace DeleteRunsDtors { |
| 480 |
struct InnerFoo { |
| 481 |
int *mem; |
| 482 |
constexpr ~InnerFoo() { |
| 483 |
delete mem; |
| 484 |
} |
| 485 |
}; |
| 486 |
|
| 487 |
struct Foo { |
| 488 |
int *a; |
| 489 |
InnerFoo IF; |
| 490 |
|
| 491 |
constexpr Foo() { |
| 492 |
a = new int(13); |
| 493 |
IF.mem = new int(100); |
| 494 |
} |
| 495 |
constexpr ~Foo() { delete a; } |
| 496 |
}; |
| 497 |
|
| 498 |
constexpr int abc() { |
| 499 |
Foo *F = new Foo(); |
| 500 |
int n = *F->a; |
| 501 |
delete F; |
| 502 |
|
| 503 |
return n; |
| 504 |
} |
| 505 |
static_assert(abc() == 13); |
| 506 |
|
| 507 |
constexpr int abc2() { |
| 508 |
Foo *f = new Foo[3]; |
| 509 |
|
| 510 |
delete[] f; |
| 511 |
|
| 512 |
return 1; |
| 513 |
} |
| 514 |
static_assert(abc2() == 1); |
| 515 |
} |
| 516 |
|
| 517 |
/// FIXME: There is a slight difference in diagnostics here, because we don't |
| 518 |
/// create a new frame when we delete record fields or bases at all. |
| 519 |
namespace FaultyDtorCalledByDelete { |
| 520 |
struct InnerFoo { |
| 521 |
int *mem; |
| 522 |
constexpr ~InnerFoo() { |
| 523 |
if (mem) { |
| 524 |
(void)(1/0); // both-warning {{division by zero is undefined}} \ |
| 525 |
// both-note {{division by zero}} |
| 526 |
} |
| 527 |
delete mem; |
| 528 |
} |
| 529 |
}; |
| 530 |
|
| 531 |
struct Foo { |
| 532 |
int *a; |
| 533 |
InnerFoo IF; |
| 534 |
|
| 535 |
constexpr Foo() { |
| 536 |
a = new int(13); |
| 537 |
IF.mem = new int(100); |
| 538 |
} |
| 539 |
constexpr ~Foo() { delete a; } |
| 540 |
}; |
| 541 |
|
| 542 |
constexpr int abc() { |
| 543 |
Foo *F = new Foo(); |
| 544 |
int n = *F->a; |
| 545 |
delete F; // both-note {{in call to}} \ |
| 546 |
// ref-note {{in call to}} |
| 547 |
|
| 548 |
return n; |
| 549 |
} |
| 550 |
static_assert(abc() == 13); // both-error {{not an integral constant expression}} \ |
| 551 |
// both-note {{in call to 'abc()'}} |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
#else |
| 556 |
/// Make sure we reject this prior to C++20 |
| 557 |
constexpr int a() { // both-error {{never produces a constant expression}} |
| 558 |
delete new int(12); // both-note 2{{dynamic memory allocation is not permitted in constant expressions until C++20}} |
| 559 |
return 1; |
| 560 |
} |
| 561 |
static_assert(a() == 1, ""); // both-error {{not an integral constant expression}} \ |
| 562 |
// both-note {{in call to 'a()'}} |
| 563 |
|
| 564 |
|
| 565 |
static_assert(true ? *new int : 4, ""); // both-error {{expression is not an integral constant expression}} \ |
| 566 |
// both-note {{read of uninitialized object is not allowed in a constant expression}} |
| 567 |
|
| 568 |
#endif |