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++1y %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
4 
5 // PR13819 -- __SIZE_TYPE__ is incompatible.
6 // REQUIRES: LP64
7 
8 #if __cplusplus < 201103L
9 #define fold(x) (__builtin_constant_p(x) ? (x) : (x))
10 #else
11 #define fold
12 #endif
13 
14 namespace dr200 { // dr200: dup 214
15   template <class T> T f(int);
16   template <class T, class U> T f(U) = delete; // expected-error 0-1{{extension}}
17 
18   void g() {
19     f<int>(1);
20   }
21 }
22 
23 // dr201 FIXME: write codegen test
24 
25 namespace dr202 { // dr202: yes
26   template<typename T> T f();
27   template<int (*g)()> struct X {
28     int arr[fold(g == &f<int>) ? 1 : -1];
29   };
30   template struct X<f>;
31 }
32 
33 // FIXME (export) dr204: no
34 
35 namespace dr206 { // dr206: yes
36   struct S; // expected-note 2{{declaration}}
37   template<typename T> struct Q { S s; }; // expected-error {{incomplete}}
38   template<typename T> void f() { S s; } // expected-error {{incomplete}}
39 }
40 
41 namespace dr207 { // dr207: yes
42   class A {
43   protected:
44     static void f() {}
45   };
46   class B : A {
47   public:
48     using A::f;
49     void g() {
50       A::f();
51       f();
52     }
53   };
54 }
55 
56 // dr208 FIXME: write codegen test
57 
58 namespace dr209 { // dr209: yes
59   class A {
60     void f(); // expected-note {{here}}
61   };
62   class B {
63     friend void A::f(); // expected-error {{private}}
64   };
65 }
66 
67 // dr210 FIXME: write codegen test
68 
69 namespace dr211 { // dr211: yes
70   struct A {
71     A() try {
72       throw 0;
73     } catch (...) {
74       return; // expected-error {{return in the catch of a function try block of a constructor}}
75     }
76   };
77 }
78 
79 namespace dr213 { // dr213: yes
80   template <class T> struct A : T {
81     void h(T t) {
82       char &r1 = f(t);
83       int &r2 = g(t); // expected-error {{undeclared}}
84     }
85   };
86   struct B {
87     int &f(B);
88     int &g(B); // expected-note {{in dependent base class}}
89   };
90   char &f(B);
91 
92   template void A<B>::h(B); // expected-note {{instantiation}}
93 }
94 
95 namespace dr214 { // dr214: yes
96   template<typename T, typename U> T checked_cast(U from) { U::error; }
97   template<typename T, typename U> T checked_cast(U *from);
98   class C {};
99   void foo(int *arg) { checked_cast<const C *>(arg); }
100 
101   template<typename T> T f(int);
102   template<typename T, typename U> T f(U) { T::error; }
103   void g() {
104     f<int>(1);
105   }
106 }
107 
108 namespace dr215 { // dr215: yes
109   template<typename T> class X {
110     friend void T::foo();
111     int n;
112   };
113   struct Y {
114     void foo() { (void)+X<Y>().n; }
115   };
116 }
117 
118 namespace dr216 { // dr216: no
119   // FIXME: Should reject this: 'f' has linkage but its type does not,
120   // and 'f' is odr-used but not defined in this TU.
121   typedef enum { e } *E;
122   void f(E);
123   void g(E e) { f(e); }
124 
125   struct S {
126     // FIXME: Should reject this: 'f' has linkage but its type does not,
127     // and 'f' is odr-used but not defined in this TU.
128     typedef enum { e } *E;
129     void f(E);
130   };
131   void g(S s, S::E e) { s.f(e); }
132 }
133 
134 namespace dr217 { // dr217: yes
135   template<typename T> struct S {
136     void f(int);
137   };
138   template<typename T> void S<T>::f(int = 0) {} // expected-error {{default arguments cannot be added}}
139 }
140 
141 namespace dr218 { // dr218: yes
142   namespace A {
143     struct S {};
144     void f(S);
145   }
146   namespace B {
147     struct S {};
148     void f(S);
149   }
150 
151   struct C {
152     int f;
153     void test1(A::S as) { f(as); } // expected-error {{called object type 'int'}}
154     void test2(A::S as) { void f(); f(as); } // expected-error {{too many arguments}} expected-note {{}}
155     void test3(A::S as) { using A::f; f(as); } // ok
156     void test4(A::S as) { using B::f; f(as); } // ok
157     void test5(A::S as) { int f; f(as); } // expected-error {{called object type 'int'}}
158     void test6(A::S as) { struct f {}; (void) f(as); } // expected-error {{no matching conversion}} expected-note +{{}}
159   };
160 
161   namespace D {
162     struct S {};
163     struct X { void operator()(S); } f;
164   }
165   void testD(D::S ds) { f(ds); } // expected-error {{undeclared identifier}}
166 
167   namespace E {
168     struct S {};
169     struct f { f(S); };
170   }
171   void testE(E::S es) { f(es); } // expected-error {{undeclared identifier}}
172 
173   namespace F {
174     struct S {
175       template<typename T> friend void f(S, T) {}
176     };
177   }
178   void testF(F::S fs) { f(fs, 0); }
179 
180   namespace G {
181     namespace X {
182       int f;
183       struct A {};
184     }
185     namespace Y {
186       template<typename T> void f(T);
187       struct B {};
188     }
189     template<typename A, typename B> struct C {};
190   }
191   void testG(G::C<G::X::A, G::Y::B> gc) { f(gc); }
192 }
193 
194 // dr219: na
195 // dr220: na
196 
197 namespace dr221 { // dr221: yes
198   struct A {
199     A &operator=(int&);
200     A &operator+=(int&);
201     static A &operator=(A&, double&); // expected-error {{cannot be a static member}}
202     static A &operator+=(A&, double&); // expected-error {{cannot be a static member}}
203     friend A &operator=(A&, char&); // expected-error {{must be a non-static member function}}
204     friend A &operator+=(A&, char&);
205   };
206   A &operator=(A&, float&); // expected-error {{must be a non-static member function}}
207   A &operator+=(A&, float&);
208 
209   void test(A a, int n, char c, float f) {
210     a = n;
211     a += n;
212     a = c;
213     a += c;
214     a = f;
215     a += f;
216   }
217 }
218 
219 // dr222 is a mystery -- it lists no changes to the standard, and yet was
220 // apparently both voted into the WP and acted upon by the editor.
221 
222 // dr223: na
223 
224 namespace dr224 { // dr224: no
225   namespace example1 {
226     template <class T> class A {
227       typedef int type;
228       A::type a;
229       A<T>::type b;
230       A<T*>::type c; // expected-error {{missing 'typename'}}
231       ::dr224::example1::A<T>::type d;
232 
233       class B {
234         typedef int type;
235 
236         A::type a;
237         A<T>::type b;
238         A<T*>::type c; // expected-error {{missing 'typename'}}
239         ::dr224::example1::A<T>::type d;
240 
241         B::type e;
242         A<T>::B::type f;
243         A<T*>::B::type g; // expected-error {{missing 'typename'}}
244         typename A<T*>::B::type h;
245       };
246     };
247 
248     template <class T> class A<T*> {
249       typedef int type;
250       A<T*>::type a;
251       A<T>::type b; // expected-error {{missing 'typename'}}
252     };
253 
254     template <class T1, class T2, int I> struct B {
255       typedef int type;
256       B<T1, T2, I>::type b1;
257       B<T2, T1, I>::type b2; // expected-error {{missing 'typename'}}
258 
259       typedef T1 my_T1;
260       static const int my_I = I;
261       static const int my_I2 = I+0;
262       static const int my_I3 = my_I;
263       B<my_T1, T2, my_I>::type b3; // FIXME: expected-error {{missing 'typename'}}
264       B<my_T1, T2, my_I2>::type b4; // expected-error {{missing 'typename'}}
265       B<my_T1, T2, my_I3>::type b5; // FIXME: expected-error {{missing 'typename'}}
266     };
267   }
268 
269   namespace example2 {
270     template <int, typename T> struct X { typedef T type; };
271     template <class T> class A {
272       static const int i = 5;
273       X<i, int>::type w; // FIXME: expected-error {{missing 'typename'}}
274       X<A::i, char>::type x; // FIXME: expected-error {{missing 'typename'}}
275       X<A<T>::i, double>::type y; // FIXME: expected-error {{missing 'typename'}}
276       X<A<T*>::i, long>::type z; // expected-error {{missing 'typename'}}
277       int f();
278     };
279     template <class T> int A<T>::f() {
280       return i;
281     }
282   }
283 }
284 
285 // dr225: yes
286 template<typename T> void dr225_f(T t) { dr225_g(t); } // expected-error {{call to function 'dr225_g' that is neither visible in the template definition nor found by argument-dependent lookup}}
287 void dr225_g(int); // expected-note {{should be declared prior to the call site}}
288 template void dr225_f(int); // expected-note {{in instantiation of}}
289 
290 namespace dr226 { // dr226: no
291   template<typename T = void> void f() {}
292 #if __cplusplus < 201103L
293   // expected-error@-2 {{extension}}
294   // FIXME: This appears to be wrong: default arguments for function templates
295   // are listed as a defect (in c++98) not an extension. EDG accepts them in
296   // strict c++98 mode.
297 #endif
298   template<typename T> struct S {
299     template<typename U = void> void g();
300 #if __cplusplus < 201103L
301   // expected-error@-2 {{extension}}
302 #endif
303     template<typename U> struct X;
304     template<typename U> void h();
305   };
306   template<typename T> template<typename U> void S<T>::g() {}
307   template<typename T> template<typename U = void> struct S<T>::X {}; // expected-error {{cannot add a default template arg}}
308   template<typename T> template<typename U = void> void S<T>::h() {} // expected-error {{cannot add a default template arg}}
309 
310   template<typename> void friend_h();
311   struct A {
312     // FIXME: This is ill-formed.
313     template<typename=void> struct friend_B;
314     // FIXME: f, h, and i are ill-formed.
315     //  f is ill-formed because it is not a definition.
316     //  h and i are ill-formed because they are not the only declarations of the
317     //  function in the translation unit.
318     template<typename=void> void friend_f();
319     template<typename=void> void friend_g() {}
320     template<typename=void> void friend_h() {}
321     template<typename=void> void friend_i() {}
322 #if __cplusplus < 201103L
323   // expected-error@-5 {{extension}} expected-error@-4 {{extension}}
324   // expected-error@-4 {{extension}} expected-error@-3 {{extension}}
325 #endif
326   };
327   template<typename> void friend_i();
328 
329   template<typename=void, typename X> void foo(X) {}
330   template<typename=void, typename X> struct Foo {}; // expected-error {{missing a default argument}} expected-note {{here}}
331 #if __cplusplus < 201103L
332   // expected-error@-3 {{extension}}
333 #endif
334 
335   template<typename=void, typename X, typename, typename Y> int foo(X, Y);
336   template<typename, typename X, typename=void, typename Y> int foo(X, Y);
337   int x = foo(0, 0);
338 #if __cplusplus < 201103L
339   // expected-error@-4 {{extension}}
340   // expected-error@-4 {{extension}}
341 #endif
342 }
343 
344 void dr227(bool b) { // dr227: yes
345   if (b)
346     int n;
347   else
348     int n;
349 }
350 
351 namespace dr228 { // dr228: yes
352   template <class T> struct X {
353     void f();
354   };
355   template <class T> struct Y {
356     void g(X<T> x) { x.template X<T>::f(); }
357   };
358 }
359 
360 namespace dr229 { // dr229: yes
361   template<typename T> void f();
362   template<typename T> void f<T*>() {} // expected-error {{function template partial specialization}}
363   template<> void f<int>() {}
364 }
365 
366 namespace dr231 { // dr231: yes
367   namespace outer {
368     namespace inner {
369       int i; // expected-note {{here}}
370     }
371     void f() { using namespace inner; }
372     int j = i; // expected-error {{undeclared identifier 'i'; did you mean 'inner::i'?}}
373   }
374 }
375 
376 // dr234: na
377 // dr235: na
378 
379 namespace dr236 { // dr236: yes
380   void *p = int();
381 #if __cplusplus < 201103L
382   // expected-warning@-2 {{null pointer}}
383 #else
384   // expected-error@-4 {{cannot initialize}}
385 #endif
386 }
387 
388 namespace dr237 { // dr237: dup 470
389   template<typename T> struct A { void f() { T::error; } };
390   template<typename T> struct B : A<T> {};
391   template struct B<int>; // ok
392 }
393 
394 namespace dr239 { // dr239: yes
395   namespace NS {
396     class T {};
397     void f(T);
398     float &g(T, int);
399   }
400   NS::T parm;
401   int &g(NS::T, float);
402   int main() {
403     f(parm);
404     float &r = g(parm, 1);
405     extern int &g(NS::T, float);
406     int &s = g(parm, 1);
407   }
408 }
409 
410 // dr240: dup 616
411 
412 namespace dr241 { // dr241: yes
413   namespace A {
414     struct B {};
415     template <int X> void f(); // expected-note 2{{candidate}}
416     template <int X> void g(B);
417   }
418   namespace C {
419     template <class T> void f(T t); // expected-note 2{{candidate}}
420     template <class T> void g(T t); // expected-note {{candidate}}
421   }
422   void h(A::B b) {
423     f<3>(b); // expected-error {{undeclared identifier}}
424     g<3>(b); // expected-error {{undeclared identifier}}
425     A::f<3>(b); // expected-error {{no matching}}
426     A::g<3>(b);
427     C::f<3>(b); // expected-error {{no matching}}
428     C::g<3>(b); // expected-error {{no matching}}
429     using C::f;
430     using C::g;
431     f<3>(b); // expected-error {{no matching}}
432     g<3>(b);
433   }
434 }
435 
436 namespace dr243 { // dr243: yes
437   struct B;
438   struct A {
439     A(B); // expected-note {{candidate}}
440   };
441   struct B {
442     operator A() = delete; // expected-error 0-1{{extension}} expected-note {{candidate}}
443   } b;
444   A a1(b);
445   A a2 = b; // expected-error {{ambiguous}}
446 }
447 
448 namespace dr244 { // dr244: no
449   struct B {}; struct D : B {}; // expected-note {{here}}
450 
451   D D_object;
452   typedef B B_alias;
453   B* B_ptr = &D_object;
454 
455   void f() {
456     D_object.~B(); // expected-error {{expression does not match the type}}
457     D_object.B::~B();
458     B_ptr->~B();
459     B_ptr->~B_alias();
460     B_ptr->B_alias::~B();
461     // This is valid under DR244.
462     B_ptr->B_alias::~B_alias(); // FIXME: expected-error {{expected the class name after '~' to name a destructor}}
463     B_ptr->dr244::~B(); // expected-error {{refers to a member in namespace}}
464     B_ptr->dr244::~B_alias(); // expected-error {{refers to a member in namespace}}
465   }
466 }
467 
468 namespace dr245 { // dr245: yes
469   struct S {
470     enum E {}; // expected-note {{here}}
471     class E *p; // expected-error {{does not match previous declaration}}
472   };
473 }
474 
475 namespace dr246 { // dr246: yes
476   struct S {
477     S() try { // expected-note {{try block}}
478       throw 0;
479 X: ;
480     } catch (int) {
481       goto X; // expected-error {{protected scope}}
482     }
483   };
484 }
485 
486 namespace dr247 { // dr247: yes
487   struct A {};
488   struct B : A {
489     void f();
490     void f(int);
491   };
492   void (A::*f)() = (void (A::*)())&B::f;
493 
494   struct C {
495     void f();
496     void f(int);
497   };
498   struct D : C {};
499   void (C::*g)() = &D::f;
500   void (D::*h)() = &D::f;
501 
502   struct E {
503     void f();
504   };
505   struct F : E {
506     using E::f;
507     void f(int);
508   };
509   void (F::*i)() = &F::f;
510 }
511 
512 namespace dr248 { // dr248: yes c++11
513   // FIXME: Should this also apply to c++98 mode? This was a DR against C++98.
514   int \u040d\u040e = 0;
515 #if __cplusplus < 201103L
516   // FIXME: expected-error@-2 {{expected ';'}}
517 #endif
518 }
519 
520 namespace dr249 { // dr249: yes
521   template<typename T> struct X { void f(); };
522   template<typename T> void X<T>::f() {}
523 }
524 
525 namespace dr250 { // dr250: yes
526   typedef void (*FPtr)(double x[]);
527 
528   template<int I> void f(double x[]);
529   FPtr fp = &f<3>;
530 
531   template<int I = 3> void g(double x[]); // expected-error 0-1{{extension}}
532   FPtr gp = &g<>;
533 }
534 
535 namespace dr252 { // dr252: yes
536   struct A {
537     void operator delete(void*); // expected-note {{found}}
538   };
539   struct B {
540     void operator delete(void*); // expected-note {{found}}
541   };
542   struct C : A, B {
543     virtual ~C();
544   };
545   C::~C() {} // expected-error {{'operator delete' found in multiple base classes}}
546 
547   struct D {
548     void operator delete(void*, int); // expected-note {{here}}
549     virtual ~D();
550   };
551   D::~D() {} // expected-error {{no suitable member 'operator delete'}}
552 
553   struct E {
554     void operator delete(void*, int);
555     void operator delete(void*) = delete; // expected-error 0-1{{extension}} expected-note {{here}}
556     virtual ~E();
557   };
558   E::~E() {} // expected-error {{deleted}}
559 
560   struct F {
561     // If both functions are available, the first one is a placement delete.
562     void operator delete(void*, __SIZE_TYPE__);
563     void operator delete(void*) = delete; // expected-error 0-1{{extension}} expected-note {{here}}
564     virtual ~F();
565   };
566   F::~F() {} // expected-error {{deleted}}
567 
568   struct G {
569     void operator delete(void*, __SIZE_TYPE__);
570     virtual ~G();
571   };
572   G::~G() {}
573 }
574 
575 namespace dr254 { // dr254: yes
576   template<typename T> struct A {
577     typedef typename T::type type; // ok even if this is a typedef-name, because
578                                    // it's not an elaborated-type-specifier
579     typedef struct T::type foo; // expected-error {{elaborated type refers to a typedef}}
580   };
581   struct B { struct type {}; };
582   struct C { typedef struct {} type; }; // expected-note {{here}}
583   A<B>::type n;
584   A<C>::type n; // expected-note {{instantiation of}}
585 }
586 
587 // dr256: dup 624
588 
589 namespace dr257 { // dr257: yes
590   struct A { A(int); }; // expected-note {{here}}
591   struct B : virtual A {
592     B() {}
593     virtual void f() = 0;
594   };
595   struct C : B {
596     C() {}
597   };
598   struct D : B {
599     D() {} // expected-error {{must explicitly initialize the base class 'dr257::A'}}
600     void f();
601   };
602 }
603 
604 namespace dr258 { // dr258: yes
605   struct A {
606     void f(const int);
607     template<typename> void g(int);
608     float &h() const;
609   };
610   struct B : A {
611     using A::f;
612     using A::g;
613     using A::h;
614     int &f(int);
615     template<int> int &g(int); // expected-note {{candidate}}
616     int &h();
617   } b;
618   int &w = b.f(0);
619   int &x = b.g<int>(0); // expected-error {{no match}}
620   int &y = b.h();
621   float &z = const_cast<const B&>(b).h();
622 
623   struct C {
624     virtual void f(const int) = 0;
625   };
626   struct D : C {
627     void f(int);
628   } d;
629 
630   struct E {
631     virtual void f() = 0; // expected-note {{unimplemented}}
632   };
633   struct F : E {
634     void f() const {}
635   } f; // expected-error {{abstract}}
636 }
637 
638 namespace dr259 { // dr259: yes c++11
639   template<typename T> struct A {};
640   template struct A<int>; // expected-note {{previous}}
641   template struct A<int>; // expected-error {{duplicate explicit instantiation}}
642 
643   // FIXME: We only apply this DR in C++11 mode.
644   template<> struct A<float>;
645   template struct A<float>;
646 #if __cplusplus < 201103L
647   // expected-error@-2 {{extension}} expected-note@-3 {{here}}
648 #endif
649 
650   template struct A<char>; // expected-note {{here}}
651   template<> struct A<char>; // expected-error {{explicit specialization of 'dr259::A<char>' after instantiation}}
652 
653   template<> struct A<double>;
654   template<> struct A<double>;
655   template<> struct A<double> {}; // expected-note {{here}}
656   template<> struct A<double> {}; // expected-error {{redefinition}}
657 
658   template<typename T> struct B; // expected-note {{here}}
659   template struct B<int>; // expected-error {{undefined}}
660 
661   template<> struct B<float>;
662   template struct B<float>;
663 #if __cplusplus < 201103L
664   // expected-error@-2 {{extension}} expected-note@-3 {{here}}
665 #endif
666 }
667 
668 namespace dr261 { // dr261: no
669 #pragma clang diagnostic push
670 #pragma clang diagnostic warning "-Wused-but-marked-unused"
671 
672   // FIXME: This is ill-formed, with a diagnostic required, because operator new
673   // and operator delete are inline and odr-used, but not defined in this
674   // translation unit.
675   // We're also missing the -Wused-but-marked-unused diagnostic here.
676   struct A {
677     inline void *operator new(__SIZE_TYPE__) __attribute__((unused));
678     inline void operator delete(void*) __attribute__((unused));
679     A() {}
680   };
681 
682   // FIXME: These are ill-formed, with a required diagnostic, for the same
683   // reason.
684   struct B {
685     inline void operator delete(void*) __attribute__((unused));
686     ~B() {}
687   };
688   struct C {
689     inline void operator delete(void*) __attribute__((unused));
690     virtual ~C() {}
691   };
692 
693   struct D {
694     inline void operator delete(void*) __attribute__((unused));
695   };
696   void h() { C::operator delete(0); } // expected-warning {{marked unused but was used}}
697 
698 #pragma clang diagnostic pop
699 }
700 
701 namespace dr262 { // dr262: yes
702   int f(int = 0, ...);
703   int k = f();
704   int l = f(0);
705   int m = f(0, 0);
706 }
707 
708 namespace dr263 { // dr263: yes
709   struct X {};
710   struct Y {
711 #if __cplusplus < 201103L
712     friend X::X() throw();
713     friend X::~X() throw();
714 #else
715     friend constexpr X::X() noexcept;
716     friend X::~X();
717 #endif
718     Y::Y(); // expected-error {{extra qualification}}
719     Y::~Y(); // expected-error {{extra qualification}}
720   };
721 }
722 
723 // dr265: dup 353
724 // dr266: na
725 // dr269: na
726 // dr270: na
727