Debian Codegraph @

clang/test/CXX/drs/cwg23xx.cpp

raw
1 // RUN: %clang_cc1 -std=c++98 %s -verify=expected,cxx98 -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 %s -verify=expected,cxx11-14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 %s -verify=expected,cxx11-14,since-cxx11,since-cxx14 -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++17 %s -verify=expected,since-cxx11,since-cxx14,since-cxx17 -fexceptions -fcxx-exceptions -pedantic-errors
5 // RUN: %clang_cc1 -std=c++20 %s -verify=expected,since-cxx11,since-cxx14,since-cxx17,since-cxx20 -fexceptions -fcxx-exceptions -pedantic-errors
6 // RUN: %clang_cc1 -std=c++23 %s -verify=expected,since-cxx11,since-cxx14,since-cxx17,since-cxx20 -fexceptions -fcxx-exceptions -pedantic-errors
7 // RUN: %clang_cc1 -std=c++2c %s -verify=expected,since-cxx11,since-cxx14,since-cxx17,since-cxx20 -fexceptions -fcxx-exceptions -pedantic-errors
8
9 namespace std {
10 __extension__ typedef __SIZE_TYPE__ size_t;
11
12 template<typename E> struct initializer_list {
13 const E *p; size_t n;
14 initializer_list(const E *p, size_t n);
15 initializer_list();
16 };
17 }
18
19 namespace cwg2303 { // cwg2303: 12
20 #if __cplusplus >= 201103L
21 template <typename... T>
22 struct A;
23 template <>
24 struct A<> {};
25 template <typename T, typename... Ts>
26 struct A<T, Ts...> : A<Ts...> {};
27 struct B : A<int, int> {};
28 struct C : A<int, int>, A<int> {};
29 /* since-cxx11-warning@-1 {{direct base 'A<int>' is inaccessible due to ambiguity:
30 struct cwg2303::C -> A<int, int> -> A<int>
31 struct cwg2303::C -> A<int>}} */
32 struct D : A<int>, A<int, int> {};
33 /* since-cxx11-warning@-1 {{direct base 'A<int>' is inaccessible due to ambiguity:
34 struct cwg2303::D -> A<int>
35 struct cwg2303::D -> A<int, int> -> A<int>}} */
36 struct E : A<int, int> {};
37 struct F : B, E {};
38
39 template <typename... T>
40 void f(const A<T...> &) {
41 static_assert(sizeof...(T) == 2, "Should only match A<int,int>");
42 }
43 template <typename... T>
44 void f2(const A<T...> *);
45
46 void g() {
47 f(B{}); // This is no longer ambiguous.
48 B b;
49 f2(&b);
50 f(C{});
51 f(D{});
52 f(F{});
53 /* since-cxx11-error@-1 {{ambiguous conversion from derived class 'const F' to base class 'const A<int, int>':
54 struct cwg2303::F -> B -> A<int, int>
55 struct cwg2303::F -> E -> A<int, int>}} */
56 }
57 #endif
58 } // namespace cwg2303
59
60 namespace cwg2304 { // cwg2304: 2.8
61 template<typename T> void foo(T, int);
62 template<typename T> void foo(T&, ...);
63 struct Q; // #cwg2304-Q
64 void fn1(Q &data_vectors) {
65 foo(data_vectors, 0);
66 // expected-error@-1 {{argument type 'cwg2304::Q' is incomplete}}
67 // expected-note@#cwg2304-Q {{forward declaration of 'cwg2304::Q'}}
68 }
69 } // namespace cwg2304
70
71 namespace cwg2310 { // cwg2310: partial
72 #if __cplusplus >= 201103L
73 template<typename A, typename B>
74 struct check_derived_from {
75 static A a;
76 // FIXME: all 3 examples should be rejected in all language modes.
77 // FIXME: we should test this in 98 mode.
78 // FIXME: we accept this when MSVC triple is used
79 static constexpr B *p = &a;
80 #if !defined(_WIN32) || defined(__MINGW32__)
81 // cxx11-14-error@-2 {{cannot initialize a variable of type 'cwg2310::X *const' with an rvalue of type 'cwg2310::Z *'}}
82 // cxx11-14-note@#cwg2310-X {{in instantiation of template class 'cwg2310::check_derived_from<cwg2310::Z, cwg2310::X>' requested here}}
83 // cxx11-14-error@-4 {{cannot initialize a variable of type 'cwg2310::Y *const' with an rvalue of type 'cwg2310::Z *'}}
84 // cxx11-14-note@#cwg2310-Y {{in instantiation of template class 'cwg2310::check_derived_from<cwg2310::Z, cwg2310::Y>' requested here}}
85 #endif
86 };
87
88 struct W {};
89 struct X {};
90 struct Y {};
91 struct Z : W,
92 X, check_derived_from<Z, X>, // #cwg2310-X
93 check_derived_from<Z, Y>, Y // #cwg2310-Y
94 {
95 // FIXME: It was properly rejected before, but we're crashing since Clang 11 in C++11 and C++14 modes.
96 // See https://github.com/llvm/llvm-project/issues/59920
97 #if __cplusplus >= 201703L
98 check_derived_from<Z, W> cdf;
99 #endif
100 };
101 #endif
102 } // namespace cwg2310
103
104 // cwg2331: na
105 // cwg2335 is in cwg2335.cxx
106
107 namespace cwg2311 { // cwg2311 is open with no proposed resolution
108 #if __cplusplus >= 201707L
109 template<typename T>
110 void test() {
111 // Ensure none of these try to call a move constructor.
112 T a = T{T(0)};
113 T b{T(0)};
114 auto c{T(0)};
115 T d = {T(0)};
116 auto e = {T(0)};
117 #if __cplusplus >= 202302L
118 auto f = auto{T(0)};
119 #endif
120 void(*fn)(T);
121 fn({T(0)});
122 }
123
124 struct NonMovable {
125 NonMovable(int);
126 NonMovable(NonMovable&&) = delete;
127 };
128 struct NonMovableNonApplicableIList {
129 NonMovableNonApplicableIList(int);
130 NonMovableNonApplicableIList(NonMovableNonApplicableIList&&) = delete;
131 NonMovableNonApplicableIList(std::initializer_list<int>);
132 };
133 struct ExplicitMovable {
134 ExplicitMovable(int);
135 explicit ExplicitMovable(ExplicitMovable&&);
136 };
137 struct ExplicitNonMovable {
138 ExplicitNonMovable(int);
139 explicit ExplicitNonMovable(ExplicitNonMovable&&) = delete;
140 };
141 struct ExplicitNonMovableNonApplicableIList {
142 ExplicitNonMovableNonApplicableIList(int);
143 explicit ExplicitNonMovableNonApplicableIList(ExplicitNonMovableNonApplicableIList&&) = delete;
144 ExplicitNonMovableNonApplicableIList(std::initializer_list<int>);
145 };
146 struct CopyOnly {
147 CopyOnly(int);
148 CopyOnly(const CopyOnly&);
149 CopyOnly(CopyOnly&&) = delete;
150 };
151 struct ExplicitCopyOnly {
152 ExplicitCopyOnly(int);
153 explicit ExplicitCopyOnly(const ExplicitCopyOnly&);
154 explicit ExplicitCopyOnly(ExplicitCopyOnly&&) = delete;
155 };
156
157 template void test<NonMovable>();
158 template void test<NonMovableNonApplicableIList>();
159 template void test<ExplicitMovable>();
160 template void test<ExplicitNonMovable>();
161 template void test<ExplicitNonMovableNonApplicableIList>();
162 template void test<CopyOnly>();
163 template void test<ExplicitCopyOnly>();
164
165 struct any {
166 template<typename T>
167 any(T&&);
168 };
169
170 template<typename T>
171 struct X {
172 X();
173 X(T) = delete; // #cwg2311-X
174 };
175
176 X<std::initializer_list<any>> x{ X<std::initializer_list<any>>() };
177 // since-cxx17-error@-1 {{call to deleted constructor of 'X<std::initializer_list<any>>'}}
178 // since-cxx17-note@#cwg2311-X {{'X' has been explicitly marked deleted here}}
179
180 // Per the currently implemented resolution, this does not apply to std::initializer_list.
181 // An initializer list initialized from `{ e }` always has exactly one element constructed
182 // from `e`, where previously that could have been a copy of an init list or `e.operator std::initializer_list()`
183 struct InitListCtor {
184 InitListCtor(int);
185 InitListCtor(InitListCtor&&) = delete;
186 InitListCtor(std::initializer_list<InitListCtor>) = delete; // #cwg2311-InitListCtor
187 };
188
189 std::initializer_list<InitListCtor> i;
190 auto j = std::initializer_list<InitListCtor>{ i };
191 // since-cxx17-error@-1 {{conversion function from 'std::initializer_list<InitListCtor>' to 'const InitListCtor' invokes a deleted function}}
192 // since-cxx17-note@#cwg2311-InitListCtor {{'InitListCtor' has been explicitly marked deleted here}}
193 #endif
194 } // namespace cwg2311
195
196 namespace cwg2338 { // cwg2338: 12
197 #if __cplusplus >= 201103L
198 namespace B {
199 enum E : bool { Zero, One };
200 static_assert((int)(E)2 == 1, "");
201 } // namespace B
202 namespace D {
203 enum class E : bool { Zero, One };
204 static_assert((int)(E)2 == 1, "");
205 } // namespace D
206 #endif
207 } // namespace cwg2338
208
209 namespace cwg2346 { // cwg2346: 11
210 void test() {
211 const int i2 = 0;
212 extern void h2b(int x = i2 + 0); // ok, not odr-use
213 }
214 } // namespace cwg2346
215
216 namespace cwg2351 { // cwg2351: 20
217 #if __cplusplus >= 201103L
218 static_assert((void{}, true), "");
219
220 void f() {
221 return void{};
222 }
223
224 template<typename T>
225 void g() {
226 return T{};
227 }
228 template void g<void>();
229 template void g<const void>();
230
231 void h() {
232 return {};
233 // since-cxx11-error@-1 {{void function 'h' must not return a value}}
234 }
235
236 template<typename T, int... I>
237 T i() {
238 return T{I...};
239 }
240 template void i<void>();
241 template const void i<const void>();
242
243 static_assert((void({}), true), "");
244 // since-cxx11-error@-1 {{cannot initialize non-class type 'void' with a parenthesized initializer list}}
245 #else
246 int I = (void{}, 0);
247 // cxx98-error@-1 {{expected ')'}}
248 // cxx98-note@-2 {{to match this '('}}
249 // cxx98-error@-3 {{expected expression}}
250 #endif
251 } // namespace cwg2351
252
253 namespace cwg2352 { // cwg2352: 10
254 int **p;
255 const int *const *const &f1() { return p; }
256 int *const *const &f2() { return p; }
257 int **const &f3() { return p; }
258
259 const int **const &f4() { return p; }
260 // expected-error@-1 {{reference to type 'const int **const' could not bind to an lvalue of type 'int **'}}
261 const int *const *&f5() { return p; }
262 // expected-error@-1 {{binding reference of type 'const int *const *' to value of type 'int **' not permitted due to incompatible qualifiers}}
263
264 // FIXME: We permit this as a speculative defect resolution, allowing
265 // qualification conversions when forming a glvalue conditional expression.
266 const int * const * const q = 0;
267 __typeof(&(true ? p : q)) x = &(true ? p : q);
268
269 // FIXME: Should we compute the composite pointer type here and produce an
270 // lvalue of type 'const int *const * const'?
271 const int * const * r;
272 void *y = &(true ? p : r);
273 // expected-error@-1 {{rvalue of type 'const int *const *'}}
274
275 // FIXME: We order these as a speculative defect resolution.
276 void f(const int * const * const &r);
277 #if __cplusplus >= 201103L
278 constexpr
279 #endif
280 int *const *const &f(int * const * const &r) { return r; }
281
282 // No temporary is created here.
283 int *const *const &check_f = f(p);
284 #if __cplusplus >= 201103L
285 static_assert(&p == &check_f, "");
286 #endif
287 } // namespace cwg2352
288
289 namespace cwg2354 { // cwg2354: 15
290 #if __cplusplus >= 201103L
291 enum alignas(64) A {};
292 // since-cxx11-error@-1 {{'alignas' attribute cannot be applied to an enumeration}}
293 enum struct alignas(64) B {};
294 // since-cxx11-error@-1 {{'alignas' attribute cannot be applied to an enumeration}}
295 #endif
296 } // namespace cwg2354
297
298 namespace cwg2356 { // cwg2356: 4
299 #if __cplusplus >= 201103L
300 struct A {
301 A();
302 A(A &&); // #1
303 template<typename T> A(T &&); // #2
304 };
305 struct B : A {
306 using A::A;
307 B(const B &); // #3
308 B(B &&) = default; // #4, implicitly deleted
309 // since-cxx11-warning@-1 {{explicitly defaulted move constructor is implicitly deleted}}
310 // since-cxx11-note@#cwg2356-X {{move constructor of 'B' is implicitly deleted because field 'x' has a deleted move constructor}}
311 // since-cxx11-note@#cwg2356-X {{'X' has been explicitly marked deleted here}}
312 // since-cxx11-note@-4 {{replace 'default' with 'delete'}}
313
314 struct X { X(X &&) = delete; } x; // #cwg2356-X
315 };
316 extern B b1;
317 B b2 = static_cast<B&&>(b1); // calls #3: #1, #2, and #4 are not viable
318 struct C { operator B&&(); };
319 B b3 = C(); // calls #3
320 #endif
321 } // namespace cwg2356
322
323 namespace cwg2358 { // cwg2358: 16
324 #if __cplusplus >= 201402L
325 void f2() {
326 int i = 1;
327 void g1(int = [xxx=1] { return xxx; }()); // OK
328 void g2(int = [xxx=i] { return xxx; }());
329 // since-cxx14-error@-1 {{default argument references local variable 'i' of enclosing function}}
330 }
331 #endif
332 } // namespace cwg2358
333
334 // CWG2363 was closed as NAD, but its resolution does affirm that
335 // a friend declaration cannot have an opaque-enumm-specifier.
336 namespace cwg2363 { // cwg2363: 19
337 #if __cplusplus >= 201103L
338 enum class E0;
339 enum E1 : int;
340
341 struct A {
342 friend enum class E0;
343 // since-cxx11-error@-1 {{reference to enumeration must use 'enum' not 'enum class'}}
344 // since-cxx11-error@-2 {{elaborated enum specifier cannot be declared as a friend}}
345 // since-cxx11-note@-3 {{remove 'enum class' to befriend an enum}}
346
347 friend enum E0;
348 // since-cxx11-error@-1 {{elaborated enum specifier cannot be declared as a friend}}
349 // since-cxx11-note@-2 {{remove 'enum' to befriend an enum}}
350
351 friend enum class E1;
352 // since-cxx11-error@-1 {{reference to enumeration must use 'enum' not 'enum class'}}
353 // since-cxx11-error@-2 {{elaborated enum specifier cannot be declared as a friend}}
354 // since-cxx11-note@-3 {{remove 'enum class' to befriend an enum}}
355
356 friend enum E1;
357 // since-cxx11-error@-1 {{elaborated enum specifier cannot be declared as a friend}}
358 // since-cxx11-note@-2 {{remove 'enum' to befriend an enum}}
359
360 friend enum class E2;
361 // since-cxx11-error@-1 {{reference to enumeration must use 'enum' not 'enum class'}}
362 // since-cxx11-error@-2 {{elaborated enum specifier cannot be declared as a friend}}
363 // since-cxx11-note@-3 {{remove 'enum class' to befriend an enum}}
364 };
365 #endif
366 } // namespace cwg2363
367
368 namespace cwg2369 { // cwg2369: partial
369 #if __cplusplus >= 202002L
370 template <class T> struct Z {
371 typedef typename T::x xx;
372 };
373
374 template <class T>
375 concept C = requires { typename T::A; };
376 template <C T> typename Z<T>::xx f(void *, T); // #1
377 template <class T> void f(int, T); // #2
378
379 struct A {
380 } a;
381
382 struct ZZ {
383 template <class T, class = typename Z<T>::xx> operator T *();
384 operator int();
385 };
386
387 void foo() {
388 ZZ zz;
389 f(1, a); // OK, deduction fails for #1 because there is no conversion from int
390 // to void*
391 f(zz, 42); // OK, deduction fails for #1 because C<int> is not satisfied
392 }
393
394 #endif
395 } // namespace cwg2369
396
397 namespace cwg2370 { // cwg2370: no
398 namespace N {
399 typedef int type;
400 void g(type);
401 void h(type);
402 } // namespace N
403 class C {
404 typedef N::type N_type;
405 // FIXME: `type` should be searched for in N
406 // friend void N::g(type);
407 friend void N::h(N_type);
408 };
409 } // namespace cwg2370
410
411 namespace cwg2376 { // cwg2376: 21
412 #if __cplusplus >= 201703L
413 template<int = 0> class C {};
414
415 C a;
416 const volatile C b = C<2>();
417 C (c) = {};
418 C* d;
419 // expected-error@-1 {{cannot form pointer to deduced class template specialization type}}
420 C e[1];
421 // expected-error@-1 {{cannot form array of deduced class template specialization type}}
422 #endif
423 }
424
425 namespace cwg2386 { // cwg2386: 9
426 // Otherwise, if the qualified-id std::tuple_size<E> names a complete class
427 // type **with a member value**, the expression std::tuple_size<E>::value shall
428 // be a well-formed integral constant expression
429 #if __cplusplus >= 201702L
430 struct Bad1 { int a, b; };
431 struct Bad2 { int a, b; };
432 } // namespace cwg2386
433 namespace std {
434 template <typename T> struct tuple_size;
435 template <> struct tuple_size<cwg2386::Bad1> {};
436 template <> struct tuple_size<cwg2386::Bad2> {
437 static const int value = 42;
438 };
439 } // namespace std
440 namespace cwg2386 {
441 void no_value() { auto [x, y] = Bad1(); }
442 void wrong_value() { auto [x, y] = Bad2(); }
443 // since-cxx17-error@-1 {{type 'Bad2' binds to 42 elements, but only 2 names were provided}}
444 #endif
445 } // namespace cwg2386
446
447 // cwg2385: na
448
449 namespace cwg2387 { // cwg2387: 9
450 #if __cplusplus >= 201402L
451 template<int> int a = 0;
452 extern template int a<0>; // ok
453
454 template<int> static int b = 0;
455 extern template int b<0>;
456 // since-cxx14-error@-1 {{explicit instantiation declaration of 'b<0>' with internal linkage}}
457
458 template<int> const int c = 0;
459 extern template const int c<0>; // ok, has external linkage despite 'const'
460
461 template<typename T> T d = 0;
462 extern template int d<int>;
463 extern template const int d<const int>;
464 #endif
465 } // namespace cwg2387
466
467 namespace cwg2390 { // cwg2390: 14
468 // Test that macro expansion of the builtin argument works.
469 #define C clang
470 #define F fallthrough
471 #define CF clang::fallthrough
472
473 #if !__has_cpp_attribute(F)
474 #error "doesn't have fallthrough"
475 #endif
476
477 #if !__has_cpp_attribute(C::F)
478 #error "doesn't have clang::fallthrough 1"
479 #endif
480
481 #if !__has_cpp_attribute(clang::F)
482 #error "doesn't have clang::fallthrough 2"
483 #endif
484
485 #if !__has_cpp_attribute(C::fallthrough)
486 #error "doesn't have clang::fallthrough 3"
487 #endif
488
489 #if !__has_cpp_attribute(CF)
490 #error "doesn't have clang::fallthrough 4"
491 #endif
492
493 #define FUNCLIKE1(x) clang::x
494 #if !__has_cpp_attribute(FUNCLIKE1(fallthrough))
495 #error "doesn't have clang::fallthrough through func-like macro 1"
496 #endif
497
498 #define FUNCLIKE2(x) _Clang::x
499 #if !__has_cpp_attribute(FUNCLIKE2(fallthrough))
500 #error "doesn't have clang::fallthrough through func-like macro 2"
501 #endif
502 } // namespace cwg2390
503
504 namespace cwg2394 { // cwg2394: 15
505
506 struct A {};
507 const A a;
508
509 // Now allowed to default-init B.
510 struct B { const A a; };
511 B b;
512
513 } // namespace cwg2394
514
515 namespace cwg2396 { // cwg2396: no
516 struct A {
517 struct B;
518 operator B B::*();
519 };
520 struct B;
521
522 // FIXME: per P1787 "Calling a conversion function" example, all of the
523 // examples below are well-formed, with B resolving to A::B, but currently
524 // it's been resolved to cwg2396::B.
525
526 // void f(A a) { a.operator B B::*(); }
527 // void g(A a) { a.operator decltype(B()) B::*(); }
528 // void g2(A a) { a.operator B decltype(B())::*(); }
529 } // namespace cwg2396
530
531 namespace cwg2397 { // cwg2397: 17
532 #if __cplusplus >= 201103L
533 void foo() {
534 int a[5];
535
536 auto (&b)[5] = a;
537 auto (*c)[5] = &a;
538 }
539 #endif
540 } // namespace cwg2397