xref: /aosp_15_r20/external/clang/test/SemaTemplate/instantiate-exception-spec-cxx11.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -triple %itanium_abi_triple -std=c++11 -ftemplate-depth 16 -fcxx-exceptions -fexceptions %s
2*67e74705SXin Li 
3*67e74705SXin Li // DR1330: an exception specification for a function template is only
4*67e74705SXin Li // instantiated when it is needed.
5*67e74705SXin Li 
6*67e74705SXin Li // Note: the test is Itanium-specific because it depends on key functions in the
7*67e74705SXin Li // PR12763 namespace.
8*67e74705SXin Li 
9*67e74705SXin Li template<typename T> void f1(T*) throw(T); // expected-error{{incomplete type 'Incomplete' is not allowed in exception specification}}
10*67e74705SXin Li struct Incomplete; // expected-note{{forward}}
11*67e74705SXin Li 
test_f1(Incomplete * incomplete_p,int * int_p)12*67e74705SXin Li void test_f1(Incomplete *incomplete_p, int *int_p) {
13*67e74705SXin Li   f1(int_p);
14*67e74705SXin Li   f1(incomplete_p); // expected-note{{instantiation of exception spec}}
15*67e74705SXin Li }
16*67e74705SXin Li 
17*67e74705SXin Li template<typename T> struct A {
18*67e74705SXin Li   template<typename U> struct B {
19*67e74705SXin Li     static void f() noexcept(A<U>().n);
20*67e74705SXin Li   };
21*67e74705SXin Li 
AA22*67e74705SXin Li   constexpr A() : n(true) {}
23*67e74705SXin Li   bool n;
24*67e74705SXin Li };
25*67e74705SXin Li 
26*67e74705SXin Li static_assert(noexcept(A<int>::B<char>::f()), "");
27*67e74705SXin Li 
28*67e74705SXin Li template<unsigned N> struct S {
29*67e74705SXin Li   static void recurse() noexcept(noexcept(S<N+1>::recurse())); // \
30*67e74705SXin Li   // expected-error {{no member named 'recurse'}} \
31*67e74705SXin Li   // expected-note 9{{instantiation of exception spec}}
32*67e74705SXin Li };
33*67e74705SXin Li decltype(S<0>::recurse()) *pVoid1 = 0; // ok, exception spec not needed
34*67e74705SXin Li decltype(&S<0>::recurse) pFn = 0; // ok, exception spec not needed
35*67e74705SXin Li 
36*67e74705SXin Li template<> struct S<10> {};
37*67e74705SXin Li void (*pFn2)() noexcept = &S<0>::recurse; // expected-note {{instantiation of exception spec}} expected-error {{not superset}}
38*67e74705SXin Li 
39*67e74705SXin Li 
40*67e74705SXin Li namespace dr1330_example {
41*67e74705SXin Li   template <class T> struct A {
42*67e74705SXin Li     void f(...) throw (typename T::X); // expected-error {{'int'}}
43*67e74705SXin Li     void f(int);
44*67e74705SXin Li   };
45*67e74705SXin Li 
main()46*67e74705SXin Li   int main() {
47*67e74705SXin Li     A<int>().f(42);
48*67e74705SXin Li   }
49*67e74705SXin Li 
50*67e74705SXin Li   struct S {
51*67e74705SXin Li     template<typename T>
fdr1330_example::S52*67e74705SXin Li     static int f() noexcept(noexcept(A<T>().f("boo!"))) { return 0; } // \
53*67e74705SXin Li     // expected-note {{instantiation of exception spec}}
54*67e74705SXin Li     typedef decltype(f<S>()) X;
55*67e74705SXin Li   };
56*67e74705SXin Li 
test2()57*67e74705SXin Li   int test2() {
58*67e74705SXin Li     S().f<S>(); // ok
59*67e74705SXin Li     S().f<int>(); // expected-note {{instantiation of exception spec}}
60*67e74705SXin Li   }
61*67e74705SXin Li 
62*67e74705SXin Li   template<typename T>
63*67e74705SXin Li   struct U {
64*67e74705SXin Li     void f() noexcept(T::error);
65*67e74705SXin Li     void (g)() noexcept(T::error);
66*67e74705SXin Li   };
67*67e74705SXin Li   U<int> uint; // ok
68*67e74705SXin Li }
69*67e74705SXin Li 
70*67e74705SXin Li namespace core_19754_example {
71*67e74705SXin Li   template<typename T> T declval() noexcept;
72*67e74705SXin Li 
73*67e74705SXin Li   template<typename T, typename = decltype(T(declval<T&&>()))>
74*67e74705SXin Li   struct is_movable { static const bool value = true; };
75*67e74705SXin Li 
76*67e74705SXin Li   template<typename T>
77*67e74705SXin Li   struct wrap {
78*67e74705SXin Li     T val;
79*67e74705SXin Li     void irrelevant(wrap &p) noexcept(is_movable<T>::value);
80*67e74705SXin Li   };
81*67e74705SXin Li 
82*67e74705SXin Li   template<typename T>
83*67e74705SXin Li   struct base {
basecore_19754_example::base84*67e74705SXin Li      base() {}
85*67e74705SXin Li      base(const typename T::type1 &);
86*67e74705SXin Li      base(const typename T::type2 &);
87*67e74705SXin Li   };
88*67e74705SXin Li 
89*67e74705SXin Li   template<typename T>
90*67e74705SXin Li   struct type1 {
91*67e74705SXin Li      wrap<typename T::base> base;
92*67e74705SXin Li   };
93*67e74705SXin Li 
94*67e74705SXin Li   template<typename T>
95*67e74705SXin Li   struct type2 {
96*67e74705SXin Li      wrap<typename T::base> base;
97*67e74705SXin Li   };
98*67e74705SXin Li 
99*67e74705SXin Li   struct types {
100*67e74705SXin Li      typedef base<types> base;
101*67e74705SXin Li      typedef type1<types> type1;
102*67e74705SXin Li      typedef type2<types> type2;
103*67e74705SXin Li   };
104*67e74705SXin Li 
105*67e74705SXin Li   base<types> val = base<types>();
106*67e74705SXin Li }
107*67e74705SXin Li 
108*67e74705SXin Li namespace pr9485 {
109*67e74705SXin Li   template <typename T> void f1(T) throw(typename T::exception); // expected-note {{candidate}}
110*67e74705SXin Li   template <typename T> void f1(T, int = 0) throw(typename T::noitpecxe); // expected-note {{candidate}}
111*67e74705SXin Li 
112*67e74705SXin Li   template <typename T> void f2(T) noexcept(T::throws); // expected-note {{candidate}}
113*67e74705SXin Li   template <typename T> void f2(T, int = 0) noexcept(T::sworht); // expected-note {{candidate}}
114*67e74705SXin Li 
test()115*67e74705SXin Li   void test() {
116*67e74705SXin Li     f1(0); // expected-error {{ambiguous}}
117*67e74705SXin Li     f2(0); // expected-error {{ambiguous}}
118*67e74705SXin Li   }
119*67e74705SXin Li }
120*67e74705SXin Li 
121*67e74705SXin Li struct Exc1 { char c[4]; };
122*67e74705SXin Li struct Exc2 { double x, y, z; };
123*67e74705SXin Li struct Base {
124*67e74705SXin Li   virtual void f() noexcept; // expected-note {{overridden}}
125*67e74705SXin Li };
126*67e74705SXin Li template<typename T> struct Derived : Base {
127*67e74705SXin Li   void f() noexcept (sizeof(T) == 4); // expected-error {{is more lax}}
128*67e74705SXin Li   void g() noexcept (T::error);
129*67e74705SXin Li };
130*67e74705SXin Li 
131*67e74705SXin Li Derived<Exc1> d1; // ok
132*67e74705SXin Li Derived<Exc2> d2; // expected-note {{in instantiation of}}
133*67e74705SXin Li 
134*67e74705SXin Li // If the vtable for a derived class is used, the exception specification of
135*67e74705SXin Li // any member function which ends up in that vtable is needed, even if it was
136*67e74705SXin Li // declared in a base class.
137*67e74705SXin Li namespace PR12763 {
138*67e74705SXin Li   template<bool *B> struct T {
139*67e74705SXin Li     virtual void f() noexcept (*B); // expected-error {{constant expression}} expected-note {{read of non-const}}
140*67e74705SXin Li   };
141*67e74705SXin Li   bool b; // expected-note {{here}}
142*67e74705SXin Li   struct X : public T<&b> {
143*67e74705SXin Li     virtual void g();
144*67e74705SXin Li   };
g()145*67e74705SXin Li   void X::g() {} // expected-note {{in instantiation of}}
146*67e74705SXin Li }
147*67e74705SXin Li 
148*67e74705SXin Li namespace Variadic {
check()149*67e74705SXin Li   template<bool B> void check() { static_assert(B, ""); }
check()150*67e74705SXin Li   template<bool B, bool B2, bool ...Bs> void check() { static_assert(B, ""); check<B2, Bs...>(); }
151*67e74705SXin Li 
152*67e74705SXin Li   template<typename ...T> void consume(T...);
153*67e74705SXin Li 
f(void (* ...p)()throw (T))154*67e74705SXin Li   template<typename ...T> void f(void (*...p)() throw (T)) {
155*67e74705SXin Li     void (*q[])() = { p... };
156*67e74705SXin Li     consume((p(),0)...);
157*67e74705SXin Li   }
g(void (* ...p)()noexcept (B))158*67e74705SXin Li   template<bool ...B> void g(void (*...p)() noexcept (B)) {
159*67e74705SXin Li     consume((p(),0)...);
160*67e74705SXin Li     check<noexcept(p()) == B ...>();
161*67e74705SXin Li   }
i()162*67e74705SXin Li   template<typename ...T> void i() {
163*67e74705SXin Li     consume([]() throw(T) {} ...);
164*67e74705SXin Li     consume([]() noexcept(sizeof(T) == 4) {} ...);
165*67e74705SXin Li   }
j()166*67e74705SXin Li   template<bool ...B> void j() {
167*67e74705SXin Li     consume([](void (*p)() noexcept(B)) {
168*67e74705SXin Li       void (*q)() noexcept = p; // expected-error {{not superset of source}}
169*67e74705SXin Li     } ...);
170*67e74705SXin Li   }
171*67e74705SXin Li 
z()172*67e74705SXin Li   void z() {
173*67e74705SXin Li     f<int, char, double>(nullptr, nullptr, nullptr);
174*67e74705SXin Li     g<true, false, true>(nullptr, nullptr, nullptr);
175*67e74705SXin Li     i<int, long, short>();
176*67e74705SXin Li     j<true, true>();
177*67e74705SXin Li     j<true, false>(); // expected-note {{in instantiation of}}
178*67e74705SXin Li   }
179*67e74705SXin Li 
180*67e74705SXin Li }
181*67e74705SXin Li 
182*67e74705SXin Li namespace NondefDecls {
f1()183*67e74705SXin Li   template<typename T> void f1() {
184*67e74705SXin Li     int g1(int) noexcept(T::error); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}}
185*67e74705SXin Li   }
186*67e74705SXin Li   template void f1<int>(); // expected-note{{in instantiation of function template specialization 'NondefDecls::f1<int>' requested here}}
187*67e74705SXin Li }
188*67e74705SXin Li 
189