Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CXX / drs / dr16xx.cpp
blob463353dab3b1604f09e9c6712aba55647c6c61e9
1 // RUN: %clang_cc1 -std=c++98 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
5 // RUN: %clang_cc1 -std=c++2a -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
7 #if __cplusplus < 201103L
8 // expected-error@+1 {{variadic macro}}
9 #define static_assert(...) __extension__ _Static_assert(__VA_ARGS__)
10 #endif
12 #if __cplusplus >= 201103L
13 namespace std {
14 typedef decltype(sizeof(int)) size_t;
16 template<typename E> class initializer_list {
17 const E *begin;
18 size_t size;
20 public:
21 initializer_list();
23 } // std
24 #endif
26 namespace dr1601 { // dr1601: 10
27 enum E : char { e };
28 #if __cplusplus < 201103L
29 // expected-error@-2 {{enumeration types with a fixed underlying type are a C++11 extension}}
30 #endif
31 void f(char);
32 void f(int);
33 void g() {
34 f(e);
36 } // namespace dr1601
38 namespace dr1611 { // dr1611: dup 1658
39 struct A { A(int); };
40 struct B : virtual A { virtual void f() = 0; };
41 struct C : B { C() : A(0) {} void f(); };
42 C c;
45 namespace dr1631 { // dr1631: 3.7
46 #if __cplusplus >= 201103L
47 // Incorrect overload resolution for single-element initializer-list
49 struct A { int a[1]; };
50 struct B { B(int); };
51 void f(B, int);
52 void f(B, int, int = 0);
53 void f(int, A);
55 void test() {
56 f({0}, {{1}}); // expected-warning {{braces around scalar init}}
59 namespace with_error {
60 void f(B, int); // TODO: expected- note {{candidate function}}
61 void f(int, A); // expected-note {{candidate function}}
62 void f(int, A, int = 0); // expected-note {{candidate function}}
64 void test() {
65 f({0}, {{1}}); // expected-error{{call to 'f' is ambiguous}}
68 #endif
71 namespace dr1638 { // dr1638: yes
72 #if __cplusplus >= 201103L
73 template<typename T> struct A {
74 enum class E; // expected-note {{previous}}
75 enum class F : T; // expected-note 2{{previous}}
78 template<> enum class A<int>::E;
79 template<> enum class A<int>::E {};
80 template<> enum class A<int>::F : int;
81 template<> enum class A<int>::F : int {};
83 template<> enum class A<short>::E : int;
84 template<> enum class A<short>::E : int {};
86 template<> enum class A<short>::F; // expected-error {{different underlying type}}
87 template<> enum class A<char>::E : char; // expected-error {{different underlying type}}
88 template<> enum class A<char>::F : int; // expected-error {{different underlying type}}
90 enum class A<unsigned>::E; // expected-error {{template specialization requires 'template<>'}} expected-error {{nested name specifier}}
91 template enum class A<unsigned>::E; // expected-error {{enumerations cannot be explicitly instantiated}}
92 enum class A<unsigned>::E *e; // expected-error {{must use 'enum' not 'enum class'}}
94 struct B {
95 friend enum class A<unsigned>::E; // expected-error {{must use 'enum' not 'enum class'}}
97 #endif
100 namespace dr1645 { // dr1645: 3.9
101 #if __cplusplus >= 201103L
102 struct A {
103 constexpr A(int, float = 0); // expected-note {{candidate}}
104 explicit A(int, int = 0); // expected-note 2{{candidate}}
105 A(int, int, int = 0) = delete; // expected-note {{candidate}}
108 struct B : A {
109 using A::A; // expected-note 4{{inherited here}}
112 constexpr B a(0); // expected-error {{ambiguous}}
113 constexpr B b(0, 0); // expected-error {{ambiguous}}
114 #endif
117 namespace dr1652 { // dr1652: 3.6
118 int a, b;
119 int arr[&a + 1 == &b ? 1 : 2]; // expected-error 2{{variable length array}}
120 // expected-note@-1 {{points past the end}}
123 namespace dr1653 { // dr1653: 4 c++17
124 void f(bool b) {
125 ++b;
126 b++;
127 #if __cplusplus <= 201402L
128 // expected-warning@-3 {{deprecated}} expected-warning@-2 {{deprecated}}
129 #else
130 // expected-error@-5 {{incrementing expression of type bool}} expected-error@-4 {{incrementing expression of type bool}}
131 #endif
132 --b; // expected-error {{cannot decrement expression of type bool}}
133 b--; // expected-error {{cannot decrement expression of type bool}}
134 b += 1; // ok
135 b -= 1; // ok
139 namespace dr1658 { // dr1658: 5
140 namespace DefCtor {
141 class A { A(); }; // expected-note 0-2{{here}}
142 class B { ~B(); }; // expected-note 0-2{{here}}
144 // The stars align! An abstract class does not construct its virtual bases.
145 struct C : virtual A { C(); virtual void foo() = 0; };
146 C::C() = default; // ok, not deleted, expected-error 0-1{{extension}}
147 struct D : virtual B { D(); virtual void foo() = 0; };
148 D::D() = default; // ok, not deleted, expected-error 0-1{{extension}}
150 // In all other cases, we are not so lucky.
151 struct E : A { E(); virtual void foo() = 0; };
152 #if __cplusplus < 201103L
153 E::E() = default; // expected-error {{private default constructor}} expected-error {{extension}} expected-note {{here}}
154 #else
155 E::E() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible default constructor}}
156 #endif
157 struct F : virtual A { F(); };
158 #if __cplusplus < 201103L
159 F::F() = default; // expected-error {{private default constructor}} expected-error {{extension}} expected-note {{here}}
160 #else
161 F::F() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible default constructor}}
162 #endif
164 struct G : B { G(); virtual void foo() = 0; };
165 #if __cplusplus < 201103L
166 G::G() = default; // expected-error@-2 {{private destructor}} expected-error {{extension}} expected-note {{here}}
167 #else
168 G::G() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible destructor}}
169 #endif
170 struct H : virtual B { H(); };
171 #if __cplusplus < 201103L
172 H::H() = default; // expected-error@-2 {{private destructor}} expected-error {{extension}} expected-note {{here}}
173 #else
174 H::H() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible destructor}}
175 #endif
178 namespace Dtor {
179 class B { ~B(); }; // expected-note 0-2{{here}}
181 struct D : virtual B { ~D(); virtual void foo() = 0; };
182 D::~D() = default; // ok, not deleted, expected-error 0-1{{extension}}
184 struct G : B { ~G(); virtual void foo() = 0; };
185 #if __cplusplus < 201103L
186 G::~G() = default; // expected-error@-2 {{private destructor}} expected-error {{extension}} expected-note {{here}}
187 #else
188 G::~G() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible destructor}}
189 #endif
190 struct H : virtual B { ~H(); };
191 #if __cplusplus < 201103L
192 H::~H() = default; // expected-error@-2 {{private destructor}} expected-error {{extension}} expected-note {{here}}
193 #else
194 H::~H() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible destructor}}
195 #endif
198 namespace MemInit {
199 struct A { A(int); }; // expected-note {{here}}
200 struct B : virtual A {
201 B() {}
202 virtual void f() = 0;
204 struct C : virtual A {
205 C() {} // expected-error {{must explicitly initialize}}
209 namespace CopyCtorParamType {
210 struct A { A(A&); };
211 struct B : virtual A { virtual void f() = 0; };
212 struct C : virtual A { virtual void f(); };
213 struct D : A { virtual void f() = 0; };
215 struct X {
216 friend B::B(const B&) throw();
217 friend C::C(C&);
218 friend D::D(D&);
222 namespace CopyCtor {
223 class A { A(const A&); A(A&&); }; // expected-note 0-4{{here}} expected-error 0-1{{extension}}
225 struct C : virtual A { C(const C&); C(C&&); virtual void foo() = 0; }; // expected-error 0-1{{extension}}
226 C::C(const C&) = default; // expected-error 0-1{{extension}}
227 C::C(C&&) = default; // expected-error 0-2{{extension}}
229 struct E : A { E(const E&); E(E&&); virtual void foo() = 0; }; // expected-error 0-1{{extension}}
230 #if __cplusplus < 201103L
231 E::E(const E&) = default; // expected-error {{private copy constructor}} expected-error {{extension}} expected-note {{here}}
232 E::E(E&&) = default; // expected-error {{private move constructor}} expected-error 2{{extension}} expected-note {{here}}
233 #else
234 E::E(const E&) = default; // expected-error {{would delete}} expected-note@-5{{inaccessible copy constructor}}
235 E::E(E&&) = default; // expected-error {{would delete}} expected-note@-6{{inaccessible move constructor}}
236 #endif
237 struct F : virtual A { F(const F&); F(F&&); }; // expected-error 0-1{{extension}}
238 #if __cplusplus < 201103L
239 F::F(const F&) = default; // expected-error {{private copy constructor}} expected-error {{extension}} expected-note {{here}}
240 F::F(F&&) = default; // expected-error {{private move constructor}} expected-error 2{{extension}} expected-note {{here}}
241 #else
242 F::F(const F&) = default; // expected-error {{would delete}} expected-note@-5{{inaccessible copy constructor}}
243 F::F(F&&) = default; // expected-error {{would delete}} expected-note@-6{{inaccessible move constructor}}
244 #endif
247 // assignment case is superseded by dr2180
250 namespace dr1672 { // dr1672: 7
251 struct Empty {};
252 struct A : Empty {};
253 struct B { Empty e; };
254 struct C : A { B b; int n; };
255 struct D : A { int n; B b; };
257 static_assert(!__is_standard_layout(C), "");
258 static_assert(__is_standard_layout(D), "");
260 struct E { B b; int n; };
261 struct F { int n; B b; };
262 union G { B b; int n; };
263 union H { int n; B b; };
265 struct X {};
266 template<typename T> struct Y : X, A { T t; };
268 static_assert(!__is_standard_layout(Y<E>), "");
269 static_assert(__is_standard_layout(Y<F>), "");
270 static_assert(!__is_standard_layout(Y<G>), "");
271 static_assert(!__is_standard_layout(Y<H>), "");
272 static_assert(!__is_standard_layout(Y<X>), "");
275 namespace dr1684 { // dr1684: 3.6
276 #if __cplusplus >= 201103L
277 struct NonLiteral { // expected-note {{because}}
278 NonLiteral();
279 constexpr int f() { return 0; } // expected-warning 0-1{{will not be implicitly 'const'}}
281 constexpr int f(NonLiteral &) { return 0; }
282 constexpr int f(NonLiteral) { return 0; } // expected-error {{not a literal type}}
283 #endif
286 namespace dr1687 { // dr1687: 7
287 template<typename T> struct To {
288 operator T(); // expected-note 2{{first operand was implicitly converted to type 'int *'}}
289 // expected-note@-1 {{second operand was implicitly converted to type 'double'}}
290 #if __cplusplus > 201703L
291 // expected-note@-3 2{{operand was implicitly converted to type 'dr1687::E}}
292 #endif
295 int *a = To<int*>() + 100.0; // expected-error {{invalid operands to binary expression ('To<int *>' and 'double')}}
296 int *b = To<int*>() + To<double>(); // expected-error {{invalid operands to binary expression ('To<int *>' and 'To<double>')}}
298 #if __cplusplus > 201703L
299 enum E1 {};
300 enum E2 {};
301 auto c = To<E1>() <=> To<E2>(); // expected-error {{invalid operands to binary expression ('To<E1>' and 'To<E2>')}}
302 #endif
305 namespace dr1690 { // dr1690: 9
306 // See also the various tests in "CXX/basic/basic.lookup/basic.lookup.argdep".
307 #if __cplusplus >= 201103L
308 namespace N {
309 static auto lambda = []() { struct S {} s; return s; };
310 void f(decltype(lambda()));
313 void test() {
314 auto s = N::lambda();
315 f(s); // ok
317 #endif
320 namespace dr1691 { // dr1691: 9
321 #if __cplusplus >= 201103L
322 namespace N {
323 namespace M {
324 enum E : int;
325 void f(E);
327 enum M::E : int {};
328 void g(M::E); // expected-note {{declared here}}
330 void test() {
331 N::M::E e;
332 f(e); // ok
333 g(e); // expected-error {{use of undeclared}}
335 #endif
338 namespace dr1692 { // dr1692: 9
339 namespace N {
340 struct A {
341 struct B {
342 struct C {};
345 void f(A::B::C);
347 void test() {
348 N::A::B::C c;
349 f(c); // ok
353 namespace dr1696 { // dr1696: 7
354 namespace std_examples {
355 #if __cplusplus >= 201402L
356 extern struct A a;
357 struct A {
358 const A &x = { A{a, a} };
359 const A &y = { A{} }; // expected-error {{default member initializer for 'y' needed within definition of enclosing class 'A' outside of member functions}} expected-note {{here}}
361 A a{a, a};
362 #endif
365 struct A { A(); ~A(); };
366 #if __cplusplus >= 201103L
367 struct B {
368 A &&a; // expected-note {{declared here}}
369 B() : a{} {} // expected-error {{reference member 'a' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}
370 } b;
371 #endif
373 struct C {
374 C();
375 const A &a; // expected-note {{declared here}}
377 C::C() : a(A()) {} // expected-error {{reference member 'a' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}
379 #if __cplusplus >= 201103L
380 // This is OK in C++14 onwards, per DR1815, though we don't support that yet:
381 // D1 d1 = {};
382 // is equivalent to
383 // D1 d1 = {A()};
384 // ... which lifetime-extends the A temporary.
385 struct D1 {
386 #if __cplusplus < 201402L
387 // expected-error@-2 {{binds to a temporary}}
388 #endif
389 const A &a = A(); // expected-note {{default member init}}
391 D1 d1 = {};
392 #if __cplusplus < 201402L
393 // expected-note@-2 {{first required here}}
394 #else
395 // expected-warning-re@-4 {{sorry, lifetime extension {{.*}} not supported}}
396 #endif
398 struct D2 {
399 const A &a = A(); // expected-note {{default member init}}
400 D2() {} // expected-error {{binds to a temporary}}
403 struct D3 { // expected-error {{binds to a temporary}}
404 const A &a = A(); // expected-note {{default member init}}
406 D3 d3; // expected-note {{first required here}}
408 struct haslist1 {
409 std::initializer_list<int> il; // expected-note {{'std::initializer_list' member}}
410 haslist1(int i) : il{i, 2, 3} {} // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
413 struct haslist2 {
414 std::initializer_list<int> il; // expected-note {{'std::initializer_list' member}}
415 haslist2();
417 haslist2::haslist2() : il{1, 2} {} // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
419 struct haslist3 {
420 std::initializer_list<int> il = {1, 2, 3};
423 struct haslist4 { // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
424 std::initializer_list<int> il = {1, 2, 3}; // expected-note {{default member initializer}}
426 haslist4 hl4; // expected-note {{in implicit default constructor}}
428 struct haslist5 {
429 std::initializer_list<int> il = {1, 2, 3}; // expected-note {{default member initializer}}
430 haslist5() {} // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
432 #endif