Debian Codegraph @

clang/test/SemaCXX/type-aware-new-constexpr.cpp

raw
1 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fsized-deallocation -faligned-allocation
2 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fno-sized-deallocation -faligned-allocation
3 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fsized-deallocation -fno-aligned-allocation
4 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fno-sized-deallocation -fno-aligned-allocation
5 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fsized-deallocation -faligned-allocation -fexperimental-new-constant-interpreter
6 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fno-sized-deallocation -faligned-allocation -fexperimental-new-constant-interpreter
7 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fsized-deallocation -fno-aligned-allocation -fexperimental-new-constant-interpreter
8 // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++26 -Wno-ext-cxx-type-aware-allocators -fexceptions -fno-sized-deallocation -fno-aligned-allocation -fexperimental-new-constant-interpreter
9
10
11 namespace std {
12 template <class T> struct type_identity {};
13 enum class align_val_t : __SIZE_TYPE__ {};
14 struct destroying_delete_t { explicit destroying_delete_t() = default; };
15 }
16
17 using size_t = __SIZE_TYPE__;
18
19 struct S1 {
20 constexpr explicit S1() : i(5) { }
21 const int i;
22 };
23
24 void *operator new(std::type_identity<S1>, size_t sz, std::align_val_t); // #1
25 void operator delete(std::type_identity<S1>, void* ptr, size_t sz, std::align_val_t); // #2
26
27 constexpr int ensure_consteval_skips_typed_allocators() {
28 // Verify we dont resolve typed allocators in const contexts
29 auto * s = new S1();
30 auto result = s->i;
31 delete s;
32 return result;
33 };
34
35 struct S2 {
36 constexpr explicit S2() : i(5) { }
37 const int i;
38 };
39
40 void *operator new(std::type_identity<S2>, size_t sz, std::align_val_t) = delete; // #3
41 void operator delete(std::type_identity<S2>, void* ptr, size_t sz, std::align_val_t) = delete; // #4
42
43 constexpr int ensure_constexpr_retains_types_at_runtime() {
44 // Verify we dont resolve typed allocators in const contexts
45 S2 *s = new S2();
46 // expected-error@-1 {{call to deleted function 'operator new'}}
47 // expected-note@#1 {{candidate function not viable: no known conversion from 'type_identity<S2>' to 'type_identity<S1>' for 1st argument}}
48 // expected-note@#3 {{candidate function has been explicitly deleted}}
49 auto result = s->i;
50 delete s;
51 // expected-error@-1 {{attempt to use a deleted function}}
52 // expected-note@#4 {{'operator delete' has been explicitly marked deleted here}}
53 return result;
54 };
55
56
57 struct S3 {
58 constexpr explicit S3() : i(5) { }
59 const int i;
60 template <typename T> void* operator new(std::type_identity<T>, size_t sz, std::align_val_t) = delete; // #5
61 template <typename T> void operator delete(std::type_identity<T>, void *, size_t sz, std::align_val_t) = delete; // #6
62 };
63
64 template <typename T> void* operator new(std::type_identity<T>, size_t sz, std::align_val_t) = delete; // #7
65 template <typename T> void operator delete(std::type_identity<T>, void *, size_t sz, std::align_val_t) = delete; // #8
66
67 constexpr int constexpr_vs_inclass_operators() {
68 S3 *s;
69 if consteval {
70 s = ::new S3();
71 // expected-error@-1 {{call to deleted function 'operator new'}}
72 // expected-note@#1 {{candidate function not viable: no known conversion from 'type_identity<S3>' to 'type_identity<S1>' for 1st argument}}
73 // expected-note@#3 {{candidate function not viable: no known conversion from 'type_identity<S3>' to 'type_identity<S2>' for 1st argument}}
74 // expected-note@#7 {{candidate function [with T = S3] has been explicitly deleted}}
75 } else {
76 s = new S3();
77 // expected-error@-1 {{call to deleted function 'operator new'}}
78 // expected-note@#5 {{candidate function [with T = S3] has been explicitly deleted}}
79 }
80 auto result = s->i;
81 if consteval {
82 ::delete s;
83 // expected-error@-1 {{attempt to use a deleted function}}
84 // expected-note@#8 {{'operator delete<S3>' has been explicitly marked deleted here}}
85 } else {
86 delete s;
87 // expected-error@-1 {{attempt to use a deleted function}}
88 // expected-note@#6 {{'operator delete<S3>' has been explicitly marked deleted here}}
89 }
90 return result;
91 };
92
93 // Test a variety of valid constant evaluation paths
94 struct S4 {
95 int i = 1;
96 constexpr S4() __attribute__((noinline)) {}
97 };
98
99 void* operator new(std::type_identity<S4>, size_t sz, std::align_val_t);
100 void operator delete(std::type_identity<S4>, void *, size_t sz, std::align_val_t);
101
102 constexpr int do_dynamic_alloc(int n) {
103 S4* s = new S4;
104 int result = n * s->i;
105 delete s;
106 return result;
107 }
108
109 template <int N> struct Tag {
110 };
111
112 static constexpr int force_do_dynamic_alloc = do_dynamic_alloc(5);
113
114 constexpr int test_consteval_calling_constexpr(int i) {
115 if consteval {
116 return do_dynamic_alloc(2 * i);
117 }
118 return do_dynamic_alloc(3 * i);
119 }
120
121 int test_consteval(int n, Tag<test_consteval_calling_constexpr(2)>, Tag<do_dynamic_alloc(3)>) {
122 static const int t1 = test_consteval_calling_constexpr(4);
123 static const int t2 = do_dynamic_alloc(5);
124 int t3 = test_consteval_calling_constexpr(6);
125 int t4 = do_dynamic_alloc(7);
126 return t1 * t2 * t3 * t4;
127 }