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