[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / CXX / drs / cwg26xx.cpp
blob63a954c803b77a5ba9f42c1b0ffcf288660af68b
1 // RUN: %clang_cc1 -std=c++98 -pedantic-errors %s -verify=expected,cxx98
2 // RUN: %clang_cc1 -std=c++11 -pedantic-errors %s -verify=expected,since-cxx11,cxx11
3 // RUN: %clang_cc1 -std=c++14 -pedantic-errors %s -verify=expected,since-cxx11
4 // RUN: %clang_cc1 -std=c++17 -pedantic-errors %s -verify=expected,since-cxx11
5 // RUN: %clang_cc1 -std=c++20 -pedantic-errors %s -verify=expected,since-cxx11,since-cxx20
6 // RUN: %clang_cc1 -std=c++23 -pedantic-errors %s -verify=expected,since-cxx11,since-cxx20,since-cxx23
7 // RUN: %clang_cc1 -std=c++2c -pedantic-errors %s -verify=expected,since-cxx11,since-cxx20,since-cxx23
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
14 namespace std {
15 #if __cplusplus >= 202002L
16 struct strong_ordering {
17 int n;
18 constexpr operator int() const { return n; }
19 static const strong_ordering less, equal, greater;
21 constexpr strong_ordering strong_ordering::less{-1},
22 strong_ordering::equal{0}, strong_ordering::greater{1};
23 #endif
25 typedef short int16_t;
26 typedef unsigned short uint16_t;
27 typedef int int32_t;
28 typedef unsigned uint32_t;
29 typedef long long int64_t;
30 // cxx98-error@-1 {{'long long' is a C++11 extension}}
31 typedef unsigned long long uint64_t;
32 // cxx98-error@-1 {{'long long' is a C++11 extension}}
33 static_assert(sizeof(int16_t) == 2 && sizeof(int32_t) == 4 && sizeof(int64_t) == 8, "Some tests rely on these sizes");
35 template<typename T> T declval();
38 namespace cwg2621 { // cwg2621: sup 2877
39 #if __cplusplus >= 202002L
40 enum class E { a };
41 namespace One {
42 using E_t = E;
43 using enum E_t; // typedef ok
44 auto v = a;
46 namespace Two {
47 using cwg2621::E;
48 int E; // ignored by type-only lookup
49 using enum E;
51 #endif
54 namespace cwg2627 { // cwg2627: 20
55 #if __cplusplus >= 202002L
56 struct C {
57 long long i : 8;
58 friend auto operator<=>(C, C) = default;
61 void f() {
62 C x{1}, y{2};
63 static_cast<void>(x <=> y);
64 static_cast<void>(x.i <=> y.i);
67 template<typename T>
68 struct CDependent {
69 T i : 8;
70 friend auto operator<=>(CDependent, CDependent) = default;
73 template<typename T>
74 concept three_way_comparable = requires(T t) { { t <=> t }; };
75 template<typename T>
76 concept bf_three_way_comparable = requires(T t) { { t.i <=> t.i }; };
77 static_assert(three_way_comparable<CDependent<long long>>);
78 static_assert(bf_three_way_comparable<CDependent<long long>>);
79 #endif
81 #if __cplusplus >= 201103L
82 template<typename T, int N>
83 struct D {
84 T i : N;
87 template<typename T, int N>
88 D<T, N> d();
90 std::int32_t d1{ d<std::int64_t, 31>().i };
91 std::int32_t d2{ d<std::int64_t, 32>().i };
92 std::int32_t d3{ d<std::int64_t, 33>().i };
93 // since-cxx11-error@-1 {{non-constant-expression cannot be narrowed from type 'long long' to 'std::int32_t' (aka 'int') in initializer list}}
94 // since-cxx11-note@-2 {{insert an explicit cast to silence this issue}}
96 std::int16_t d6{ d<int, 16>().i };
97 std::int16_t d7{ d<unsigned, 15>().i };
98 std::int16_t d8{ d<unsigned, 16>().i };
99 // since-cxx11-error@-1 {{non-constant-expression cannot be narrowed from type 'unsigned int' to 'std::int16_t' (aka 'short') in initializer list}}
100 // since-cxx11-note@-2 {{insert an explicit cast to silence this issue}}
101 std::uint16_t d9{ d<unsigned, 16>().i };
102 std::uint16_t da{ d<int, 1>().i };
103 // since-cxx11-error@-1 {{non-constant-expression cannot be narrowed from type 'int' to 'std::uint16_t' (aka 'unsigned short') in initializer list}}
104 // since-cxx11-note@-2 {{insert an explicit cast to silence this issue}}
106 bool db{ d<unsigned, 1>().i };
107 bool dc{ d<int, 1>().i };
108 // since-cxx11-error@-1 {{non-constant-expression cannot be narrowed from type 'int' to 'bool' in initializer list}}
109 // since-cxx11-note@-2 {{insert an explicit cast to silence this issue}}
111 template<typename Target, typename Source>
112 constexpr decltype(Target{ std::declval<Source>().i }, false) is_narrowing(int) { return false; }
113 template<typename Target, typename Source>
114 constexpr bool is_narrowing(long) { return true; }
116 static_assert(!is_narrowing<std::int16_t, D<int, 16>>(0), "");
117 static_assert(!is_narrowing<std::int16_t, D<unsigned, 15>>(0), "");
118 static_assert(is_narrowing<std::int16_t, D<unsigned, 16>>(0), "");
119 static_assert(!is_narrowing<std::uint16_t, D<unsigned, 16>>(0), "");
120 static_assert(is_narrowing<std::uint16_t, D<int, 1>>(0), "");
121 static_assert(!is_narrowing<bool, D<unsigned, 1>>(0), "");
122 static_assert(is_narrowing<bool, D<int, 1>>(0), "");
124 template<int N>
125 struct E {
126 signed int x : N;
127 decltype(std::int16_t{ x }) dependent_narrowing;
128 decltype(unsigned{ x }) always_narrowing;
129 // since-cxx11-error@-1 {{non-constant-expression cannot be narrowed from type 'int' to 'unsigned int' in initializer list}}
130 // since-cxx11-note@-2 {{insert an explicit cast to silence this issue}}
132 #endif
133 } // namespace cwg2627
135 namespace cwg2628 { // cwg2628: no
136 // this was reverted for the 16.x release
137 // due to regressions, see the issue for more details:
138 // https://github.com/llvm/llvm-project/issues/60777
139 #if __cplusplus >= 202002L
140 template <bool A = false, bool B = false>
141 struct foo {
142 // The expected notes below should be removed when cwg2628 is fully implemented again
143 constexpr foo() requires (!A && !B) = delete; // #cwg2628-ctor-1
144 constexpr foo() requires (A || B) = delete; // #cwg2628-ctor-2
147 void f() {
148 // The FIXME's below should be the expected errors when cwg2628 is
149 // fully implemented again.
150 foo fooable; // #cwg2628-fooable
151 // since-cxx20-error@-1 {{ambiguous deduction for template arguments of 'foo'}}
152 // since-cxx20-note@#cwg2628-ctor-1 {{candidate function [with A = false, B = false]}}
153 // since-cxx20-note@#cwg2628-ctor-2 {{candidate function [with A = false, B = false]}}
154 // FIXME-since-cxx20-error@#cwg2628-fooable {{call to deleted}}
155 // FIXME-since-cxx20-note@#cwg2628-ctor {{marked deleted here}}
157 #endif
160 // cwg2630 is in cwg2630.cpp
162 namespace cwg2631 { // cwg2631: 16
163 #if __cplusplus >= 202002L
164 constexpr int g();
165 consteval int f() {
166 return g();
168 int k(int x = f()) {
169 return x;
171 constexpr int g() {
172 return 42;
174 int test() {
175 return k();
177 #endif
180 namespace cwg2635 { // cwg2635: 16
181 #if __cplusplus >= 202002L
182 template<typename T>
183 concept UnaryC = true;
184 template<typename T, typename U>
185 concept BinaryC = true;
187 struct S{ int i, j; };
188 S get_S();
190 template<typename T>
191 T get_T();
193 void use() {
194 UnaryC auto [a, b] = get_S();
195 // since-cxx20-error@-1 {{decomposition declaration cannot be declared with constrained 'auto'}}
196 BinaryC<int> auto [c, d] = get_S();
197 // since-cxx20-error@-1 {{decomposition declaration cannot be declared with constrained 'auto'}}
200 template<typename T>
201 void TemplUse() {
202 UnaryC auto [a, b] = get_T<T>();
203 // since-cxx20-error@-1 {{decomposition declaration cannot be declared with constrained 'auto'}}
204 BinaryC<T> auto [c, d] = get_T<T>();
205 // since-cxx20-error@-1 {{decomposition declaration cannot be declared with constrained 'auto'}}
207 #endif
210 // cwg2636: na
212 namespace cwg2640 { // cwg2640: 16
214 int \N{Λ} = 0;
215 // expected-error@-1 {{'Λ' is not a valid Unicode character name}}
216 // expected-error@-2 {{expected unqualified-id}}
217 const char* emoji = "\N{🤡}";
218 // expected-error@-1 {{'🤡' is not a valid Unicode character name}}
219 // expected-note@-2 {{did you mean OX ('🐂' U+1F402)?}}
220 // expected-note@-3 {{did you mean ANT ('🐜' U+1F41C)?}}
221 // expected-note@-4 {{did you mean ARC ('⌒' U+2312)?}}
222 // expected-note@-5 {{did you mean AXE ('🪓' U+1FA93)?}}
223 // expected-note@-6 {{did you mean BAT ('🦇' U+1F987)?}}
225 #define z(x) 0
226 #define cwg2640_a z(
227 int x = cwg2640_a\N{abc});
228 // expected-error@-1 {{'abc' is not a valid Unicode character name}}
229 int y = cwg2640_a\N{LOTUS});
230 // expected-error@-1 {{character <U+1FAB7> not allowed in an identifier}}
231 // expected-error@-2 {{use of undeclared identifier 'cwg2640_a🪷'}}
232 // expected-error@-3 {{extraneous ')' before ';'}}
235 // cwg2642: na
237 namespace cwg2644 { // cwg2644: 8
238 #if __cplusplus >= 201103L
239 auto z = [a = 42](int a) {
240 // cxx11-error@-1 {{initialized lambda captures are a C++14 extension}}
241 // since-cxx11-error@-2 {{a lambda parameter cannot shadow an explicitly captured entity}}
242 // since-cxx11-note@-3 {{variable 'a' is explicitly captured here}}
243 return 1;
245 #endif
248 #if __cplusplus >= 202302L
249 namespace cwg2650 { // cwg2650: 17
250 template <class T, T> struct S {};
251 template <class T> int f(S<T, T{}>*); // #cwg2650-f
252 class X {
253 int m;
255 int i0 = f<X>(0);
256 // since-cxx23-error@-1 {{no matching function for call to 'f'}}
257 // since-cxx23-note@#cwg2650-f {{type 'X' of non-type template parameter is not a structural type}}
259 #endif
261 #if __cplusplus >= 202302L
262 namespace cwg2653 { // cwg2653: 18
263 struct Test { void f(this const auto& = Test{}); };
264 // since-cxx23-error@-1 {{the explicit object parameter cannot have a default argument}}
265 auto L = [](this const auto& = Test{}){};
266 // since-cxx23-error@-1 {{the explicit object parameter cannot have a default argument}}
268 #endif
270 namespace cwg2654 { // cwg2654: 16
271 void f() {
272 int neck, tail;
273 volatile int brachiosaur;
274 brachiosaur += neck; // OK
275 brachiosaur -= neck; // OK
276 brachiosaur |= neck; // OK
280 namespace cwg2681 { // cwg2681: 17
281 #if __cplusplus >= 202002L
282 using size_t = decltype(sizeof(int));
284 template<class T, size_t N>
285 struct H {
286 T array[N];
288 template<class T, size_t N>
289 struct I {
290 volatile T array[N];
292 template<size_t N>
293 struct J { // #cwg2681-J
294 unsigned char array[N];
297 H h = { "abc" };
298 I i = { "def" };
299 static_assert(__is_same(decltype(h), H<char, 4>)); // Not H<const char, 4>
300 static_assert(__is_same(decltype(i), I<char, 4>));
302 J j = { "ghi" };
303 // since-cxx20-error@-1 {{no viable constructor or deduction guide}}
304 // since-cxx20-note@#cwg2681-J {{candidate template ignored: could not match 'J<N>' against 'const char *'}}
305 // since-cxx20-note@#cwg2681-J {{implicit deduction guide declared as 'template <size_t N> J(J<N>) -> J<N>'}}
306 // since-cxx20-note@#cwg2681-J {{candidate template ignored: could not match 'const unsigned char' against 'const char'}}
307 // since-cxx20-note@#cwg2681-J {{implicit deduction guide declared as 'template <size_t N> J(const unsigned char (&)[N]) -> J<N>'}}
308 // since-cxx20-note@#cwg2681-J {{candidate function template not viable: requires 0 arguments, but 1 was provided}}
309 // since-cxx20-note@#cwg2681-J {{implicit deduction guide declared as 'template <size_t N> J() -> J<N>'}}
310 #endif
313 namespace cwg2672 { // cwg2672: 18
314 #if __cplusplus >= 202002L
315 template <class T>
316 void f(T) requires requires { []() { T::invalid; } (); };
317 // since-cxx20-error@-1 {{type 'int' cannot be used prior to '::' because it has no members}}
318 // since-cxx20-note@-2 {{while substituting into a lambda expression here}}
319 // since-cxx20-note@-3 {{in instantiation of requirement here}}
320 // since-cxx20-note@-4 {{while substituting template arguments into constraint expression here}}
321 // since-cxx20-note@#cwg2672-f-0 {{while checking constraint satisfaction for template 'f<int>' required here}}
322 // since-cxx20-note@#cwg2672-f-0 {{in instantiation of function template specialization 'cwg2672::f<int>' requested here}}
323 void f(...);
325 template <class T>
326 void bar(T) requires requires {
327 []() -> decltype(T::foo()) {};
329 void bar(...);
331 void m() {
332 f(0); // #cwg2672-f-0
333 bar(0);
335 #endif
338 #if __cplusplus >= 202302L
339 namespace cwg2687 { // cwg2687: 18
340 struct S{
341 void f(int);
342 static void g(int);
343 void h(this const S&, int);
346 void test() {
347 (&S::f)(1);
348 // since-cxx23-error@-1 {{called object type 'void (cwg2687::S::*)(int)' is not a function or function pointer}}
349 (&S::g)(1);
350 (&S::h)(S(), 1);
353 #endif
356 namespace cwg2692 { // cwg2692: 19
357 #if __cplusplus >= 202302L
359 struct A {
360 static void f(A); // #cwg2692-1
361 void f(this A); // #cwg2692-2
363 void g();
366 void A::g() {
367 (&A::f)(A());
368 // expected-error@-1 {{call to 'f' is ambiguous}}
369 // expected-note@#cwg2692-1 {{candidate}}
370 // expected-note@#cwg2692-2 {{candidate}}
374 (&A::f)();
375 // expected-error@-1 {{no matching function for call to 'f'}}
376 // expected-note@#cwg2692-1 {{candidate function not viable: requires 1 argument, but 0 were provided}}
377 // expected-note@#cwg2692-2 {{candidate function not viable: requires 1 argument, but 0 were provided}}
379 #endif