Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CXX / drs / dr13xx.cpp
blob3510695954e27c11335fc6d42db9ef39a579354d
1 // RUN: %clang_cc1 -std=c++98 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++17 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
6 __extension__ typedef __SIZE_TYPE__ size_t;
8 namespace std {
9 template<typename T> struct initializer_list {
10 const T *ptr;
11 size_t n;
12 initializer_list(const T*, size_t);
16 #if __cplusplus >= 201103L
17 namespace dr1305 { // dr1305: yes
18 struct Incomplete; // expected-note {{forward declaration of 'dr1305::Incomplete'}}
19 struct Complete {};
21 int incomplete = alignof(Incomplete(&)[]); // expected-error {{invalid application of 'alignof' to an incomplete type 'Incomplete'}}
22 int complete = alignof(Complete(&)[]);
24 #endif
26 namespace dr1307 { // dr1307: 14
27 #if __cplusplus >= 201103L
28 void f(int const (&)[2]);
29 void f(int const (&)[3]);
31 void caller() {
32 // This should not be ambiguous, the 2nd overload is better.
33 f({1, 2, 3});
35 #endif // __cplusplus >= 201103L
36 } // namespace dr1307
38 namespace dr1310 { // dr1310: 5
39 struct S {} * sp = new S::S; // expected-error {{qualified reference to 'S' is a constructor name}}
40 void f() {
41 S::S(a); // expected-error {{qualified reference to 'S' is a constructor name}}
43 struct T { int n; typedef int U; typedef T V; };
44 int k = T().T::T::n;
45 T::V v;
47 struct U { int U; };
48 int u = U().U::U;
49 struct U::U w;
51 struct V : T::T {
52 // FIXME: This is technically ill-formed, but we consider that to be a defect.
53 V() : T::T() {}
55 template<typename T> struct VT : T::T {
56 VT() : T::T() {}
58 template struct VT<T>;
60 template<template<typename> class> class TT {};
61 template<typename> class TTy {};
63 template<typename T> struct WBase {};
64 template<typename T> struct W : WBase<T> { typedef int X; int n; };
66 void w_test() {
67 W<int>::W w1a; // expected-error {{qualified reference to 'W' is a constructor name}}
68 W<int>::W::X w1ax;
69 W<int>::W<int> w1b; // expected-error {{qualified reference to 'W' is a constructor name}}
70 W<int>::W<int>::X w1bx;
71 typename W<int>::W w2a; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}}
72 typename W<int>::W::X w2ax; // expected-error 0-1{{outside of a template}}
73 typename W<int>::W<int> w2b; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}}
74 typename W<int>::W<int>::X w2bx; // expected-error 0-1{{outside of a template}}
75 W<int>::template W<int> w3; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}}
76 W<int>::template W<int>::X w3x; // expected-error 0-1{{outside of a template}}
77 typename W<int>::template W<int> w4; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-2{{outside of a template}}
78 typename W<int>::template W<int>::X w4x; // expected-error 0-2{{outside of a template}}
80 TT<W<int>::W> tt1; // expected-error {{qualified reference to 'W' is a constructor name}}
81 TTy<W<int>::W> tt1a; // expected-error {{qualified reference to 'W' is a constructor name}}
82 TT<W<int>::template W> tt2; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}}
83 TT<W<int>::WBase> tt3;
84 TTy<W<int>::WBase> tt3a;
85 TT<W<int>::template WBase> tt4; // expected-error 0-1{{outside of a template}}
87 W<int> w;
88 (void)w.W::W::n;
89 (void)w.W<int>::W::n;
90 (void)w.W<int>::W<int>::n;
91 (void)w.W<int>::template W<int>::n; // expected-error 0-1{{outside of a template}}
94 template<typename W>
95 void wt_test() {
96 typename W::W w2a; // expected-error {{qualified reference to 'W' is a constructor name}}
97 typename W::template W<int> w4; // expected-error {{qualified reference to 'W' is a constructor name}}
98 TTy<typename W::W> tt2; // expected-error {{qualified reference to 'W' is a constructor name}}
99 TT<W::template W> tt3; // expected-error {{qualified reference to 'W' is a constructor name}}
101 template<typename W>
102 void wt_test_good() {
103 typename W::W::X w2ax;
104 typename W::template W<int>::X w4x;
105 TTy<typename W::WBase> tt4;
106 TT<W::template WBase> tt5;
108 W w;
109 (void)w.W::W::n;
110 (void)w.W::template W<int>::n;
111 (void)w.template W<int>::W::n;
112 (void)w.template W<int>::template W<int>::n;
114 template void wt_test<W<int> >(); // expected-note {{instantiation of}}
115 template void wt_test_good<W<int> >();
118 namespace dr1315 { // dr1315: partial
119 template <int I, int J> struct A {};
120 template <int I> // expected-note {{non-deducible template parameter 'I'}}
121 struct A<I + 5, I * 2> {}; // expected-error {{contains a template parameter that cannot be deduced}}
122 template <int I> struct A<I, I> {};
124 template <int I, int J, int K> struct B;
125 template <int I, int K> struct B<I, I * 2, K> {}; // expected-note {{matches}}
126 B<1, 2, 3> b1;
128 // Multiple declarations with the same dependent expression are equivalent
129 // for partial ordering purposes.
130 template <int I> struct B<I, I * 2, 2> { typedef int type; };
131 B<1, 2, 2>::type b2;
133 // Multiple declarations with differing dependent expressions are unordered.
134 template <int I, int K> struct B<I, I + 1, K> {}; // expected-note {{matches}}
135 B<1, 2, 4> b3; // expected-error {{ambiguous}}
137 // FIXME: Under dr1315, this is perhaps valid, but that is not clear: this
138 // fails the "more specialized than the primary template" test because the
139 // dependent type of T::value is not the same as 'int'.
140 // A core issue will be opened to decide what is supposed to happen here.
141 template <typename T, int I> struct C;
142 template <typename T> struct C<T, T::value>;
143 // expected-error@-1 {{type of specialized non-type template argument depends on a template parameter of the partial specialization}}
146 namespace dr1330 { // dr1330: 4 c++11
147 // exception-specifications are parsed in a context where the class is complete.
148 struct A {
149 void f() throw(T) {} // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
150 struct T {};
152 #if __cplusplus >= 201103L
153 void g() noexcept(&a == b) {}
154 static int a;
155 static constexpr int *b = &a;
156 #endif
159 void (A::*af1)() throw(A::T) = &A::f; // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
160 void (A::*af2)() throw() = &A::f; // expected-error-re {{{{not superset|different exception spec}}}}
162 #if __cplusplus >= 201103L
163 static_assert(noexcept(A().g()), "");
164 #endif
166 // Likewise, they're instantiated separately from an enclosing class template.
167 template<typename U>
168 struct B {
169 void f() throw(T, typename U::type) {} // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
170 struct T {};
172 #if __cplusplus >= 201103L
173 void g() noexcept(&a == b && U::value) {}
174 static int a;
175 static constexpr int *b = &a;
176 #endif
179 B<int> bi; // ok
181 struct P {
182 typedef int type;
183 static const int value = true;
186 void (B<P>::*bpf1)() throw(B<P>::T, int) = &B<P>::f; // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
187 #if __cplusplus < 201103L
188 // expected-error@-2 {{not superset}}
189 // FIXME: We only delay instantiation in C++11 onwards. In C++98, something
190 // weird happens: instantiation of B<P> fails because it references T before
191 // it's instantiated, but the diagnostic is suppressed in
192 // Sema::FindInstantiatedDecl because we've already hit an error. This is
193 // obviously a bad way to react to this situation; we should still producing
194 // the "T has not yet been instantiated" error here, rather than giving
195 // confusing errors later on.
196 #endif
197 void (B<P>::*bpf2)() throw(int) = &B<P>::f; // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
198 #if __cplusplus <= 201402L
199 // expected-error@-2 {{not superset}}
200 #else
201 // expected-warning@-4 {{not superset}}
202 #endif
203 void (B<P>::*bpf3)() = &B<P>::f;
204 void (B<P>::*bpf4)() throw() = &B<P>::f;
205 #if __cplusplus <= 201402L
206 // expected-error@-2 {{not superset}}
207 #else
208 // expected-error@-4 {{different exception specifications}}
209 #endif
211 #if __cplusplus >= 201103L
212 static_assert(noexcept(B<P>().g()), "");
213 struct Q { static const int value = false; };
214 static_assert(!noexcept(B<Q>().g()), "");
215 #endif
217 template<typename T> int f() throw(typename T::error) { return 0; } // expected-error 1-4{{prior to '::'}} expected-note 0-1{{prior to '::'}} expected-note 0-1{{requested here}}
218 #if __cplusplus > 201402L
219 // expected-error@-2 0-1{{C++17}} expected-note@-2 0-1{{noexcept}}
220 #endif
221 // An exception-specification is needed even if the function is only used in
222 // an unevaluated operand.
223 int f1 = sizeof(f<int>()); // expected-note {{instantiation of}}
224 #if __cplusplus >= 201103L
225 decltype(f<char>()) f2; // expected-note {{instantiation of}}
226 bool f3 = noexcept(f<float>()); // expected-note {{instantiation of}}
227 #endif
228 // In C++17 onwards, substituting explicit template arguments into the
229 // function type substitutes into the exception specification (because it's
230 // part of the type). In earlier languages, we don't notice there's a problem
231 // until we've already started to instantiate.
232 template int f<short>();
233 #if __cplusplus >= 201703L
234 // expected-error@-2 {{does not refer to a function template}}
235 #else
236 // expected-note@-4 {{instantiation of}}
237 #endif
239 template<typename T> struct C {
240 C() throw(typename T::type); // expected-error 1-2{{prior to '::'}}
241 #if __cplusplus > 201402L
242 // expected-error@-2 0-1{{C++17}} expected-note@-2 0-1{{noexcept}}
243 #endif
245 struct D : C<void> {}; // ok
246 #if __cplusplus < 201103L
247 // expected-note@-2 {{instantiation of}}
248 #endif
249 void f(D &d) { d = d; } // ok
251 struct E : C<int> {}; // expected-note {{in instantiation of}}
252 #if __cplusplus >= 201103L
253 E e; // expected-note {{needed here}}
254 #endif
257 namespace dr1341 { // dr1341: sup P0683R1
258 #if __cplusplus >= 202002L
259 int a;
260 const int b = 0; // #dr1341-b-decl
261 struct S {
262 int x1 : 8 = 42;
263 int x2 : 8 { 42 };
264 int y1 : true ? 8 : a = 42;
265 int y2 : true ? 8 : b = 42;
266 // expected-error@-1 {{cannot assign to variable 'b' with const-qualified type 'const int'}}
267 // expected-note@#dr1341-b-decl {{variable 'b' declared const here}}
268 int y3 : (true ? 8 : b) = 42;
269 int z : 1 || new int { 0 };
271 #endif
274 namespace dr1346 { // dr1346: 3.5
275 auto a(1); // expected-error 0-1{{extension}}
276 auto b(1, 2); // expected-error {{multiple expressions}} expected-error 0-1{{extension}}
277 #if __cplusplus >= 201103L
278 auto c({}); // expected-error {{parenthesized initializer list}}
279 auto d({1}); // expected-error {{parenthesized initializer list}}
280 auto e({1, 2}); // expected-error {{parenthesized initializer list}}
281 #endif
282 template<typename...Ts> void f(Ts ...ts) { // expected-error 0-1{{extension}}
283 auto x(ts...); // expected-error {{empty}} expected-error 0-1{{extension}}
285 template void f(); // expected-note {{instantiation}}
287 #if __cplusplus >= 201103L
288 void init_capture() {
289 [a(1)] {} (); // expected-error 0-1{{extension}}
290 [b(1, 2)] {} (); // expected-error {{multiple expressions}} expected-error 0-1{{extension}}
291 #if __cplusplus >= 201103L
292 [c({})] {} (); // expected-error {{parenthesized initializer list}} expected-error 0-1{{extension}}
293 [d({1})] {} (); // expected-error {{parenthesized initializer list}} expected-error 0-1{{extension}}
294 [e({1, 2})] {} (); // expected-error {{parenthesized initializer list}} expected-error 0-1{{extension}}
295 #endif
297 #endif
300 namespace dr1347 { // dr1347: yes
301 auto x = 5, *y = &x; // expected-error 0-1{{extension}}
302 auto z = y, *q = y; // expected-error {{'auto' deduced as 'int *' in declaration of 'z' and deduced as 'int' in declaration of 'q'}} expected-error 0-1{{extension}}
303 #if __cplusplus >= 201103L
304 auto a = 5, b = {1, 2}; // expected-error {{'auto' deduced as 'int' in declaration of 'a' and deduced as 'std::initializer_list<int>' in declaration of 'b'}}
305 auto (*fp)(int) -> int, i = 0; // expected-error {{declaration with trailing return type must be the only declaration in its group}}
306 #endif
309 namespace dr1358 { // dr1358: yes
310 #if __cplusplus >= 201103L
311 struct Lit { constexpr operator int() const { return 0; } };
312 struct NonLit { NonLit(); operator int(); }; // expected-note 2{{no constexpr constructors}}
313 struct NonConstexprConv { constexpr operator int() const; };
314 struct Virt { virtual int f(int) const; };
316 template<typename T, typename U, typename V> struct A : V {
317 int member;
318 constexpr A(U u) : member(u) {}
319 constexpr T f(U u) const { return T(); }
322 constexpr A<Lit, Lit, Lit> ce = Lit();
323 constexpr int k = ce.f(Lit{});
325 // Can have a non-literal return type and parameter type.
326 // Constexpr function can be implicitly virtual.
327 A<NonLit, NonLit, Virt> a = NonLit();
328 void g() { a.f(NonLit()); }
330 // Constructor is still constexpr, so this is a literal type.
331 static_assert(__is_literal_type(decltype(a)), "");
333 // Constructor can call non-constexpr functions.
334 A<Lit, NonConstexprConv, Lit> b = NonConstexprConv();
336 // But the corresponding non-template cases are rejected.
337 struct B : Virt {
338 int member;
339 constexpr B(NonLit u) : member(u) {} // expected-error {{not a literal type}}
340 constexpr NonLit f(NonLit u) const { return NonLit(); } // expected-error {{not a literal type}}
342 #endif
345 namespace dr1359 { // dr1359: 3.5
346 #if __cplusplus >= 201103L
347 union A { constexpr A() = default; };
348 union B { constexpr B() = default; int a; }; // expected-error {{not constexpr}} expected-note 2{{candidate}}
349 union C { constexpr C() = default; int a, b; }; // expected-error {{not constexpr}} expected-note 2{{candidate}}
350 struct X { constexpr X() = default; union {}; }; // expected-error {{does not declare anything}}
351 struct Y { constexpr Y() = default; union { int a; }; }; // expected-error {{not constexpr}} expected-note 2{{candidate}}
353 constexpr A a = A();
354 constexpr B b = B(); // expected-error {{no matching}}
355 constexpr C c = C(); // expected-error {{no matching}}
356 constexpr X x = X();
357 constexpr Y y = Y(); // expected-error {{no matching}}
358 #endif
361 namespace dr1388 { // dr1388: 4
362 template<typename A, typename ...T> void f(T..., A); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}}
363 template<typename ...T> void g(T..., int); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}}
364 template<typename ...T, typename A> void h(T..., A); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}}
366 void test_f() {
367 f(0); // ok, trailing parameter pack deduced to empty
368 f(0, 0); // expected-error {{no matching}}
369 f<int>(0);
370 f<int>(0, 0); // expected-error {{no matching}}
371 f<int, int>(0, 0);
372 f<int, int, int>(0, 0); // expected-error {{no matching}}
374 g(0);
375 g(0, 0); // expected-error {{no matching}}
376 g<>(0);
377 g<int>(0); // expected-error {{no matching}}
378 g<int>(0, 0);
380 h(0);
381 h(0, 0); // expected-error {{no matching}}
382 h<int>(0, 0);
383 h<int, int>(0, 0); // expected-error {{no matching}}
386 // A non-trailing parameter pack is still a non-deduced context, even though
387 // we know exactly how many arguments correspond to it.
388 template<typename T, typename U> struct pair {};
389 template<typename ...T> struct tuple { typedef char type; }; // expected-error 0-2{{C++11}}
390 template<typename ...T, typename ...U> void f_pair_1(pair<T, U>..., int); // expected-error 0-2{{C++11}} expected-note {{[with T = <int, long>]: deduced incomplete pack <(no value), (no value)> for template parameter 'U'}}
391 template<typename ...T, typename U> void f_pair_2(pair<T, char>..., U); // expected-error 0-2{{C++11}}
392 template<typename ...T, typename ...U> void f_pair_3(pair<T, U>..., tuple<U...>); // expected-error 0-2{{C++11}} expected-note {{deduced packs of different lengths for parameter 'U' (<(no value), (no value)> vs. <char>)}}
393 template<typename ...T> void f_pair_4(pair<T, char>..., T...); // expected-error 0-2{{C++11}} expected-note {{<int, long> vs. <int, long, const char *>}}
394 void g(pair<int, char> a, pair<long, char> b, tuple<char, char> c) {
395 f_pair_1<int, long>(a, b, 0); // expected-error {{no match}}
396 f_pair_2<int, long>(a, b, 0);
397 f_pair_3<int, long>(a, b, c);
398 f_pair_3<int, long>(a, b, tuple<char>()); // expected-error {{no match}}
399 f_pair_4<int, long>(a, b, 0, 0L);
400 f_pair_4<int, long>(a, b, 0, 0L, "foo"); // expected-error {{no match}}
404 namespace dr1391 { // dr1391: partial
405 struct A {}; struct B : A {};
406 template<typename T> struct C { C(int); typename T::error error; }; // expected-error 2{{'::'}}
407 template<typename T> struct D {};
409 // No deduction is performed for parameters with no deducible template-parameters, therefore types do not need to match.
410 template<typename T> void a(T, int T::*);
411 void test_a(int A::*p) { a(A(), p); } // ok, type of second parameter does not need to match
413 namespace dr_example_1 {
414 template<typename T, typename U> void f(C<T>);
415 template<typename T> void f(D<T>);
417 void g(D<int> d) {
418 f(d); // ok, first 'f' eliminated by deduction failure
419 f<int>(d); // ok, first 'f' eliminated because 'U' cannot be deduced
423 namespace dr_example_2 {
424 template<typename T> typename C<T>::error f(int, T);
425 template<typename T> T f(T, T);
427 void g(A a) {
428 f(a, a); // ok, no conversion from A to int for first parameter of first candidate
432 namespace std_example {
433 template<typename T> struct Z {
434 typedef typename T::x xx;
436 template<typename T> typename Z<T>::xx f(void *, T);
437 template<typename T> void f(int, T);
438 struct A {} a;
439 void g() { f(1, a); }
442 template<typename T> void b(C<int> ci, T *p);
443 void b(...);
444 void test_b() {
445 b(0, 0); // ok, deduction fails prior to forming a conversion sequence and instantiating C<int>
446 // FIXME: The "while substituting" note should point at the overload candidate.
447 b<int>(0, 0); // expected-note {{instantiation of}} expected-note {{while substituting}}
450 template<typename T> struct Id { typedef T type; };
451 template<typename T> void c(T, typename Id<C<T> >::type);
452 void test_c() {
453 // Implicit conversion sequences for dependent types are checked later.
454 c(0.0, 0); // expected-note {{instantiation of}}
457 namespace partial_ordering {
458 // FIXME: Second template should be considered more specialized because non-dependent parameter is ignored.
459 template<typename T> int a(T, short) = delete; // expected-error 0-1{{extension}} expected-note {{candidate}}
460 template<typename T> int a(T*, char); // expected-note {{candidate}}
461 int test_a = a((int*)0, 0); // FIXME: expected-error {{ambiguous}}
463 // FIXME: Second template should be considered more specialized:
464 // deducing #1 from #2 ignores the second P/A pair, so deduction succeeds,
465 // deducing #2 from #1 fails to deduce T, so deduction fails.
466 template<typename T> int b(T, int) = delete; // expected-error 0-1{{extension}} expected-note {{candidate}}
467 template<typename T, typename U> int b(T*, U); // expected-note {{candidate}}
468 int test_b = b((int*)0, 0); // FIXME: expected-error {{ambiguous}}
470 // Unintended consequences: because partial ordering does not consider
471 // explicit template arguments, and deduction from a non-dependent type
472 // vacuously succeeds, a non-dependent template is less specialized than
473 // anything else!
474 // According to DR1391, this is ambiguous!
475 template<typename T> int c(int);
476 template<typename T> int c(T);
477 int test_c1 = c(0); // ok
478 int test_c2 = c<int>(0); // FIXME: apparently ambiguous
482 namespace dr1394 { // dr1394: 15
483 #if __cplusplus >= 201103L
484 struct Incomplete;
485 Incomplete f(Incomplete) = delete; // well-formed
486 #endif
489 namespace dr1395 { // dr1395: 16
490 #if __cplusplus >= 201103L
491 template <typename T, typename... U> void f(T, U...);
492 template <typename T> void f(T);
493 void h(int i) {
494 // This is made ambiguous by dr692, but made valid again by dr1395.
495 f(&i);
497 #endif
500 namespace dr1397 { // dr1397: 3.2
501 #if __cplusplus >= 201103L
502 struct A { // #dr1397-struct-A
503 void *p = A{}; // #dr1397-void-p
504 #if __cplusplus == 201103L
505 // expected-error@#dr1397-struct-A {{default member initializer for 'p' needed within definition of enclosing class 'A' outside of member functions}}
506 // expected-note@#dr1397-void-p {{in evaluation of exception specification for 'dr1397::A::A' needed here}}
507 // expected-note@#dr1397-void-p {{default member initializer declared here}}
508 #elif __cplusplus >= 201402L
509 // expected-error@#dr1397-void-p {{default member initializer for 'p' needed within definition of enclosing class 'A' outside of member functions}}
510 // expected-note@#dr1397-void-p {{default member initializer declared here}}
511 #endif
512 operator void*() const { return nullptr; }
514 #endif
515 } // namespace dr1397
517 namespace dr1399 { // dr1399: dup 1388
518 template<typename ...T> void f(T..., int, T...) {} // expected-note {{candidate}} expected-error 0-1{{C++11}}
519 void g() {
520 f(0);
521 f<int>(0, 0, 0);
522 f(0, 0, 0); // expected-error {{no match}}