dr0xx.cpp revision 1c7315116ffb9b7527358ef2d145c5db68fccb36
1// RUN: %clang_cc1 -std=c++98 %s -verify -fexceptions -pedantic-errors -Wno-bind-to-temporary-copy 2// RUN: %clang_cc1 -std=c++11 %s -verify -fexceptions -pedantic-errors 3// RUN: %clang_cc1 -std=c++1y %s -verify -fexceptions -pedantic-errors 4 5namespace dr1 { // dr1: no 6 namespace X { extern "C" void dr1_f(int a = 1); } // expected-note 2{{candidate}} expected-note {{conflicting}} 7 namespace Y { extern "C" void dr1_f(int a = 2); } // expected-note 2{{candidate}} expected-note {{target}} 8 using X::dr1_f; using Y::dr1_f; 9 void g() { 10 // FIXME: The first of these two should be accepted. 11 dr1_f(0); // expected-error {{ambiguous}} 12 dr1_f(); // expected-error {{ambiguous}} 13 } 14 namespace X { 15 using Y::dr1_f; // expected-error {{conflicts with declaration already in scope}} 16 void h() { 17 // FIXME: The second of these two should be rejected. 18 dr1_f(0); 19 dr1_f(); 20 } 21 } 22 23 namespace X { 24 void z(int); 25 } 26 void X::z(int = 1) {} // expected-note {{previous}} 27 namespace X { 28 void z(int = 2); // expected-error {{redefinition of default argument}} 29 } 30} 31 32namespace dr3 { // dr3: yes 33 template<typename T> struct A {}; 34 template<typename T> void f(T) { A<T> a; } // expected-note {{implicit instantiation}} 35 template void f(int); 36 template<> struct A<int> {}; // expected-error {{explicit specialization of 'dr3::A<int>' after instantiation}} 37} 38 39namespace dr4 { // dr4: yes 40 extern "C" { 41 static void dr4_f(int) {} 42 static void dr4_f(float) {} 43 void dr4_g(int) {} // expected-note {{previous}} 44 void dr4_g(float) {} // expected-error {{conflicting types}} 45 } 46} 47 48namespace dr5 { // dr5: yes 49 struct A {} a; 50 struct B { 51 B(const A&); 52 B(const B&); 53 }; 54 const volatile B b = a; 55 56 struct C { C(C&); }; 57 struct D : C {}; 58 struct E { operator D&(); } e; 59 const C c = e; 60} 61 62namespace dr7 { // dr7: no 63 class A { public: ~A(); }; 64 class B : virtual private A {}; 65 class C : public B {} c; // FIXME: should be rejected, ~A is inaccessible 66 67 class X { ~X(); }; // expected-note {{here}} 68 class Y : X { ~Y() {} }; // expected-error {{private destructor}} 69} 70 71namespace dr8 { // dr8: dup 45 72 class A { 73 struct U; 74 static const int k = 5; 75 void f(); 76 template<typename, int, void (A::*)()> struct T; 77 78 T<U, k, &A::f> *g(); 79 }; 80 A::T<A::U, A::k, &A::f> *A::g() { return 0; } 81} 82 83namespace dr9 { // dr9: yes 84 struct B { 85 protected: 86 int m; // expected-note {{here}} 87 friend int R1(); 88 }; 89 struct N : protected B { // expected-note 2{{protected}} 90 friend int R2(); 91 } n; 92 int R1() { return n.m; } // expected-error {{protected base class}} expected-error {{protected member}} 93 int R2() { return n.m; } 94} 95 96namespace dr10 { // dr10: dup 45 97 class A { 98 struct B { 99 A::B *p; 100 }; 101 }; 102} 103 104namespace dr11 { // dr11: yes 105 template<typename T> struct A : T { 106 using typename T::U; 107 U u; 108 }; 109 template<typename T> struct B : T { 110 using T::V; 111 V v; // expected-error {{unknown type name}} 112 }; 113 struct X { typedef int U; }; 114 A<X> ax; 115} 116 117namespace dr12 { // dr12: sup 239 118 enum E { e }; 119 E &f(E, E = e); 120 void g() { 121 int &f(int, E = e); 122 // Under DR12, these call two different functions. 123 // Under DR239, they call the same function. 124 int &b = f(e); 125 int &c = f(1); 126 } 127} 128 129namespace dr14 { // dr14: no 130 namespace X { extern "C" int dr14_f(); } // expected-note {{candidate}} 131 namespace Y { extern "C" int dr14_f(); } // expected-note {{candidate}} 132 using namespace X; 133 using namespace Y; 134 // FIXME: This should be accepted, name lookup only finds one function (in two 135 // different namespaces). 136 int k = dr14_f(); // expected-error {{ambiguous}} 137 138 class C { 139 int k; // expected-note {{here}} 140 friend int Y::dr14_f(); 141 } c; 142 namespace Z { 143 // FIXME: This should be accepted, this function is a friend. 144 extern "C" int dr14_f() { return c.k; } // expected-error {{private}} 145 } 146 147 namespace X { typedef int T; typedef int U; } // expected-note {{candidate}} 148 namespace Y { typedef int T; typedef long U; } // expected-note {{candidate}} 149 T t; // ok, same type both times 150 U u; // expected-error {{ambiguous}} 151} 152 153namespace dr15 { // dr15: yes 154 template<typename T> void f(int); // expected-note {{previous}} 155 template<typename T> void f(int = 0); // expected-error {{default arguments cannot be added}} 156} 157 158namespace dr16 { // dr16: yes 159 class A { // expected-note {{here}} 160 void f(); // expected-note {{here}} 161 friend class C; 162 }; 163 class B : A {}; // expected-note 4{{here}} 164 class C : B { 165 void g() { 166 f(); // expected-error {{private member}} expected-error {{private base}} 167 A::f(); // expected-error {{private member}} expected-error {{private base}} 168 } 169 }; 170} 171 172namespace dr17 { // dr17: yes 173 class A { 174 int n; 175 int f(); 176 struct C; 177 }; 178 struct B : A {} b; 179 int A::f() { return b.n; } 180 struct A::C : A { 181 int g() { return n; } 182 }; 183} 184 185namespace dr18 { // dr18: yes 186 typedef void Void; 187 void f(Void); // expected-error {{empty parameter list defined with a typedef of 'void'}} 188} 189 190namespace dr19 { // dr19: yes 191 struct A { 192 int n; // expected-note {{here}} 193 }; 194 struct B : protected A { // expected-note {{here}} 195 }; 196 struct C : B {} c; 197 struct D : B { 198 int get1() { return c.n; } // expected-error {{protected member}} 199 int get2() { return ((A&)c).n; } // ok, A is an accessible base of B from here 200 }; 201} 202 203namespace dr20 { // dr20: yes 204 class X { 205 public: 206 X(); 207 private: 208 X(const X&); // expected-note {{here}} 209 }; 210 X f(); 211 X x = f(); // expected-error {{private}} 212} 213 214namespace dr21 { // dr21: no 215 template<typename T> struct A; 216 struct X { 217 // FIXME: We should reject these, per [temp.param]p9. 218 template<typename T = int> friend struct A; 219 template<typename T = int> friend struct B; 220 }; 221} 222 223namespace dr22 { // dr22: sup 481 224 template<typename dr22_T = dr22_T> struct X; // expected-error {{unknown type name 'dr22_T'}} 225 typedef int T; 226 template<typename T = T> struct Y; 227} 228 229namespace dr23 { // dr23: yes 230 template<typename T> void f(T, T); // expected-note {{candidate}} 231 template<typename T> void f(T, int); // expected-note {{candidate}} 232 void g() { f(0, 0); } // expected-error {{ambiguous}} 233} 234 235// dr24: na 236 237namespace dr25 { // dr25: no 238 struct A { 239 void f() throw(int); 240 }; 241 // FIXME: The initializations of g and i should be rejected. 242 void (A::*f)() throw (int); 243 void (A::*g)() throw () = f; 244 void (A::*h)() throw (int, char) = f; 245 void (A::*i)() throw () = &A::f; 246 void (A::*j)() throw (int, char) = &A::f; 247 void x() { 248 // FIXME: The assignments to g and i should be rejected. 249 g = f; 250 h = f; 251 i = &A::f; 252 j = &A::f; 253 } 254} 255 256namespace dr26 { // dr26: yes 257 struct A { A(A, const A & = A()); }; // expected-error {{must pass its first argument by reference}} 258 struct B { 259 B(); // expected-note {{candidate}} 260 B(const B &, B = B()); // expected-error {{no matching constructor}} expected-note {{candidate}} expected-note {{here}} 261 }; 262} 263 264namespace dr27 { // dr27: yes 265 enum E { e } n; 266 E &m = true ? n : n; 267} 268 269// dr28: na 270 271namespace dr29 { // dr29: no 272 void dr29_f0(); // expected-note {{here}} 273 void g0() { void dr29_f0(); } 274 extern "C++" void g0_cxx() { void dr29_f0(); } 275 extern "C" void g0_c() { void dr29_f0(); } // expected-error {{different language linkage}} 276 277 extern "C" void dr29_f1(); // expected-note {{here}} 278 void g1() { void dr29_f1(); } 279 extern "C" void g1_c() { void dr29_f1(); } 280 extern "C++" void g1_cxx() { void dr29_f1(); } // expected-error {{different language linkage}} 281 282 // FIXME: We should reject this. 283 void g2() { void dr29_f2(); } 284 extern "C" void dr29_f2(); 285 286 // FIXME: We should reject this. 287 extern "C" void g3() { void dr29_f3(); } 288 extern "C++" void dr29_f3(); 289 290 // FIXME: We should reject this. 291 extern "C++" void g4() { void dr29_f4(); } 292 extern "C" void dr29_f4(); 293 294 extern "C" void g5(); 295 extern "C++" void dr29_f5(); 296 void g5() { 297 void dr29_f5(); // ok, g5 is extern "C" but we're not inside the linkage-specification here. 298 } 299 300 extern "C++" void g6(); 301 extern "C" void dr29_f6(); 302 void g6() { 303 void dr29_f6(); // ok, g6 is extern "C" but we're not inside the linkage-specification here. 304 } 305 306 extern "C" void g7(); 307 extern "C++" void dr29_f7(); // expected-note {{here}} 308 extern "C" void g7() { 309 void dr29_f7(); // expected-error {{different language linkage}} 310 } 311 312 extern "C++" void g8(); 313 extern "C" void dr29_f8(); // expected-note {{here}} 314 extern "C++" void g8() { 315 void dr29_f8(); // expected-error {{different language linkage}} 316 } 317} 318 319namespace dr30 { // dr30: sup 468 320 struct A { 321 template<int> static int f(); 322 } a, *p = &a; 323 int x = A::template f<0>(); 324 int y = a.template f<0>(); 325 int z = p->template f<0>(); 326#if __cplusplus < 201103L 327 // FIXME: It's not clear whether DR468 applies to C++98 too. 328 // expected-error@-5 {{'template' keyword outside of a template}} 329 // expected-error@-5 {{'template' keyword outside of a template}} 330 // expected-error@-5 {{'template' keyword outside of a template}} 331#endif 332} 333 334namespace dr31 { // dr31: yes 335 class X { 336 private: 337 void operator delete(void*); // expected-note {{here}} 338 }; 339 // We would call X::operator delete if X() threw (even though it can't, 340 // and even though we allocated the X using ::operator delete). 341 X *p = new X; // expected-error {{private}} 342} 343 344// dr32: na 345 346namespace dr33 { // dr33: yes 347 namespace X { struct S; void f(void (*)(S)); } // expected-note {{candidate}} 348 namespace Y { struct T; void f(void (*)(T)); } // expected-note {{candidate}} 349 void g(X::S); 350 template<typename Z> Z g(Y::T); 351 void h() { f(&g); } // expected-error {{ambiguous}} 352} 353 354// dr34: na 355// dr35: dup 178 356// dr37: sup 475 357 358namespace dr38 { // dr38: yes 359 template<typename T> struct X {}; 360 template<typename T> X<T> operator+(X<T> a, X<T> b) { return a; } 361 template X<int> operator+<int>(X<int>, X<int>); 362} 363 364namespace dr39 { // dr39: no 365 namespace example1 { 366 struct A { int &f(int); }; 367 struct B : A { 368 using A::f; 369 float &f(float); 370 } b; 371 int &r = b.f(0); 372 } 373 374 namespace example2 { 375 struct A { 376 int &x(int); // expected-note {{found}} 377 static int &y(int); // expected-note {{found}} 378 }; 379 struct V { 380 int &z(int); 381 }; 382 struct B : A, virtual V { 383 using A::x; // expected-note {{found}} 384 float &x(float); 385 using A::y; // expected-note {{found}} 386 static float &y(float); 387 using V::z; 388 float &z(float); 389 }; 390 struct C : A, B, virtual V {} c; 391 int &x = c.x(0); // expected-error {{found in multiple base classes}} 392 // FIXME: This is valid, because we find the same static data member either way. 393 int &y = c.y(0); // expected-error {{found in multiple base classes}} 394 int &z = c.z(0); 395 } 396 397 namespace example3 { 398 struct A { static int f(); }; 399 struct B : virtual A { using A::f; }; 400 struct C : virtual A { using A::f; }; 401 struct D : B, C {} d; 402 int k = d.f(); 403 } 404 405 namespace example4 { 406 struct A { int n; }; // expected-note {{found}} 407 struct B : A {}; 408 struct C : A {}; 409 struct D : B, C { int f() { return n; } }; // expected-error {{found in multiple base-class}} 410 } 411} 412 413// dr40: na 414 415namespace dr41 { // dr41: yes 416 struct S f(S); 417} 418 419namespace dr42 { // dr42: yes 420 struct A { static const int k = 0; }; 421 struct B : A { static const int k = A::k; }; 422} 423 424// dr43: na 425 426namespace dr44 { // dr44: yes 427 struct A { 428 template<int> void f(); 429 template<> void f<0>(); // expected-error {{explicit specialization of 'f' in class scope}} 430 }; 431} 432 433namespace dr45 { // dr45: yes 434 class A { 435 class B {}; 436 class C : B {}; 437 C c; 438 }; 439} 440 441namespace dr46 { // dr46: yes 442 template<typename> struct A { template<typename> struct B {}; }; 443 template template struct A<int>::B<int>; // expected-error {{expected unqualified-id}} 444} 445 446namespace dr47 { // dr47: no 447 template<typename T> struct A { 448 friend void f() { T t; } 449 }; 450 A<int> a; 451 A<float> b; 452#if __cplusplus < 201103L 453 // expected-error@-5 {{redefinition}} expected-note@-5 {{previous}} 454 // expected-note@-3 {{instantiation of}} 455#else 456 void f(); 457 // FIXME: We should produce some kind of error here. C++11 [temp.friend]p4 458 // says we instantiate 'f' when it's odr-used, but that doesn't imply that 459 // this is valid; we still have multiple definitions of 'f' even if we never 460 // instantiate any of them. 461 void g() { f(); } 462#endif 463} 464 465namespace dr48 { // dr48: yes 466 namespace { 467 struct S { 468 static const int m = 0; 469 static const int n = 0; 470 static const int o = 0; 471 }; 472 } 473 int a = S::m; 474 // FIXME: We should produce a 'has internal linkage but is not defined' 475 // diagnostic for 'S::n'. 476 const int &b = S::n; 477 const int S::o; 478 const int &c = S::o; 479} 480 481namespace dr49 { // dr49: yes 482 template<int*> struct A {}; // expected-note {{here}} 483 int k; 484#if __has_feature(cxx_constexpr) 485 constexpr 486#endif 487 int *const p = &k; 488 A<&k> a; 489 A<p> b; // expected-error {{must have its address taken}} 490#if __cplusplus < 201103L 491 // expected-error@-2 {{internal linkage}} 492 // expected-note@-5 {{here}} 493#endif 494} 495 496namespace dr50 { // dr50: yes 497 struct X; // expected-note {{forward}} 498 extern X *p; 499 X *q = (X*)p; 500 X *r = static_cast<X*>(p); 501 X *s = const_cast<X*>(p); 502 X *t = reinterpret_cast<X*>(p); 503 X *u = dynamic_cast<X*>(p); // expected-error {{incomplete}} 504} 505