Debian Codegraph @

clang/test/CXX/drs/cwg27xx.cpp

raw
1 // RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++98 -pedantic-errors -verify=expected,cxx98 %s
2 // RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++11 -pedantic-errors -verify=expected %s
3 // RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++14 -pedantic-errors -verify=expected %s
4 // RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++17 -pedantic-errors -verify=expected %s
5 // RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++20 -pedantic-errors -verify=expected,since-cxx20 %s
6 // RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -pedantic-errors -verify=expected,since-cxx20,since-cxx23 %s
7 // RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++2c -pedantic-errors -verify=expected,since-cxx20,since-cxx23,since-cxx26 %s
8
9 #if __cplusplus == 199711L
10 #define static_assert(...) __extension__ _Static_assert(__VA_ARGS__)
11 // cxx98-error@-1 {{variadic macros are a C99 feature}}
12 #endif
13
14 #if __cplusplus == 199711L
15 #define __enable_constant_folding(x) (__builtin_constant_p(x) ? (x) : (x))
16 #else
17 #define __enable_constant_folding
18 #endif
19
20 namespace std {
21 #if __cplusplus >= 202002L
22 struct strong_ordering {
23 int n;
24 constexpr operator int() const { return n; }
25 static const strong_ordering less, equal, greater;
26 };
27 constexpr strong_ordering strong_ordering::less{-1},
28 strong_ordering::equal{0}, strong_ordering::greater{1};
29 #endif
30 } // namespace std
31
32 namespace cwg2707 { // cwg2707: 20
33
34 #if __cplusplus >= 202002L
35
36 template <class T, unsigned N> struct A { // #cwg2707-A
37 T value[N];
38 };
39
40 template <typename... T>
41 A(T...) -> A<int, sizeof...(T)> requires (sizeof...(T) == 2); // #cwg2707-guide-A
42
43 // Brace elision is not allowed for synthesized CTAD guides if the array size
44 // is value-dependent.
45 // So this should pick up our explicit deduction guide.
46 A a = {1, 2};
47
48 A b = {3, 4, 5};
49 // since-cxx20-error@-1 {{no viable constructor or deduction guide}}
50 // since-cxx20-note@#cwg2707-A {{candidate function template not viable}}
51 // since-cxx20-note@#cwg2707-A {{implicit deduction guide}}
52 // since-cxx20-note@#cwg2707-guide-A {{constraints not satisfied}}
53 // since-cxx20-note@#cwg2707-guide-A {{because 'sizeof...(T) == 2' (3 == 2) evaluated to false}}
54 // since-cxx20-note@#cwg2707-A {{candidate function template not viable}}
55 // since-cxx20-note@#cwg2707-A {{implicit deduction guide}}
56
57 #endif
58
59 } // namespace cwg2707
60
61 namespace cwg2718 { // cwg2718: 2.7
62 struct B {};
63 struct D;
64
65 void f(B b) {
66 static_cast<D&>(b);
67 // expected-error@-1 {{non-const lvalue reference to type 'D' cannot bind to a value of unrelated type 'B'}}
68 }
69
70 struct D : B {};
71 } // namespace cwg2718
72
73 namespace cwg2749 { // cwg2749: 20
74
75 extern int x[2];
76 struct Y {
77 int i;
78 int j;
79 };
80 extern Y y[2];
81
82 static_assert(__enable_constant_folding(static_cast<void*>(&x[0]) < static_cast<void*>(&x[1])), "");
83 static_assert(__enable_constant_folding(static_cast<void*>(&y[0].i) < static_cast<void*>(&y[0].j)), "");
84 static_assert(__enable_constant_folding(static_cast<void*>(&y[0].j) < static_cast<void*>(&y[1].i)), "");
85
86 #if __cplusplus >= 202002L
87 static_assert((static_cast<void*>(&x[0]) <=> static_cast<void*>(&x[1])) == std::strong_ordering::less);
88 static_assert((static_cast<void*>(&y[0].i) <=> static_cast<void*>(&y[0].j)) == std::strong_ordering::less);
89 static_assert((static_cast<void*>(&y[0].j) <=> static_cast<void*>(&y[1].i)) == std::strong_ordering::less);
90 #endif
91
92 } // namespace cwg2749
93
94 namespace cwg2759 { // cwg2759: 19
95 #if __cplusplus >= 201103L
96
97 struct CStruct {
98 int one;
99 int two;
100 };
101
102 struct CEmptyStruct {};
103 struct CEmptyStruct2 {};
104
105 struct CStructNoUniqueAddress {
106 int one;
107 [[no_unique_address]] int two;
108 };
109
110 struct CStructNoUniqueAddress2 {
111 int one;
112 [[no_unique_address]] int two;
113 };
114
115 union UnionLayout {
116 int a;
117 double b;
118 CStruct c;
119 [[no_unique_address]] CEmptyStruct d;
120 [[no_unique_address]] CEmptyStruct2 e;
121 };
122
123 union UnionLayout2 {
124 CStruct c;
125 int a;
126 CEmptyStruct2 e;
127 double b;
128 [[no_unique_address]] CEmptyStruct d;
129 };
130
131 union UnionLayout3 {
132 CStruct c;
133 int a;
134 double b;
135 [[no_unique_address]] CEmptyStruct d;
136 };
137
138 struct StructWithAnonUnion {
139 union {
140 int a;
141 double b;
142 CStruct c;
143 [[no_unique_address]] CEmptyStruct d;
144 [[no_unique_address]] CEmptyStruct2 e;
145 };
146 };
147
148 struct StructWithAnonUnion2 {
149 union {
150 CStruct c;
151 int a;
152 CEmptyStruct2 e;
153 double b;
154 [[no_unique_address]] CEmptyStruct d;
155 };
156 };
157
158 struct StructWithAnonUnion3 {
159 union {
160 CStruct c;
161 int a;
162 CEmptyStruct2 e;
163 double b;
164 [[no_unique_address]] CEmptyStruct d;
165 } u;
166 };
167
168 static_assert(__is_layout_compatible(CStruct, CStructNoUniqueAddress) != bool(__has_cpp_attribute(no_unique_address)), "");
169 static_assert(__is_layout_compatible(CStructNoUniqueAddress, CStructNoUniqueAddress2) != bool(__has_cpp_attribute(no_unique_address)), "");
170 static_assert(!__is_layout_compatible(UnionLayout, UnionLayout2), "");
171 static_assert(!__is_layout_compatible(UnionLayout, UnionLayout3), "");
172 static_assert(!__is_layout_compatible(StructWithAnonUnion, StructWithAnonUnion2), "");
173 static_assert(!__is_layout_compatible(StructWithAnonUnion, StructWithAnonUnion3), "");
174 #endif
175 } // namespace cwg2759
176
177 namespace cwg2770 { // cwg2770: 20 open 2023-07-14
178 #if __cplusplus >= 202002L
179 template<typename T>
180 struct B {
181 static_assert(sizeof(T) == 1);
182 using type = int;
183 };
184
185 template<typename T>
186 int f(T t, typename B<T>::type u) requires (sizeof(t) == 1);
187
188 template<typename T>
189 int f(T t, long);
190
191 int i = f(1, 2);
192 int j = f('a', 2);
193
194 #endif
195 } // namespace cwg2770
196
197 namespace cwg2789 { // cwg2789: 18
198 #if __cplusplus >= 202302L
199 template <typename T = int>
200 struct Base {
201 constexpr void g(); // #cwg2789-g1
202 };
203
204 template <typename T = int>
205 struct Base2 {
206 constexpr void g() requires true; // #cwg2789-g2
207 };
208
209 template <typename T = int>
210 struct S : Base<T>, Base2<T> {
211 constexpr void f();
212 constexpr void f(this S&) requires true{};
213
214 using Base<T>::g;
215 using Base2<T>::g;
216 };
217
218 void test() {
219 S<> s;
220 s.f();
221 s.g();
222 // since-cxx23-error@-1 {{call to member function 'g' is ambiguous}}
223 // since-cxx23-note@#cwg2789-g1 {{candidate function}}
224 // since-cxx23-note@#cwg2789-g2 {{candidate function}}
225 }
226 #endif
227 } // namespace cwg2789
228
229 namespace cwg2798 { // cwg2798: 17
230 #if __cplusplus > 202302L
231 struct string {
232 constexpr string() {
233 data_ = new char[6]();
234 __builtin_memcpy(data_, "Hello", 5);
235 data_[5] = 0;
236 }
237 constexpr ~string() { delete[] data_; }
238 constexpr unsigned long size() const { return 5; };
239 constexpr const char *data() const { return data_; }
240
241 char *data_;
242 };
243 struct X {
244 string s;
245 };
246 consteval X f() { return {}; }
247
248 static_assert(false, f().s);
249 // since-cxx26-error@-1 {{static assertion failed: Hello}}
250 #endif
251 } // namespace cwg2798