Debian Codegraph @

clang/test/CXX/drs/cwg21xx.cpp

raw
1 // RUN: %clang_cc1 -std=c++98 -triple x86_64-unknown-unknown %s -verify=expected,cxx98-14,cxx98 -fexceptions -Wno-deprecated-builtins -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-unknown %s -verify=expected,cxx98-14,since-cxx11 -fexceptions -Wno-deprecated-builtins -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -verify=expected,cxx98-14,since-cxx11 -fexceptions -Wno-deprecated-builtins -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -Wno-deprecated-builtins -fcxx-exceptions -pedantic-errors
5 // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -Wno-deprecated-builtins -fcxx-exceptions -pedantic-errors
6 // RUN: %clang_cc1 -std=c++23 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -Wno-deprecated-builtins -fcxx-exceptions -pedantic-errors
7 // RUN: %clang_cc1 -std=c++2c -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -Wno-deprecated-builtins -fcxx-exceptions -pedantic-errors
8
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
13
14 namespace std {
15 struct type_info;
16 }
17
18 namespace cwg2100 { // cwg2100: 12
19 template<const int *P, bool = true> struct X {};
20 template<typename T> struct A {
21 static const int n = 1;
22 int f() {
23 return X<&n>::n; // ok, value-dependent
24 }
25 int g() {
26 static const int n = 2; // #cwg2100-n
27 return X<&n>::n; // ok, value-dependent
28 // cxx98-14-error@-1 {{non-type template argument refers to object 'n' that does not have linkage}}
29 // cxx98-14-note@#cwg2100-n {{non-type template argument refers to object here}}
30 }
31 };
32 template<const int *P> struct X<P> {
33 #if __cplusplus == 199711L
34 static const int n = 0;
35 #else
36 static const int n = *P;
37 #endif
38 };
39 int q = A<int>().f() + A<int>().g();
40
41 // Corresponding constructs where the address is not taken are not
42 // value-dependent.
43 template<int N, bool = true> struct Y {};
44 template<typename T> struct B {
45 static const int n = 1;
46 int f() {
47 return Y<n>::declared_later;
48 // expected-error@-1 {{no member named 'declared_later' in 'cwg2100::Y<1>'}}
49 }
50 int g() {
51 static const int n = 2;
52 return Y<n>::declared_later;
53 // expected-error@-1 {{no member named 'declared_later' in 'cwg2100::Y<2>'}}
54 }
55 };
56 template<int N> struct Y<N> {
57 static const int declared_later = 0;
58 };
59 }
60
61 namespace cwg2103 { // cwg2103: yes
62 void f() {
63 int a;
64 int &r = a; // #cwg2103-r
65 struct Inner {
66 void f() {
67 int &s = r;
68 // expected-error@-1 {{reference to local variable 'r' declared in enclosing function 'cwg2103::f'}}
69 // expected-note@#cwg2103-r {{'r' declared here}}
70 (void)s;
71 }
72 };
73 }
74 }
75
76 namespace cwg2120 { // cwg2120: 7
77 struct A {};
78 struct B : A {};
79 struct C { A a; };
80 struct D { C c[5]; };
81 struct E : B { D d; };
82 static_assert(__is_standard_layout(B), "");
83 static_assert(__is_standard_layout(D), "");
84 static_assert(!__is_standard_layout(E), "");
85 }
86
87 namespace cwg2126 { // cwg2126: 12
88 #if __cplusplus >= 201103L
89 struct A { int n; };
90
91 const A &a = {1}; // const temporary
92 A &b = (A &)(const A &)A{1}; // const temporary
93 A &&c = (A &&)(const A &)A{1}; // const temporary
94
95 A &&d = {1}; // non-const temporary #cwg21260-d
96 const A &e = (A &)(A &&) A{1}; // non-const temporary #cwg21260-e
97 A &&f = (A &&)(A &&) A{1}; // non-const temporary #cwg21260-f
98
99 constexpr const A &g = {1}; // const temporary
100 constexpr A &&h = {1}; // non-const temporary #cwg21260-h
101
102 struct B { const A &a; };
103 B i = {{1}}; // extending decl not usable in constant expr #cwg21260-i
104 const B j = {{1}}; // extending decl not usable in constant expr #cwg21260-j
105 constexpr B k = {{1}}; // extending decl usable in constant expr
106
107 static_assert(a.n == 1, "");
108 static_assert(b.n == 1, "");
109 static_assert(c.n == 1, "");
110 static_assert(d.n == 1, "");
111 // since-cxx11-error@-1 {{static assertion expression is not an integral constant expression}}
112 // since-cxx11-note@-2 {{read of temporary is not allowed in a constant expression outside the expression that created the temporary}}
113 // since-cxx11-note@#cwg21260-d {{temporary created here}}
114 static_assert(e.n == 1, "");
115 // since-cxx11-error@-1 {{static assertion expression is not an integral constant expression}}
116 // since-cxx11-note@-2 {{read of temporary is not allowed in a constant expression outside the expression that created the temporary}}
117 // since-cxx11-note@#cwg21260-e {{temporary created here}}
118 static_assert(f.n == 1, "");
119 // since-cxx11-error@-1 {{static assertion expression is not an integral constant expression}}
120 // since-cxx11-note@-2 {{read of temporary is not allowed in a constant expression outside the expression that created the temporary}}
121 // since-cxx11-note@#cwg21260-f {{temporary created here}}
122 static_assert(g.n == 1, "");
123 static_assert(h.n == 1, "");
124 // since-cxx11-error@-1 {{static assertion expression is not an integral constant expression}}
125 // since-cxx11-note@-2 {{read of temporary is not allowed in a constant expression outside the expression that created the temporary}}
126 // since-cxx11-note@#cwg21260-h {{temporary created here}}
127 static_assert(i.a.n == 1, "");
128 // since-cxx11-error@-1 {{static assertion expression is not an integral constant expression}}
129 // since-cxx11-note@-2 {{read of non-constexpr variable 'i' is not allowed in a constant expression}}
130 // since-cxx11-note@#cwg21260-i {{declared here}}
131 static_assert(j.a.n == 1, "");
132 // since-cxx11-error@-1 {{static assertion expression is not an integral constant expression}}
133 // since-cxx11-note@-2 {{read of temporary is not allowed in a constant expression outside the expression that created the temporary}}
134 // since-cxx11-note@#cwg21260-j {{temporary created here}}
135 static_assert(k.a.n == 1, "");
136 #endif
137 }
138
139 namespace cwg2140 { // cwg2140: 9
140 #if __cplusplus >= 201103L
141 union U { int a; decltype(nullptr) b; };
142 constexpr int *test(U u) {
143 return u.b;
144 }
145 static_assert(!test({123}), "u.b should be valid even when b is inactive");
146 #endif
147 }
148
149 namespace cwg2141 { // cwg2141: 17
150 struct A{};
151
152 template <typename T>
153 struct B{};
154
155 void foo() {
156 struct A *b = (1 == 1) ? new struct A : new struct A;
157 struct S *a = (1 == 1) ? new struct S : new struct S;
158 // expected-error@-1 {{allocation of incomplete type 'struct S'}}
159 // expected-note@-2 {{forward declaration of 'S'}}
160 // expected-error@-3 {{allocation of incomplete type 'struct S'}}
161 // expected-note@-4 {{forward declaration of 'S'}}
162
163 #if __cplusplus >= 201103L
164 A *aa = new struct A{};
165 B<int> *bb = new struct B<int>{};
166 (void)new struct C{};
167 // since-cxx11-error@-1 {{allocation of incomplete type 'struct C'}}
168 // since-cxx11-note@-2 {{forward declaration of 'C'}}
169
170 struct A *c = (1 == 1) ? new struct A {} : new struct A {};
171
172 alignof(struct D{});
173 // since-cxx11-error@-1 {{'D' cannot be defined in a type specifier}}
174 #endif
175
176 sizeof(struct E{});
177 // expected-error@-1 {{'E' cannot be defined in a type specifier}}
178
179 }
180 }
181
182 // cwg2149 is in cwg2149.cpp
183
184 namespace cwg2157 { // cwg2157: 11
185 #if __cplusplus >= 201103L
186 enum E : int;
187 struct X {
188 enum cwg2157::E : int();
189 // since-cxx11-error@-1 {{ISO C++ only allows ':' in member enumeration declaration to introduce a fixed underlying type, not an anonymous bit-field}}
190 };
191 #endif
192 }
193
194 // cwg2165: na
195
196 namespace cwg2170 { // cwg2170: 9
197 #if __cplusplus >= 201103L
198 void f() {
199 constexpr int arr[3] = {1, 2, 3}; // #cwg2170-arr
200 struct S {
201 int get(int n) { return arr[n]; }
202 const int &get_ref(int n) { return arr[n]; }
203 // since-cxx11-warning@-1 {{reference to stack memory associated with local variable 'arr' returned}} FIXME
204 // since-cxx11-error@-2 {{reference to local variable 'arr' declared in enclosing function 'cwg2170::f'}}
205 // since-cxx11-note@#cwg2170-arr {{'arr' declared here}}
206 };
207 }
208 #endif
209 }
210
211 namespace cwg2171 { // cwg2171: 15
212 #if __cplusplus >= 201103L
213
214 struct NonConstCopy {
215 NonConstCopy(NonConstCopy &) = default;
216 NonConstCopy &operator=(NonConstCopy &) = default;
217 };
218
219 static_assert(__has_trivial_copy(NonConstCopy), "");
220 static_assert(__is_trivially_constructible(NonConstCopy, NonConstCopy &), "");
221 static_assert(!__is_trivially_constructible(NonConstCopy, NonConstCopy), "");
222 static_assert(!__is_trivially_constructible(NonConstCopy, const NonConstCopy &), "");
223 static_assert(!__is_trivially_constructible(NonConstCopy, NonConstCopy &&), "");
224
225 static_assert(__has_trivial_assign(NonConstCopy), "");
226 static_assert(__is_trivially_assignable(NonConstCopy &, NonConstCopy &), "");
227 static_assert(!__is_trivially_assignable(NonConstCopy &, const NonConstCopy &), "");
228 static_assert(!__is_trivially_assignable(NonConstCopy &, NonConstCopy), "");
229 static_assert(!__is_trivially_assignable(NonConstCopy &, NonConstCopy &&), "");
230 static_assert(__is_trivially_assignable(NonConstCopy &&, NonConstCopy &), "");
231 static_assert(!__is_trivially_assignable(NonConstCopy &&, const NonConstCopy &), "");
232 static_assert(!__is_trivially_assignable(NonConstCopy &&, NonConstCopy), "");
233 static_assert(!__is_trivially_assignable(NonConstCopy &&, NonConstCopy &&), "");
234
235 #endif
236 } // namespace cwg2171
237
238 namespace cwg2191 { // cwg2191: 19
239 #if __cplusplus >= 201103L
240 struct B { virtual void f() { } };
241 struct D : B { } d;
242 static_assert(noexcept(typeid(d)), "");
243 static_assert(!noexcept(typeid(*static_cast<D*>(nullptr))), "");
244 #endif
245 } // namespace cwg2191
246
247 namespace cwg2180 { // cwg2180: yes
248 class A {
249 A &operator=(const A &); // #cwg2180-A-copy
250 A &operator=(A &&); // #cwg2180-A-move
251 // cxx98-error@-1 {{rvalue references are a C++11 extension}}
252 };
253
254 struct B : virtual A { // #cwg2180-B
255 B &operator=(const B &);
256 B &operator=(B &&);
257 // cxx98-error@-1 {{rvalue references are a C++11 extension}}
258 virtual void foo() = 0;
259 };
260 B &B::operator=(const B&) = default; // #cwg2180-B-copy
261 // cxx98-error@-1 {{defaulted function definitions are a C++11 extension}}
262 // cxx98-error@-2 {{'operator=' is a private member of 'cwg2180::A'}}
263 // cxx98-note@-3 {{in defaulted copy assignment operator for 'cwg2180::B' first required here}}
264 // cxx98-note@#cwg2180-A-copy {{implicitly declared private here}}
265 // since-cxx11-error@#cwg2180-B-copy {{defaulting this copy assignment operator would delete it after its first declaration}}
266 // since-cxx11-note@#cwg2180-B {{copy assignment operator of 'B' is implicitly deleted because base class 'A' has an inaccessible copy assignment operator}}
267 B &B::operator=(B&&) = default; // #cwg2180-B-move
268 // cxx98-error@-1 {{rvalue references are a C++11 extension}}
269 // cxx98-error@-2 {{defaulted function definitions are a C++11 extension}}
270 // cxx98-error@-3 {{'operator=' is a private member of 'cwg2180::A'}}
271 // cxx98-note@-4 {{in defaulted move assignment operator for 'cwg2180::B' first required here}}
272 // cxx98-note@#cwg2180-A-move {{implicitly declared private here}}
273 // since-cxx11-error@#cwg2180-B-move {{defaulting this move assignment operator would delete it after its first declaration}}
274 // since-cxx11-note@#cwg2180-B {{move assignment operator of 'B' is implicitly deleted because base class 'A' has an inaccessible move assignment operator}}
275 }
276
277 namespace cwg2199 { // cwg2199: 3.8
278 // NB: reusing part of cwg407 test
279 namespace A {
280 struct S {};
281 }
282 namespace B {
283 typedef int S;
284 }
285 namespace E {
286 typedef A::S S;
287 using A::S;
288 struct S s;
289 }
290 namespace F {
291 typedef A::S S;
292 }
293 namespace G {
294 using namespace A;
295 using namespace F;
296 struct S s;
297 }
298 namespace H {
299 using namespace F;
300 using namespace A;
301 struct S s;
302 }
303 }