Debian Codegraph @

clang/test/AST/ByteCode/lifetimes26.cpp

raw
1 // RUN: %clang_cc1 -verify=expected,both -std=c++26 %s -fexperimental-new-constant-interpreter
2 // RUN: %clang_cc1 -verify=ref,both -std=c++26 %s
3
4 // both-no-diagnostics
5
6 namespace std {
7 struct type_info;
8 struct destroying_delete_t {
9 explicit destroying_delete_t() = default;
10 } inline constexpr destroying_delete{};
11 struct nothrow_t {
12 explicit nothrow_t() = default;
13 } inline constexpr nothrow{};
14 using size_t = decltype(sizeof(0));
15 enum class align_val_t : size_t {};
16 };
17
18 constexpr void *operator new(std::size_t, void *p) { return p; }
19 namespace std {
20 template<typename T> constexpr T *construct_at(T *p) { return new (p) T; }
21 template<typename T> constexpr void destroy_at(T *p) { p->~T(); }
22 }
23
24 constexpr bool foo() {
25 using T = bool;
26 bool b = true;
27 b.~T();
28 new (&b) bool(false);
29 return b;
30 }
31 static_assert(!foo());
32
33 struct S {};
34 constexpr bool foo2() {
35 S s;
36 s.~S();
37 new (&s) S{};
38 return true;
39 }
40 static_assert(foo2());
41
42 constexpr void destroy_pointer() {
43 using T = int*;
44 T p;
45 p.~T();
46 std::construct_at(&p);
47 }
48 static_assert((destroy_pointer(), true));
49
50
51 namespace DestroyArrayElem {
52 /// This is proof that std::destroy_at'ing an array element
53 /// ends the lifetime of the entire array.
54 /// See https://github.com/llvm/llvm-project/issues/147528
55 /// Using destroy_at on array elements is currently a no-op due to this.
56 constexpr int test() {
57 int a[4] = {};
58
59 std::destroy_at(&a[3]);
60 int r = a[1];
61 std::construct_at(&a[3]);
62
63 return r;
64 }
65 static_assert(test() == 0);
66 }