Home | History | Annotate | Download | only in SemaTemplate
      1 // RUN: %clang_cc1 -fsyntax-only -verify -triple %itanium_abi_triple -std=c++11 -ftemplate-depth 16 -fcxx-exceptions -fexceptions %s
      2 
      3 // DR1330: an exception specification for a function template is only
      4 // instantiated when it is needed.
      5 
      6 // Note: the test is Itanium-specific because it depends on key functions in the
      7 // PR12763 namespace.
      8 
      9 template<typename T> void f1(T*) throw(T); // expected-error{{incomplete type 'Incomplete' is not allowed in exception specification}}
     10 struct Incomplete; // expected-note{{forward}}
     11 
     12 void test_f1(Incomplete *incomplete_p, int *int_p) {
     13   f1(int_p);
     14   f1(incomplete_p); // expected-note{{instantiation of exception spec}}
     15 }
     16 
     17 template<typename T> struct A {
     18   template<typename U> struct B {
     19     static void f() noexcept(A<U>().n);
     20   };
     21 
     22   constexpr A() : n(true) {}
     23   bool n;
     24 };
     25 
     26 static_assert(noexcept(A<int>::B<char>::f()), "");
     27 
     28 template<unsigned N> struct S {
     29   static void recurse() noexcept(noexcept(S<N+1>::recurse())); // \
     30   // expected-error {{no member named 'recurse'}} \
     31   // expected-note 9{{instantiation of exception spec}}
     32 };
     33 decltype(S<0>::recurse()) *pVoid1 = 0; // ok, exception spec not needed
     34 decltype(&S<0>::recurse) pFn = 0; // ok, exception spec not needed
     35 
     36 template<> struct S<10> {};
     37 void (*pFn2)() noexcept = &S<0>::recurse; // expected-note {{instantiation of exception spec}} expected-error {{not superset}}
     38 
     39 
     40 namespace dr1330_example {
     41   template <class T> struct A {
     42     void f(...) throw (typename T::X); // expected-error {{'int'}}
     43     void f(int);
     44   };
     45 
     46   int main() {
     47     A<int>().f(42);
     48   }
     49 
     50   struct S {
     51     template<typename T>
     52     static int f() noexcept(noexcept(A<T>().f("boo!"))) { return 0; } // \
     53     // expected-note {{instantiation of exception spec}}
     54     typedef decltype(f<S>()) X;
     55   };
     56 
     57   int test2() {
     58     S().f<S>(); // ok
     59     S().f<int>(); // expected-note {{instantiation of exception spec}}
     60   }
     61 }
     62 
     63 namespace core_19754_example {
     64   template<typename T> T declval() noexcept;
     65 
     66   template<typename T, typename = decltype(T(declval<T&&>()))>
     67   struct is_movable { static const bool value = true; };
     68 
     69   template<typename T>
     70   struct wrap {
     71     T val;
     72     void irrelevant(wrap &p) noexcept(is_movable<T>::value);
     73   };
     74 
     75   template<typename T>
     76   struct base {
     77      base() {}
     78      base(const typename T::type1 &);
     79      base(const typename T::type2 &);
     80   };
     81 
     82   template<typename T>
     83   struct type1 {
     84      wrap<typename T::base> base;
     85   };
     86 
     87   template<typename T>
     88   struct type2 {
     89      wrap<typename T::base> base;
     90   };
     91 
     92   struct types {
     93      typedef base<types> base;
     94      typedef type1<types> type1;
     95      typedef type2<types> type2;
     96   };
     97 
     98   base<types> val = base<types>();
     99 }
    100 
    101 namespace pr9485 {
    102   template <typename T> void f1(T) throw(typename T::exception); // expected-note {{candidate}}
    103   template <typename T> void f1(T, int = 0) throw(typename T::noitpecxe); // expected-note {{candidate}}
    104 
    105   template <typename T> void f2(T) noexcept(T::throws); // expected-note {{candidate}}
    106   template <typename T> void f2(T, int = 0) noexcept(T::sworht); // expected-note {{candidate}}
    107 
    108   void test() {
    109     f1(0); // expected-error {{ambiguous}}
    110     f2(0); // expected-error {{ambiguous}}
    111   }
    112 }
    113 
    114 struct Exc1 { char c[4]; };
    115 struct Exc2 { double x, y, z; };
    116 struct Base {
    117   virtual void f() noexcept; // expected-note {{overridden}}
    118 };
    119 template<typename T> struct Derived : Base {
    120   void f() noexcept (sizeof(T) == 4); // expected-error {{is more lax}}
    121   void g() noexcept (T::error);
    122 };
    123 
    124 Derived<Exc1> d1; // ok
    125 Derived<Exc2> d2; // expected-note {{in instantiation of}}
    126 
    127 // If the vtable for a derived class is used, the exception specification of
    128 // any member function which ends up in that vtable is needed, even if it was
    129 // declared in a base class.
    130 namespace PR12763 {
    131   template<bool *B> struct T {
    132     virtual void f() noexcept (*B); // expected-error {{constant expression}} expected-note {{read of non-const}}
    133   };
    134   bool b; // expected-note {{here}}
    135   struct X : public T<&b> {
    136     virtual void g();
    137   };
    138   void X::g() {} // expected-note {{in instantiation of}}
    139 }
    140