[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / CXX / drs / cwg19xx.cpp
blob2fe46909eaacb159a2428c2f290471c037a07149
1 // RUN: %clang_cc1 -std=c++98 %s -verify=expected,cxx98-11,cxx98 -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 %s -verify=expected,cxx98-11,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 %s -verify=expected,since-cxx14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++17 %s -verify=expected,since-cxx14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
5 // RUN: %clang_cc1 -std=c++20 %s -verify=expected,since-cxx14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
6 // RUN: %clang_cc1 -std=c++23 %s -verify=expected,since-cxx14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
7 // RUN: %clang_cc1 -std=c++2c %s -verify=expected,since-cxx14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
9 namespace std { struct type_info; }
11 namespace cwg1900 { // cwg1900: 2.7
12 // See the test for CWG1477 for detailed analysis
13 namespace N {
14 struct A {
15 friend int f();
18 int N::f() { return 0; }
19 int N::g() { return 0; }
20 // expected-error@-1 {{out-of-line definition of 'g' does not match any declaration in namespace 'cwg1900::N'}}
21 } // namespace cwg1900
23 namespace cwg1902 { // cwg1902: 3.7
24 struct A {};
25 struct B {
26 B(A); // #cwg1902-B-A
27 B() = delete; // #cwg1902-B-ctor
28 // cxx98-error@-1 {{deleted function definitions are a C++11 extension}}
29 B(const B&) = delete; // #cwg1902-B-copy-ctor
30 // cxx98-error@-1 {{deleted function definitions are a C++11 extension}}
31 operator A();
34 extern B b1;
35 B b2(b1);
36 // expected-error@-1 {{call to deleted constructor of 'B'}}
37 // expected-note@#cwg1902-B-copy-ctor {{'B' has been explicitly marked deleted here}}
39 #if __cplusplus >= 201103L
40 // This is ambiguous, even though calling the B(const B&) constructor would
41 // both directly and indirectly call a deleted function.
42 B b({});
43 // since-cxx11-error@-1 {{call to constructor of 'B' is ambiguous}}
44 // since-cxx11-note@#cwg1902-B-A {{candidate constructor}}
45 // since-cxx11-note@#cwg1902-B-copy-ctor {{candidate constructor has been explicitly deleted}}
46 #endif
49 namespace cwg1903 { // cwg1903: 2.7
50 namespace A {
51 struct a {};
52 int a;
53 namespace B {
54 int b;
56 using namespace B;
57 namespace {
58 int c;
60 namespace D {
61 int d;
63 using D::d;
65 namespace X {
66 using A::a;
67 using A::b;
68 using A::c;
69 using A::d;
70 struct a *p;
74 namespace cwg1909 { // cwg1909: 3.7
75 struct A {
76 template<typename T> struct A {};
77 // expected-error@-1 {{member 'A' has the same name as its class}}
79 struct B {
80 template<typename T> void B() {}
81 // expected-error@-1 {{constructor cannot have a return type}}
83 struct C {
84 template<typename T> static int C;
85 // expected-error@-1 {{member 'C' has the same name as its class}}
86 // cxx98-11-error@-2 {{variable templates are a C++14 extension}}
88 struct D {
89 template<typename T> using D = int;
90 // cxx98-error@-1 {{alias declarations are a C++11 extension}}
91 // expected-error@-2 {{member 'D' has the same name as its class}}
95 namespace cwg1918 { // cwg1918: no
96 template<typename T> struct A {
97 class B {
98 class C {};
101 class X {
102 static int x;
103 // FIXME: this is ill-formed, because A<T>::B::C does not end with a simple-template-id
104 template <typename T>
105 friend class A<T>::B::C;
106 // expected-warning@-1 {{dependent nested name specifier 'A<T>::B::' for friend class declaration is not supported; turning off access control for 'X'}}
108 template<> struct A<int> {
109 typedef struct Q B;
111 struct Q {
112 class C {
113 // FIXME: 'f' is not a friend, so 'X::x' is not accessible
114 int f() { return X::x; }
117 } // namespace cwg1918
119 namespace cwg1940 { // cwg1940: 3.5
120 #if __cplusplus >= 201103L
121 static union {
122 static_assert(true, ""); // ok
123 static_assert(false, "");
124 // since-cxx11-error@-1 {{static assertion failed}}
125 int not_empty;
127 #endif
130 namespace cwg1941 { // cwg1941: 3.9
131 #if __cplusplus >= 201402L
132 template<typename X>
133 struct base {
134 template<typename T>
135 base(T a, T b, decltype(void(*T()), 0) = 0) {
136 while (a != b) (void)*a++;
139 template<typename T>
140 base(T a, X x, decltype(void(T(0) * 1), 0) = 0) {
141 for (T n = 0; n != a; ++n) (void)X(x);
145 struct derived : base<int> {
146 using base::base;
149 struct iter {
150 iter operator++(int);
151 int operator*();
152 friend bool operator!=(iter, iter);
153 } it, end;
155 derived d1(it, end);
156 derived d2(42, 9);
157 #endif
160 namespace cwg1945 { // cwg1945: no
161 template<typename T> struct A {
162 class B {
163 class C {};
166 class X {
167 static int x;
168 // FIXME: this is ill-formed, because A<T>::B::C does not end with a simple-template-id
169 template <typename T>
170 friend class A<T>::B::C;
171 // expected-warning@-1 {{dependent nested name specifier 'A<T>::B::' for friend class declaration is not supported; turning off access control for 'X'}}
173 } // namespace cwg1945
175 namespace cwg1947 { // cwg1947: 3.5
176 #if __cplusplus >= 201402L
177 unsigned o = 0'01; // ok
178 unsigned b = 0b'01;
179 // since-cxx14-error@-1 {{invalid digit 'b' in octal constant}}
180 unsigned x = 0x'01;
181 // since-cxx14-error@-1 {{invalid suffix 'x'01' on integer constant}}
182 #endif
185 #if __cplusplus >= 201103L
186 // cwg1948: 3.5
187 // FIXME: This diagnostic could be improved.
188 void *operator new(__SIZE_TYPE__) noexcept { return nullptr; }
189 // since-cxx11-error@-1 {{exception specification in declaration does not match previous declaration}}
190 #endif
192 namespace cwg1959 { // cwg1959: 3.9
193 #if __cplusplus >= 201103L
194 struct b;
195 struct c;
196 struct a {
197 a() = default;
198 a(const a &) = delete; // #cwg1959-copy-ctor
199 a(const b &) = delete; // not inherited
200 a(c &&) = delete; // #cwg1959-move-ctor
201 template<typename T> a(T) = delete; // #cwg1959-temp-ctor
204 struct b : a { // #cwg1959-b
205 using a::a; // #cwg1959-using-a
208 a x;
209 // FIXME: As a resolution to an open DR against P0136R0, we disallow
210 // use of inherited constructors to construct from a single argument
211 // where the base class is reference-related to the argument type.
212 b y = x;
213 // since-cxx11-error@-1 {{no viable conversion from 'a' to 'b'}}
214 // since-cxx11-note@#cwg1959-move-ctor {{candidate inherited constructor not viable: no known conversion from 'a' to 'c &&' for 1st argument}}
215 // since-cxx11-note@#cwg1959-using-a {{constructor from base class 'a' inherited here}}
216 // since-cxx11-note@#cwg1959-b {{candidate constructor (the implicit copy constructor) not viable: cannot bind base class object of type 'a' to derived class reference 'const b &' for 1st argument}}
217 // since-cxx11-note@#cwg1959-temp-ctor {{candidate template ignored: instantiation would take its own class type by value}}
218 // since-cxx11-note@#cwg1959-using-a {{constructor from base class 'a' inherited here}}
219 b z = z;
220 // since-cxx11-error@-1 {{call to implicitly-deleted copy constructor of 'b'}}
221 // since-cxx11-note@#cwg1959-b {{copy constructor of 'b' is implicitly deleted because base class 'a' has a deleted copy constructor}}
222 // since-cxx11-note@#cwg1959-copy-ctor {{'a' has been explicitly marked deleted here}}
224 struct c : a {
225 using a::a;
226 c(const c &);
228 // FIXME: As a resolution to an open DR against P0136R0, we disallow
229 // use of inherited constructors to construct from a single argument
230 // where the base class is reference-related to the argument type.
231 c q(static_cast<c&&>(q));
232 #endif
235 namespace cwg1960 { // cwg1960: no
236 struct A {
237 void f() {}
238 protected:
239 void g() {}
242 struct B: A {
243 private:
244 using A::f;
245 using A::g;
248 struct C : B {
249 // FIXME: both declarations are ill-formed, because A::f and A::g
250 // are not accessible.
251 using A::f;
252 using A::g;
256 namespace cwg1966 { // cwg1966: 11
257 #if __cplusplus >= 201103L
258 struct A {
259 enum E : int {1};
260 // since-cxx11-error@-1 {{expected identifier}} (not bit-field)
262 auto *p1 = new enum E : int;
263 // since-cxx11-error@-1 {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration}}
264 auto *p2 = new enum F : int {};
265 // since-cxx11-error@-1 {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration}}
266 auto *p3 = true ? new enum G : int {};
267 // since-cxx11-error@-1 {{ISO C++ forbids forward references to 'enum' types}}
268 // since-cxx11-error@-2 {{allocation of incomplete type 'enum G'}}
269 // since-cxx11-note@-3 {{forward declaration of 'cwg1966::G'}}
270 auto h() -> enum E : int {};
271 // since-cxx11-error@-1 {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration}}
273 enum X : enum Y : int {} {};
274 // since-cxx11-error@-1 {{'cwg1966::Y' cannot be defined in a type specifier}}
275 struct Q {
276 // FIXME: can we emit something nicer than that?
277 enum X : enum Y : int {} {};
278 // since-cxx11-error@-1 {{non-defining declaration of enumeration with a fixed underlying type is only permitted as a standalone declaration; missing list of enumerators?}}
279 // since-cxx11-error@-2 {{non-integral type 'enum Y' is an invalid underlying type}}
280 // since-cxx11-error@-3 {{anonymous bit-field cannot have a default member initializer}}
282 #endif
285 namespace cwg1968 { // cwg1968: no
286 #if __cplusplus >= 201103L
287 // FIXME: According to CWG1968, both of these should be considered
288 // non-constant.
289 static_assert(&typeid(int) == &typeid(int), "");
291 constexpr const std::type_info *f() { return &typeid(int); }
292 static_assert(f() == f(), "");
293 #endif
296 namespace cwg1991 { // cwg1991: 3.9
297 #if __cplusplus >= 201103L
298 struct A {
299 A(int, int) = delete;
302 struct B : A {
303 using A::A;
304 B(int, int, int = 0);
307 // FIXME: As a resolution to an open DR against P0136R1, we treat derived
308 // class constructors as better than base class constructors in the presence
309 // of ambiguity.
310 B b(0, 0); // ok, calls B constructor
311 #endif
314 // cwg1994: dup 529