Debian Codegraph @

clang/test/CXX/drs/cwg23xx.cpp

raw
1 // RUN: %clang_cc1 -std=c++98 %s -verify=expected -fexceptions -fcxx-exceptions -pedantic-errors 2>&1 | FileCheck %s
2 // RUN: %clang_cc1 -std=c++11 %s -verify=expected,cxx11-14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors 2>&1 | FileCheck %s
3 // RUN: %clang_cc1 -std=c++14 %s -verify=expected,cxx11-14,since-cxx11,since-cxx14 -fexceptions -fcxx-exceptions -pedantic-errors 2>&1 | FileCheck %s
4 // RUN: %clang_cc1 -std=c++17 %s -verify=expected,since-cxx11,since-cxx14,since-cxx17 -fexceptions -fcxx-exceptions -pedantic-errors 2>&1 | FileCheck %s
5 // RUN: %clang_cc1 -std=c++20 %s -verify=expected,since-cxx11,since-cxx14,since-cxx17,since-cxx20 -fexceptions -fcxx-exceptions -pedantic-errors 2>&1 | FileCheck %s
6 // RUN: %clang_cc1 -std=c++23 %s -verify=expected,since-cxx11,since-cxx14,since-cxx17,since-cxx20 -fexceptions -fcxx-exceptions -pedantic-errors 2>&1 | FileCheck %s
7 // RUN: %clang_cc1 -std=c++2c %s -verify=expected,since-cxx11,since-cxx14,since-cxx17,since-cxx20 -fexceptions -fcxx-exceptions -pedantic-errors 2>&1 | FileCheck %s
8
9 #if __cplusplus >= 201103L
10 namespace cwg2303 { // cwg2303: 12
11 template <typename... T>
12 struct A;
13 template <>
14 struct A<> {};
15 template <typename T, typename... Ts>
16 struct A<T, Ts...> : A<Ts...> {};
17 struct B : A<int, int> {};
18 struct C : A<int, int>, A<int> {};
19 /* since-cxx11-warning@-1 {{direct base 'A<int>' is inaccessible due to ambiguity:
20 struct cwg2303::C -> A<int, int> -> A<int>
21 struct cwg2303::C -> A<int>}} */
22 struct D : A<int>, A<int, int> {};
23 /* since-cxx11-warning@-1 {{direct base 'A<int>' is inaccessible due to ambiguity:
24 struct cwg2303::D -> A<int>
25 struct cwg2303::D -> A<int, int> -> A<int>}} */
26 struct E : A<int, int> {};
27 struct F : B, E {};
28
29 template <typename... T>
30 void f(const A<T...> &) {
31 static_assert(sizeof...(T) == 2, "Should only match A<int,int>");
32 }
33 template <typename... T>
34 void f2(const A<T...> *);
35
36 void g() {
37 f(B{}); // This is no longer ambiguous.
38 B b;
39 f2(&b);
40 f(C{});
41 f(D{});
42 f(F{});
43 /* since-cxx11-error@-1 {{ambiguous conversion from derived class 'const F' to base class 'const A<int, int>':
44 struct cwg2303::F -> B -> A<int, int>
45 struct cwg2303::F -> E -> A<int, int>}} */
46 }
47 } // namespace cwg2303
48 #endif
49
50 namespace cwg2304 { // cwg2304: 2.8
51 template<typename T> void foo(T, int);
52 template<typename T> void foo(T&, ...);
53 struct Q; // #cwg2304-Q
54 void fn1(Q &data_vectors) {
55 foo(data_vectors, 0);
56 // expected-error@-1 {{argument type 'cwg2304::Q' is incomplete}}
57 // expected-note@#cwg2304-Q {{forward declaration of 'cwg2304::Q'}}
58 }
59 } // namespace cwg2304
60
61 namespace cwg2310 { // cwg2310: partial
62 #if __cplusplus >= 201103L
63 template<typename A, typename B>
64 struct check_derived_from {
65 static A a;
66 // FIXME: all 3 examples should be rejected in all language modes.
67 // FIXME: we should test this in 98 mode.
68 // FIXME: we accept this when MSVC triple is used
69 static constexpr B *p = &a;
70 #if !defined(_WIN32) || defined(__MINGW32__)
71 // cxx11-14-error@-2 {{cannot initialize a variable of type 'cwg2310::X *const' with an rvalue of type 'cwg2310::Z *'}}
72 // cxx11-14-note@#cwg2310-X {{in instantiation of template class 'cwg2310::check_derived_from<cwg2310::Z, cwg2310::X>' requested here}}
73 // cxx11-14-error@-4 {{cannot initialize a variable of type 'cwg2310::Y *const' with an rvalue of type 'cwg2310::Z *'}}
74 // cxx11-14-note@#cwg2310-Y {{in instantiation of template class 'cwg2310::check_derived_from<cwg2310::Z, cwg2310::Y>' requested here}}
75 #endif
76 };
77
78 struct W {};
79 struct X {};
80 struct Y {};
81 struct Z : W,
82 X, check_derived_from<Z, X>, // #cwg2310-X
83 check_derived_from<Z, Y>, Y // #cwg2310-Y
84 {
85 // FIXME: It was properly rejected before, but we're crashing since Clang 11 in C++11 and C++14 modes.
86 // See https://github.com/llvm/llvm-project/issues/59920
87 #if __cplusplus >= 201703L
88 check_derived_from<Z, W> cdf;
89 #endif
90 };
91 #endif
92 } // namespace cwg2310
93
94 // cwg2331: na
95 // cwg2335 is in cwg2335.cxx
96
97 #if __cplusplus >= 201103L
98 namespace cwg2338 { // cwg2338: 12
99 namespace B {
100 enum E : bool { Zero, One };
101 static_assert((int)(E)2 == 1, "");
102 } // namespace B
103 namespace D {
104 enum class E : bool { Zero, One };
105 static_assert((int)(E)2 == 1, "");
106 } // namespace D
107 } // namespace cwg2338
108 #endif
109
110 namespace cwg2346 { // cwg2346: 11
111 void test() {
112 const int i2 = 0;
113 extern void h2b(int x = i2 + 0); // ok, not odr-use
114 }
115 }
116
117 namespace cwg2352 { // cwg2352: 10
118 int **p;
119 const int *const *const &f1() { return p; }
120 int *const *const &f2() { return p; }
121 int **const &f3() { return p; }
122
123 const int **const &f4() { return p; }
124 // expected-error@-1 {{reference to type 'const int **const' could not bind to an lvalue of type 'int **'}}
125 const int *const *&f5() { return p; }
126 // expected-error@-1 {{binding reference of type 'const int *const *' to value of type 'int **' not permitted due to incompatible qualifiers}}
127
128 // FIXME: We permit this as a speculative defect resolution, allowing
129 // qualification conversions when forming a glvalue conditional expression.
130 const int * const * const q = 0;
131 __typeof(&(true ? p : q)) x = &(true ? p : q);
132
133 // FIXME: Should we compute the composite pointer type here and produce an
134 // lvalue of type 'const int *const * const'?
135 const int * const * r;
136 void *y = &(true ? p : r);
137 // expected-error@-1 {{rvalue of type 'const int *const *'}}
138
139 // FIXME: We order these as a speculative defect resolution.
140 void f(const int * const * const &r);
141 #if __cplusplus >= 201103L
142 constexpr
143 #endif
144 int *const *const &f(int * const * const &r) { return r; }
145
146 // No temporary is created here.
147 int *const *const &check_f = f(p);
148 #if __cplusplus >= 201103L
149 static_assert(&p == &check_f, "");
150 #endif
151 }
152
153 namespace cwg2353 { // cwg2353: 9
154 struct X {
155 static const int n = 0;
156 };
157
158 // CHECK: FunctionDecl {{.*}} use
159 int use(X x) {
160 // CHECK: MemberExpr {{.*}} .n
161 // CHECK-NOT: non_odr_use
162 // CHECK: DeclRefExpr {{.*}} 'x'
163 // CHECK-NOT: non_odr_use
164 return *&x.n;
165 }
166 #pragma clang __debug dump use
167
168 // CHECK: FunctionDecl {{.*}} not_use
169 int not_use(X x) {
170 // CHECK: MemberExpr {{.*}} .n {{.*}} non_odr_use_constant
171 // CHECK: DeclRefExpr {{.*}} 'x'
172 return x.n;
173 }
174 #pragma clang __debug dump not_use
175
176 // CHECK: FunctionDecl {{.*}} not_use_2
177 int not_use_2(X *x) {
178 // CHECK: MemberExpr {{.*}} ->n {{.*}} non_odr_use_constant
179 // CHECK: DeclRefExpr {{.*}} 'x'
180 return x->n;
181 }
182 #pragma clang __debug dump not_use_2
183 }
184
185 namespace cwg2354 { // cwg2354: 15
186 #if __cplusplus >= 201103L
187 enum alignas(64) A {};
188 // since-cxx11-error@-1 {{'alignas' attribute cannot be applied to an enumeration}}
189 enum struct alignas(64) B {};
190 // since-cxx11-error@-1 {{'alignas' attribute cannot be applied to an enumeration}}
191 #endif
192 } // namespace cwg2354
193
194 namespace cwg2356 { // cwg2356: 4
195 #if __cplusplus >= 201103L
196 struct A {
197 A();
198 A(A &&); // #1
199 template<typename T> A(T &&); // #2
200 };
201 struct B : A {
202 using A::A;
203 B(const B &); // #3
204 B(B &&) = default; // #4, implicitly deleted
205 // since-cxx11-warning@-1 {{explicitly defaulted move constructor is implicitly deleted}}
206 // since-cxx11-note@#cwg2356-X {{move constructor of 'B' is implicitly deleted because field 'x' has a deleted move constructor}}
207 // since-cxx11-note@#cwg2356-X {{'X' has been explicitly marked deleted here}}
208 // since-cxx11-note@-4 {{replace 'default' with 'delete'}}
209
210 struct X { X(X &&) = delete; } x; // #cwg2356-X
211 };
212 extern B b1;
213 B b2 = static_cast<B&&>(b1); // calls #3: #1, #2, and #4 are not viable
214 struct C { operator B&&(); };
215 B b3 = C(); // calls #3
216 #endif
217 }
218
219 #if __cplusplus >= 201402L
220 namespace cwg2358 { // cwg2358: 16
221 void f2() {
222 int i = 1;
223 void g1(int = [xxx=1] { return xxx; }()); // OK
224 void g2(int = [xxx=i] { return xxx; }());
225 // since-cxx14-error@-1 {{default argument references local variable 'i' of enclosing function}}
226 }
227 }
228 #endif
229
230 // CWG2363 was closed as NAD, but its resolution does affirm that
231 // a friend declaration cannot have an opaque-enumm-specifier.
232 namespace cwg2363 { // cwg2363: yes
233 #if __cplusplus >= 201103L
234 enum class E0;
235 enum E1 : int;
236
237 struct A {
238 friend enum class E0;
239 // since-cxx11-error@-1 {{reference to enumeration must use 'enum' not 'enum class'}}
240 // expected-error@-2 {{elaborated enum specifier cannot be declared as a friend}}
241 // expected-note@-3 {{remove 'enum class' to befriend an enum}}
242
243 friend enum E0;
244 // expected-error@-1 {{elaborated enum specifier cannot be declared as a friend}}
245 // expected-note@-2 {{remove 'enum' to befriend an enum}}
246
247 friend enum class E1;
248 // since-cxx11-error@-1 {{reference to enumeration must use 'enum' not 'enum class'}}
249 // expected-error@-2 {{elaborated enum specifier cannot be declared as a friend}}
250 // expected-note@-3 {{remove 'enum class' to befriend an enum}}
251
252 friend enum E1;
253 // expected-error@-1 {{elaborated enum specifier cannot be declared as a friend}}
254 // expected-note@-2 {{remove 'enum' to befriend an enum}}
255
256 friend enum class E2;
257 // since-cxx11-error@-1 {{reference to enumeration must use 'enum' not 'enum class'}}
258 // expected-error@-2 {{elaborated enum specifier cannot be declared as a friend}}
259 // expected-note@-3 {{remove 'enum class' to befriend an enum}}
260 };
261 #endif
262 } // namespace cwg2363
263
264 namespace cwg2370 { // cwg2370: no
265 namespace N {
266 typedef int type;
267 void g(type);
268 void h(type);
269 } // namespace N
270 class C {
271 typedef N::type N_type;
272 // FIXME: `type` should be searched for in N
273 // friend void N::g(type);
274 friend void N::h(N_type);
275 };
276 } // namespace cwg2370
277
278 #if __cplusplus >= 201702L
279 // Otherwise, if the qualified-id std::tuple_size<E> names a complete class
280 // type **with a member value**, the expression std::tuple_size<E>::value shall
281 // be a well-formed integral constant expression
282 namespace cwg2386 { // cwg2386: 9
283 struct Bad1 { int a, b; };
284 struct Bad2 { int a, b; };
285 } // namespace cwg2386
286 namespace std {
287 template <typename T> struct tuple_size;
288 template <> struct tuple_size<cwg2386::Bad1> {};
289 template <> struct tuple_size<cwg2386::Bad2> {
290 static const int value = 42;
291 };
292 } // namespace std
293 namespace cwg2386 {
294 void no_value() { auto [x, y] = Bad1(); }
295 void wrong_value() { auto [x, y] = Bad2(); }
296 // since-cxx17-error@-1 {{type 'Bad2' decomposes into 42 elements, but only 2 names were provided}}
297 } // namespace cwg2386
298 #endif
299
300 // cwg2385: na
301
302 namespace cwg2387 { // cwg2387: 9
303 #if __cplusplus >= 201402L
304 template<int> int a = 0;
305 extern template int a<0>; // ok
306
307 template<int> static int b = 0;
308 extern template int b<0>;
309 // since-cxx14-error@-1 {{explicit instantiation declaration of 'b<0>' with internal linkage}}
310
311 template<int> const int c = 0;
312 extern template const int c<0>; // ok, has external linkage despite 'const'
313
314 template<typename T> T d = 0;
315 extern template int d<int>;
316 extern template const int d<const int>;
317 #endif
318 }
319
320 namespace cwg2390 { // cwg2390: 14
321 // Test that macro expansion of the builtin argument works.
322 #define C clang
323 #define F fallthrough
324 #define CF clang::fallthrough
325
326 #if !__has_cpp_attribute(F)
327 #error "doesn't have fallthrough"
328 #endif
329
330 #if !__has_cpp_attribute(C::F)
331 #error "doesn't have clang::fallthrough 1"
332 #endif
333
334 #if !__has_cpp_attribute(clang::F)
335 #error "doesn't have clang::fallthrough 2"
336 #endif
337
338 #if !__has_cpp_attribute(C::fallthrough)
339 #error "doesn't have clang::fallthrough 3"
340 #endif
341
342 #if !__has_cpp_attribute(CF)
343 #error "doesn't have clang::fallthrough 4"
344 #endif
345
346 #define FUNCLIKE1(x) clang::x
347 #if !__has_cpp_attribute(FUNCLIKE1(fallthrough))
348 #error "doesn't have clang::fallthrough through func-like macro 1"
349 #endif
350
351 #define FUNCLIKE2(x) _Clang::x
352 #if !__has_cpp_attribute(FUNCLIKE2(fallthrough))
353 #error "doesn't have clang::fallthrough through func-like macro 2"
354 #endif
355 } // namespace cwg2390
356
357 namespace cwg2394 { // cwg2394: 15
358
359 struct A {};
360 const A a;
361
362 // Now allowed to default-init B.
363 struct B { const A a; };
364 B b;
365
366 }
367
368 namespace cwg2396 { // cwg2396: no
369 struct A {
370 struct B;
371 operator B B::*();
372 };
373 struct B;
374
375 // FIXME: per P1787 "Calling a conversion function" example, all of the
376 // examples below are well-formed, with B resolving to A::B, but currently
377 // it's been resolved to cwg2396::B.
378
379 // void f(A a) { a.operator B B::*(); }
380 // void g(A a) { a.operator decltype(B()) B::*(); }
381 // void g2(A a) { a.operator B decltype(B())::*(); }
382 }
383
384 #if __cplusplus >= 201103L
385 namespace cwg2397 { // cwg2397: 17
386 void foo() {
387 int a[5];
388
389 auto (&b)[5] = a;
390 auto (*c)[5] = &a;
391 }
392 } // namespace cwg2397
393
394 #endif