[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / CXX / drs / cwg23xx.cpp
blob7f57d237526bc54f44689672c9bf5b406fa80993
1 // RUN: %clang_cc1 -std=c++98 %s -verify=expected,cxx98 -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
9 namespace std {
10 __extension__ typedef __SIZE_TYPE__ size_t;
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();
19 #if __cplusplus >= 201103L
20 namespace cwg2303 { // cwg2303: 12
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 {};
39 template <typename... T>
40 void f(const A<T...> &) {
41 static_assert(sizeof...(T) == 2, "Should only match A<int,int>");
43 template <typename... T>
44 void f2(const A<T...> *);
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>}} */
57 } // namespace cwg2303
58 #endif
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'}}
69 } // namespace cwg2304
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
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
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
101 #endif
102 } // namespace cwg2310
104 // cwg2331: na
105 // cwg2335 is in cwg2335.cxx
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)});
124 struct NonMovable {
125 NonMovable(int);
126 NonMovable(NonMovable&&) = delete;
128 struct NonMovableNonApplicableIList {
129 NonMovableNonApplicableIList(int);
130 NonMovableNonApplicableIList(NonMovableNonApplicableIList&&) = delete;
131 NonMovableNonApplicableIList(std::initializer_list<int>);
133 struct ExplicitMovable {
134 ExplicitMovable(int);
135 explicit ExplicitMovable(ExplicitMovable&&);
137 struct ExplicitNonMovable {
138 ExplicitNonMovable(int);
139 explicit ExplicitNonMovable(ExplicitNonMovable&&) = delete;
141 struct ExplicitNonMovableNonApplicableIList {
142 ExplicitNonMovableNonApplicableIList(int);
143 explicit ExplicitNonMovableNonApplicableIList(ExplicitNonMovableNonApplicableIList&&) = delete;
144 ExplicitNonMovableNonApplicableIList(std::initializer_list<int>);
146 struct CopyOnly {
147 CopyOnly(int);
148 CopyOnly(const CopyOnly&);
149 CopyOnly(CopyOnly&&) = delete;
151 struct ExplicitCopyOnly {
152 ExplicitCopyOnly(int);
153 explicit ExplicitCopyOnly(const ExplicitCopyOnly&);
154 explicit ExplicitCopyOnly(ExplicitCopyOnly&&) = delete;
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>();
165 struct any {
166 template<typename T>
167 any(T&&);
170 template<typename T>
171 struct X {
172 X();
173 X(T) = delete; // #cwg2311-X
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}}
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
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
196 #if __cplusplus >= 201103L
197 namespace cwg2338 { // cwg2338: 12
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 } // namespace cwg2338
207 #endif
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
216 namespace cwg2351 { // cwg2351: 20
217 #if __cplusplus >= 201103L
218 static_assert((void{}, true), "");
220 void f() {
221 return void{};
224 template<typename T>
225 void g() {
226 return T{};
228 template void g<void>();
229 template void g<const void>();
231 void h() {
232 return {};
233 // since-cxx11-error@-1 {{void function 'h' must not return a value}}
236 template<typename T, int... I>
237 T i() {
238 return T{I...};
240 template void i<void>();
241 template const void i<const void>();
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
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; }
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}}
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);
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 *'}}
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; }
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
289 namespace cwg2353 { // cwg2353: 9
290 struct X {
291 static const int n = 0;
294 // CHECK: FunctionDecl {{.*}} use
295 int use(X x) {
296 // CHECK: MemberExpr {{.*}} .n
297 // CHECK-NOT: non_odr_use
298 // CHECK: DeclRefExpr {{.*}} 'x'
299 // CHECK-NOT: non_odr_use
300 return *&x.n;
302 #pragma clang __debug dump use
304 // CHECK: FunctionDecl {{.*}} not_use
305 int not_use(X x) {
306 // CHECK: MemberExpr {{.*}} .n {{.*}} non_odr_use_constant
307 // CHECK: DeclRefExpr {{.*}} 'x'
308 return x.n;
310 #pragma clang __debug dump not_use
312 // CHECK: FunctionDecl {{.*}} not_use_2
313 int not_use_2(X *x) {
314 // CHECK: MemberExpr {{.*}} ->n {{.*}} non_odr_use_constant
315 // CHECK: DeclRefExpr {{.*}} 'x'
316 return x->n;
318 #pragma clang __debug dump not_use_2
321 namespace cwg2354 { // cwg2354: 15
322 #if __cplusplus >= 201103L
323 enum alignas(64) A {};
324 // since-cxx11-error@-1 {{'alignas' attribute cannot be applied to an enumeration}}
325 enum struct alignas(64) B {};
326 // since-cxx11-error@-1 {{'alignas' attribute cannot be applied to an enumeration}}
327 #endif
328 } // namespace cwg2354
330 namespace cwg2356 { // cwg2356: 4
331 #if __cplusplus >= 201103L
332 struct A {
333 A();
334 A(A &&); // #1
335 template<typename T> A(T &&); // #2
337 struct B : A {
338 using A::A;
339 B(const B &); // #3
340 B(B &&) = default; // #4, implicitly deleted
341 // since-cxx11-warning@-1 {{explicitly defaulted move constructor is implicitly deleted}}
342 // since-cxx11-note@#cwg2356-X {{move constructor of 'B' is implicitly deleted because field 'x' has a deleted move constructor}}
343 // since-cxx11-note@#cwg2356-X {{'X' has been explicitly marked deleted here}}
344 // since-cxx11-note@-4 {{replace 'default' with 'delete'}}
346 struct X { X(X &&) = delete; } x; // #cwg2356-X
348 extern B b1;
349 B b2 = static_cast<B&&>(b1); // calls #3: #1, #2, and #4 are not viable
350 struct C { operator B&&(); };
351 B b3 = C(); // calls #3
352 #endif
355 #if __cplusplus >= 201402L
356 namespace cwg2358 { // cwg2358: 16
357 void f2() {
358 int i = 1;
359 void g1(int = [xxx=1] { return xxx; }()); // OK
360 void g2(int = [xxx=i] { return xxx; }());
361 // since-cxx14-error@-1 {{default argument references local variable 'i' of enclosing function}}
364 #endif
366 // CWG2363 was closed as NAD, but its resolution does affirm that
367 // a friend declaration cannot have an opaque-enumm-specifier.
368 namespace cwg2363 { // cwg2363: yes
369 #if __cplusplus >= 201103L
370 enum class E0;
371 enum E1 : int;
373 struct A {
374 friend enum class E0;
375 // since-cxx11-error@-1 {{reference to enumeration must use 'enum' not 'enum class'}}
376 // expected-error@-2 {{elaborated enum specifier cannot be declared as a friend}}
377 // expected-note@-3 {{remove 'enum class' to befriend an enum}}
379 friend enum E0;
380 // expected-error@-1 {{elaborated enum specifier cannot be declared as a friend}}
381 // expected-note@-2 {{remove 'enum' to befriend an enum}}
383 friend enum class E1;
384 // since-cxx11-error@-1 {{reference to enumeration must use 'enum' not 'enum class'}}
385 // expected-error@-2 {{elaborated enum specifier cannot be declared as a friend}}
386 // expected-note@-3 {{remove 'enum class' to befriend an enum}}
388 friend enum E1;
389 // expected-error@-1 {{elaborated enum specifier cannot be declared as a friend}}
390 // expected-note@-2 {{remove 'enum' to befriend an enum}}
392 friend enum class E2;
393 // since-cxx11-error@-1 {{reference to enumeration must use 'enum' not 'enum class'}}
394 // expected-error@-2 {{elaborated enum specifier cannot be declared as a friend}}
395 // expected-note@-3 {{remove 'enum class' to befriend an enum}}
397 #endif
398 } // namespace cwg2363
400 namespace cwg2370 { // cwg2370: no
401 namespace N {
402 typedef int type;
403 void g(type);
404 void h(type);
405 } // namespace N
406 class C {
407 typedef N::type N_type;
408 // FIXME: `type` should be searched for in N
409 // friend void N::g(type);
410 friend void N::h(N_type);
412 } // namespace cwg2370
414 #if __cplusplus >= 201702L
415 // Otherwise, if the qualified-id std::tuple_size<E> names a complete class
416 // type **with a member value**, the expression std::tuple_size<E>::value shall
417 // be a well-formed integral constant expression
418 namespace cwg2386 { // cwg2386: 9
419 struct Bad1 { int a, b; };
420 struct Bad2 { int a, b; };
421 } // namespace cwg2386
422 namespace std {
423 template <typename T> struct tuple_size;
424 template <> struct tuple_size<cwg2386::Bad1> {};
425 template <> struct tuple_size<cwg2386::Bad2> {
426 static const int value = 42;
428 } // namespace std
429 namespace cwg2386 {
430 void no_value() { auto [x, y] = Bad1(); }
431 void wrong_value() { auto [x, y] = Bad2(); }
432 // since-cxx17-error@-1 {{type 'Bad2' decomposes into 42 elements, but only 2 names were provided}}
433 } // namespace cwg2386
434 #endif
436 // cwg2385: na
438 namespace cwg2387 { // cwg2387: 9
439 #if __cplusplus >= 201402L
440 template<int> int a = 0;
441 extern template int a<0>; // ok
443 template<int> static int b = 0;
444 extern template int b<0>;
445 // since-cxx14-error@-1 {{explicit instantiation declaration of 'b<0>' with internal linkage}}
447 template<int> const int c = 0;
448 extern template const int c<0>; // ok, has external linkage despite 'const'
450 template<typename T> T d = 0;
451 extern template int d<int>;
452 extern template const int d<const int>;
453 #endif
456 namespace cwg2390 { // cwg2390: 14
457 // Test that macro expansion of the builtin argument works.
458 #define C clang
459 #define F fallthrough
460 #define CF clang::fallthrough
462 #if !__has_cpp_attribute(F)
463 #error "doesn't have fallthrough"
464 #endif
466 #if !__has_cpp_attribute(C::F)
467 #error "doesn't have clang::fallthrough 1"
468 #endif
470 #if !__has_cpp_attribute(clang::F)
471 #error "doesn't have clang::fallthrough 2"
472 #endif
474 #if !__has_cpp_attribute(C::fallthrough)
475 #error "doesn't have clang::fallthrough 3"
476 #endif
478 #if !__has_cpp_attribute(CF)
479 #error "doesn't have clang::fallthrough 4"
480 #endif
482 #define FUNCLIKE1(x) clang::x
483 #if !__has_cpp_attribute(FUNCLIKE1(fallthrough))
484 #error "doesn't have clang::fallthrough through func-like macro 1"
485 #endif
487 #define FUNCLIKE2(x) _Clang::x
488 #if !__has_cpp_attribute(FUNCLIKE2(fallthrough))
489 #error "doesn't have clang::fallthrough through func-like macro 2"
490 #endif
491 } // namespace cwg2390
493 namespace cwg2394 { // cwg2394: 15
495 struct A {};
496 const A a;
498 // Now allowed to default-init B.
499 struct B { const A a; };
500 B b;
504 namespace cwg2396 { // cwg2396: no
505 struct A {
506 struct B;
507 operator B B::*();
509 struct B;
511 // FIXME: per P1787 "Calling a conversion function" example, all of the
512 // examples below are well-formed, with B resolving to A::B, but currently
513 // it's been resolved to cwg2396::B.
515 // void f(A a) { a.operator B B::*(); }
516 // void g(A a) { a.operator decltype(B()) B::*(); }
517 // void g2(A a) { a.operator B decltype(B())::*(); }
520 #if __cplusplus >= 201103L
521 namespace cwg2397 { // cwg2397: 17
522 void foo() {
523 int a[5];
525 auto (&b)[5] = a;
526 auto (*c)[5] = &a;
528 } // namespace cwg2397
530 #endif