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   template<typename T>
     63   struct U {
     64     void f() noexcept(T::error);
     65     void (g)() noexcept(T::error);
     66   };
     67   U<int> uint; // ok
     68 }
     69 
     70 namespace core_19754_example {
     71   template<typename T> T declval() noexcept;
     72 
     73   template<typename T, typename = decltype(T(declval<T&&>()))>
     74   struct is_movable { static const bool value = true; };
     75 
     76   template<typename T>
     77   struct wrap {
     78     T val;
     79     void irrelevant(wrap &p) noexcept(is_movable<T>::value);
     80   };
     81 
     82   template<typename T>
     83   struct base {
     84      base() {}
     85      base(const typename T::type1 &);
     86      base(const typename T::type2 &);
     87   };
     88 
     89   template<typename T>
     90   struct type1 {
     91      wrap<typename T::base> base;
     92   };
     93 
     94   template<typename T>
     95   struct type2 {
     96      wrap<typename T::base> base;
     97   };
     98 
     99   struct types {
    100      typedef base<types> base;
    101      typedef type1<types> type1;
    102      typedef type2<types> type2;
    103   };
    104 
    105   base<types> val = base<types>();
    106 }
    107 
    108 namespace pr9485 {
    109   template <typename T> void f1(T) throw(typename T::exception); // expected-note {{candidate}}
    110   template <typename T> void f1(T, int = 0) throw(typename T::noitpecxe); // expected-note {{candidate}}
    111 
    112   template <typename T> void f2(T) noexcept(T::throws); // expected-note {{candidate}}
    113   template <typename T> void f2(T, int = 0) noexcept(T::sworht); // expected-note {{candidate}}
    114 
    115   void test() {
    116     f1(0); // expected-error {{ambiguous}}
    117     f2(0); // expected-error {{ambiguous}}
    118   }
    119 }
    120 
    121 struct Exc1 { char c[4]; };
    122 struct Exc2 { double x, y, z; };
    123 struct Base {
    124   virtual void f() noexcept; // expected-note {{overridden}}
    125 };
    126 template<typename T> struct Derived : Base {
    127   void f() noexcept (sizeof(T) == 4); // expected-error {{is more lax}}
    128   void g() noexcept (T::error);
    129 };
    130 
    131 Derived<Exc1> d1; // ok
    132 Derived<Exc2> d2; // expected-note {{in instantiation of}}
    133 
    134 // If the vtable for a derived class is used, the exception specification of
    135 // any member function which ends up in that vtable is needed, even if it was
    136 // declared in a base class.
    137 namespace PR12763 {
    138   template<bool *B> struct T {
    139     virtual void f() noexcept (*B); // expected-error {{constant expression}} expected-note {{read of non-const}}
    140   };
    141   bool b; // expected-note {{here}}
    142   struct X : public T<&b> {
    143     virtual void g();
    144   };
    145   void X::g() {} // expected-note {{in instantiation of}}
    146 }
    147 
    148 namespace Variadic {
    149   template<bool B> void check() { static_assert(B, ""); }
    150   template<bool B, bool B2, bool ...Bs> void check() { static_assert(B, ""); check<B2, Bs...>(); }
    151 
    152   template<typename ...T> void consume(T...);
    153 
    154   template<typename ...T> void f(void (*...p)() throw (T)) {
    155     void (*q[])() = { p... };
    156     consume((p(),0)...);
    157   }
    158   template<bool ...B> void g(void (*...p)() noexcept (B)) {
    159     consume((p(),0)...);
    160     check<noexcept(p()) == B ...>();
    161   }
    162   template<typename ...T> void i() {
    163     consume([]() throw(T) {} ...);
    164     consume([]() noexcept(sizeof(T) == 4) {} ...);
    165   }
    166   template<bool ...B> void j() {
    167     consume([](void (*p)() noexcept(B)) {
    168       void (*q)() noexcept = p; // expected-error {{not superset of source}}
    169     } ...);
    170   }
    171 
    172   void z() {
    173     f<int, char, double>(nullptr, nullptr, nullptr);
    174     g<true, false, true>(nullptr, nullptr, nullptr);
    175     i<int, long, short>();
    176     j<true, true>();
    177     j<true, false>(); // expected-note {{in instantiation of}}
    178   }
    179 
    180 }
    181 
    182 namespace NondefDecls {
    183   template<typename T> void f1() {
    184     int g1(int) noexcept(T::error); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}}
    185   }
    186   template void f1<int>(); // expected-note{{in instantiation of function template specialization 'NondefDecls::f1<int>' requested here}}
    187 }
    188 
    189