xref: /llvm-project/clang/test/CXX/drs/cwg23xx.cpp (revision 3972ed57088f6515b787d7d38dec03dc74e51827)
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 cwg2311::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 cwg2370 { // cwg2370: no
369 namespace N {
370 typedef int type;
371 void g(type);
372 void h(type);
373 } // namespace N
374 class C {
375   typedef N::type N_type;
376   // FIXME: `type` should be searched for in N
377   // friend void N::g(type);
378   friend void N::h(N_type);
379 };
380 } // namespace cwg2370
381 
382 namespace cwg2386 { // cwg2386: 9
383 // Otherwise, if the qualified-id std::tuple_size<E> names a complete class
384 // type **with a member value**, the expression std::tuple_size<E>::value shall
385 // be a well-formed integral constant expression
386 #if __cplusplus >= 201702L
387 struct Bad1 { int a, b; };
388 struct Bad2 { int a, b; };
389 } // namespace cwg2386
390 namespace std {
391 template <typename T> struct tuple_size;
392 template <> struct tuple_size<cwg2386::Bad1> {};
393 template <> struct tuple_size<cwg2386::Bad2> {
394   static const int value = 42;
395 };
396 } // namespace std
397 namespace cwg2386 {
398 void no_value() { auto [x, y] = Bad1(); }
399 void wrong_value() { auto [x, y] = Bad2(); }
400 // since-cxx17-error@-1 {{type 'Bad2' decomposes into 42 elements, but only 2 names were provided}}
401 #endif
402 } // namespace cwg2386
403 
404 // cwg2385: na
405 
406 namespace cwg2387 { // cwg2387: 9
407 #if __cplusplus >= 201402L
408   template<int> int a = 0;
409   extern template int a<0>; // ok
410 
411   template<int> static int b = 0;
412   extern template int b<0>;
413   // since-cxx14-error@-1 {{explicit instantiation declaration of 'b<0>' with internal linkage}}
414 
415   template<int> const int c = 0;
416   extern template const int c<0>; // ok, has external linkage despite 'const'
417 
418   template<typename T> T d = 0;
419   extern template int d<int>;
420   extern template const int d<const int>;
421 #endif
422 } // namespace cwg2387
423 
424 namespace cwg2390 { // cwg2390: 14
425 // Test that macro expansion of the builtin argument works.
426 #define C clang
427 #define F fallthrough
428 #define CF clang::fallthrough
429 
430 #if !__has_cpp_attribute(F)
431 #error "doesn't have fallthrough"
432 #endif
433 
434 #if !__has_cpp_attribute(C::F)
435 #error "doesn't have clang::fallthrough 1"
436 #endif
437 
438 #if !__has_cpp_attribute(clang::F)
439 #error "doesn't have clang::fallthrough 2"
440 #endif
441 
442 #if !__has_cpp_attribute(C::fallthrough)
443 #error "doesn't have clang::fallthrough 3"
444 #endif
445 
446 #if !__has_cpp_attribute(CF)
447 #error "doesn't have clang::fallthrough 4"
448 #endif
449 
450 #define FUNCLIKE1(x) clang::x
451 #if !__has_cpp_attribute(FUNCLIKE1(fallthrough))
452 #error "doesn't have clang::fallthrough through func-like macro 1"
453 #endif
454 
455 #define FUNCLIKE2(x) _Clang::x
456 #if !__has_cpp_attribute(FUNCLIKE2(fallthrough))
457 #error "doesn't have clang::fallthrough through func-like macro 2"
458 #endif
459 } // namespace cwg2390
460 
461 namespace cwg2394 { // cwg2394: 15
462 
463 struct A {};
464 const A a;
465 
466 // Now allowed to default-init B.
467 struct B { const A a; };
468 B b;
469 
470 } // namespace cwg2394
471 
472 namespace cwg2396 { // cwg2396: no
473   struct A {
474     struct B;
475     operator B B::*();
476   };
477   struct B;
478 
479   // FIXME: per P1787 "Calling a conversion function" example, all of the
480   // examples below are well-formed, with B resolving to A::B, but currently
481   // it's been resolved to cwg2396::B.
482 
483   // void f(A a) { a.operator B B::*(); }
484   // void g(A a) { a.operator decltype(B()) B::*(); }
485   // void g2(A a) { a.operator B decltype(B())::*(); }
486 } // namespace cwg2396
487 
488 namespace cwg2397 { // cwg2397: 17
489 #if __cplusplus >= 201103L
490   void foo() {
491     int a[5];
492 
493     auto (&b)[5] = a;
494     auto (*c)[5] = &a;
495   }
496 #endif
497 } // namespace cwg2397
498