xref: /llvm-project/clang/test/CXX/drs/cwg27xx.cpp (revision 3972ed57088f6515b787d7d38dec03dc74e51827)
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 cwg2789 { // cwg2789: 18
178 #if __cplusplus >= 202302L
179 template <typename T = int>
180 struct Base {
181     constexpr void g(); // #cwg2789-g1
182 };
183 
184 template <typename T = int>
185 struct Base2 {
186     constexpr void g() requires true;  // #cwg2789-g2
187 };
188 
189 template <typename T = int>
190 struct S : Base<T>, Base2<T> {
191     constexpr void f();
192     constexpr void f(this S&) requires true{};
193 
194     using Base<T>::g;
195     using Base2<T>::g;
196 };
197 
198 void test() {
199     S<> s;
200     s.f();
201     s.g();
202     // since-cxx23-error@-1 {{call to member function 'g' is ambiguous}}
203     //   since-cxx23-note@#cwg2789-g1 {{candidate function}}
204     //   since-cxx23-note@#cwg2789-g2 {{candidate function}}
205 }
206 #endif
207 } // namespace cwg2789
208 
209 namespace cwg2798 { // cwg2798: 17
210 #if __cplusplus > 202302L
211 struct string {
212   constexpr string() {
213     data_ = new char[6]();
214     __builtin_memcpy(data_, "Hello", 5);
215     data_[5] = 0;
216   }
217   constexpr ~string() { delete[] data_; }
218   constexpr unsigned long size() const { return 5; };
219   constexpr const char *data() const { return data_; }
220 
221   char *data_;
222 };
223 struct X {
224   string s;
225 };
226 consteval X f() { return {}; }
227 
228 static_assert(false, f().s);
229 // since-cxx26-error@-1 {{static assertion failed: Hello}}
230 #endif
231 } // namespace cwg2798
232