blob: 6e8323d1ee2ca31d3bfecb9957142fdc6c743009 [file] [log] [blame]
Hans Wennborgc9bd88e2014-01-14 19:35:09 +00001// RUN: %clang_cc1 -fsyntax-only -verify -triple %itanium_abi_triple -std=c++11 -ftemplate-depth 16 -fcxx-exceptions -fexceptions %s
Richard Smithf623c962012-04-17 00:58:00 +00002
3// DR1330: an exception specification for a function template is only
4// instantiated when it is needed.
5
Hans Wennborg9125b082014-01-13 19:48:13 +00006// Note: the test is Itanium-specific because it depends on key functions in the
7// PR12763 namespace.
8
Richard Smithf623c962012-04-17 00:58:00 +00009template<typename T> void f1(T*) throw(T); // expected-error{{incomplete type 'Incomplete' is not allowed in exception specification}}
10struct Incomplete; // expected-note{{forward}}
11
12void test_f1(Incomplete *incomplete_p, int *int_p) {
13 f1(int_p);
14 f1(incomplete_p); // expected-note{{instantiation of exception spec}}
15}
16
17template<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
26static_assert(noexcept(A<int>::B<char>::f()), "");
27
28template<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};
33decltype(S<0>::recurse()) *pVoid1 = 0; // ok, exception spec not needed
34decltype(&S<0>::recurse) pFn = 0; // ok, exception spec not needed
35
36template<> struct S<10> {};
Richard Smithd3729422012-04-19 00:08:28 +000037void (*pFn2)() noexcept = &S<0>::recurse; // expected-note {{instantiation of exception spec}} expected-error {{not superset}}
Richard Smithf623c962012-04-17 00:58:00 +000038
39
Richard Smithf623c962012-04-17 00:58:00 +000040namespace 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
David Majnemer766e2592013-10-22 04:14:18 +000050 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
Richard Smithf623c962012-04-17 00:58:00 +000057 int test2() {
Richard Smithf623c962012-04-17 00:58:00 +000058 S().f<S>(); // ok
59 S().f<int>(); // expected-note {{instantiation of exception spec}}
60 }
Richard Smith2e321552014-11-12 02:00:47 +000061
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
Richard Smithf623c962012-04-17 00:58:00 +000068}
69
70namespace 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}
Richard Smith79a52e52012-04-17 22:30:01 +0000107
108namespace 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}
Richard Smithd3729422012-04-19 00:08:28 +0000120
121struct Exc1 { char c[4]; };
122struct Exc2 { double x, y, z; };
123struct Base {
124 virtual void f() noexcept; // expected-note {{overridden}}
125};
126template<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
131Derived<Exc1> d1; // ok
132Derived<Exc2> d2; // expected-note {{in instantiation of}}
Richard Smith4ff9ff92012-07-07 06:59:51 +0000133
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.
137namespace 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}
Richard Smith2e321552014-11-12 02:00:47 +0000147
148namespace 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}
Serge Pavlov73c6a242015-08-23 10:22:28 +0000181
182namespace 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