xref: /aosp_15_r20/external/clang/test/CXX/except/except.spec/p14.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -verify -std=c++11 %s
2*67e74705SXin Li struct A { };
3*67e74705SXin Li struct B { };
4*67e74705SXin Li struct C { };
5*67e74705SXin Li 
6*67e74705SXin Li // Destructor
7*67e74705SXin Li struct X0 {
8*67e74705SXin Li   virtual ~X0() throw(A); // expected-note{{overridden virtual function is here}}
9*67e74705SXin Li };
10*67e74705SXin Li struct X1 {
11*67e74705SXin Li   virtual ~X1() throw(B); // expected-note{{overridden virtual function is here}}
12*67e74705SXin Li };
13*67e74705SXin Li struct X2 : public X0, public X1 { }; // expected-error 2{{exception specification of overriding function is more lax than base version}}
14*67e74705SXin Li 
15*67e74705SXin Li // Copy-assignment operator.
16*67e74705SXin Li struct CA0 {
17*67e74705SXin Li   CA0 &operator=(const CA0&) throw(A);
18*67e74705SXin Li };
19*67e74705SXin Li struct CA1 {
20*67e74705SXin Li   CA1 &operator=(const CA1&) throw(B);
21*67e74705SXin Li };
22*67e74705SXin Li struct CA2 : CA0, CA1 { };
23*67e74705SXin Li 
test_CA()24*67e74705SXin Li void test_CA() {
25*67e74705SXin Li   CA2 &(CA2::*captr1)(const CA2&) throw(A, B) = &CA2::operator=;
26*67e74705SXin Li   CA2 &(CA2::*captr2)(const CA2&) throw(A, B, C) = &CA2::operator=;
27*67e74705SXin Li   CA2 &(CA2::*captr3)(const CA2&) throw(A) = &CA2::operator=; // expected-error{{target exception specification is not superset of source}}
28*67e74705SXin Li   CA2 &(CA2::*captr4)(const CA2&) throw(B) = &CA2::operator=; // expected-error{{target exception specification is not superset of source}}
29*67e74705SXin Li }
30*67e74705SXin Li 
31*67e74705SXin Li // In-class member initializers.
32*67e74705SXin Li struct IC0 {
33*67e74705SXin Li   int inClassInit = 0;
34*67e74705SXin Li };
35*67e74705SXin Li struct IC1 {
36*67e74705SXin Li   int inClassInit = (throw B(), 0);
37*67e74705SXin Li };
38*67e74705SXin Li // FIXME: the exception specification on the default constructor is wrong:
39*67e74705SXin Li // we cannot currently compute the set of thrown types.
40*67e74705SXin Li static_assert(noexcept(IC0()), "IC0() does not throw");
41*67e74705SXin Li static_assert(!noexcept(IC1()), "IC1() throws");
42*67e74705SXin Li 
43*67e74705SXin Li namespace PR13381 {
44*67e74705SXin Li   struct NoThrowMove {
45*67e74705SXin Li     NoThrowMove(const NoThrowMove &);
46*67e74705SXin Li     NoThrowMove(NoThrowMove &&) noexcept;
47*67e74705SXin Li     NoThrowMove &operator=(const NoThrowMove &) const;
48*67e74705SXin Li     NoThrowMove &operator=(NoThrowMove &&) const noexcept;
49*67e74705SXin Li   };
50*67e74705SXin Li   struct NoThrowMoveOnly {
51*67e74705SXin Li     NoThrowMoveOnly(NoThrowMoveOnly &&) noexcept;
52*67e74705SXin Li     NoThrowMoveOnly &operator=(NoThrowMoveOnly &&) noexcept;
53*67e74705SXin Li   };
54*67e74705SXin Li   struct X {
55*67e74705SXin Li     const NoThrowMove a;
56*67e74705SXin Li     NoThrowMoveOnly b;
57*67e74705SXin Li 
58*67e74705SXin Li     static X val();
59*67e74705SXin Li     static X &ref();
60*67e74705SXin Li   };
61*67e74705SXin Li   // These both perform a move, but that copy might throw, because it calls
62*67e74705SXin Li   // NoThrowMove's copy constructor (because PR13381::a is const).
63*67e74705SXin Li   static_assert(!noexcept(X(X::val())), "");
64*67e74705SXin Li   static_assert(!noexcept(X::ref() = X::val()), "");
65*67e74705SXin Li }
66*67e74705SXin Li 
67*67e74705SXin Li namespace PR14141 {
68*67e74705SXin Li   // Part of DR1351: the implicit exception-specification is noexcept(false) if
69*67e74705SXin Li   // the set of potential exceptions of the special member function contains
70*67e74705SXin Li   // "any". Hence it is compatible with noexcept(false).
71*67e74705SXin Li   struct ThrowingBase {
72*67e74705SXin Li     ThrowingBase() noexcept(false);
73*67e74705SXin Li     ThrowingBase(const ThrowingBase&) noexcept(false);
74*67e74705SXin Li     ThrowingBase(ThrowingBase&&) noexcept(false);
75*67e74705SXin Li     ThrowingBase &operator=(const ThrowingBase&) noexcept(false);
76*67e74705SXin Li     ThrowingBase &operator=(ThrowingBase&&) noexcept(false);
77*67e74705SXin Li     ~ThrowingBase() noexcept(false);
78*67e74705SXin Li   };
79*67e74705SXin Li   struct Derived : ThrowingBase {
80*67e74705SXin Li     Derived() noexcept(false) = default;
81*67e74705SXin Li     Derived(const Derived&) noexcept(false) = default;
82*67e74705SXin Li     Derived(Derived&&) noexcept(false) = default;
83*67e74705SXin Li     Derived &operator=(const Derived&) noexcept(false) = default;
84*67e74705SXin Li     Derived &operator=(Derived&&) noexcept(false) = default;
85*67e74705SXin Li     ~Derived() noexcept(false) = default;
86*67e74705SXin Li   };
87*67e74705SXin Li   struct Derived2 : ThrowingBase {
88*67e74705SXin Li     Derived2() = default;
89*67e74705SXin Li     Derived2(const Derived2&) = default;
90*67e74705SXin Li     Derived2(Derived2&&) = default;
91*67e74705SXin Li     Derived2 &operator=(const Derived2&) = default;
92*67e74705SXin Li     Derived2 &operator=(Derived2&&) = default;
93*67e74705SXin Li     ~Derived2() = default;
94*67e74705SXin Li   };
95*67e74705SXin Li   struct Derived3 : ThrowingBase {
96*67e74705SXin Li     Derived3() noexcept(true) = default; // expected-error {{does not match the calculated}}
97*67e74705SXin Li     Derived3(const Derived3&) noexcept(true) = default; // expected-error {{does not match the calculated}}
98*67e74705SXin Li     Derived3(Derived3&&) noexcept(true) = default; // expected-error {{does not match the calculated}}
99*67e74705SXin Li     Derived3 &operator=(const Derived3&) noexcept(true) = default; // expected-error {{does not match the calculated}}
100*67e74705SXin Li     Derived3 &operator=(Derived3&&) noexcept(true) = default; // expected-error {{does not match the calculated}}
101*67e74705SXin Li     ~Derived3() noexcept(true) = default; // expected-error {{does not match the calculated}}
102*67e74705SXin Li   };
103*67e74705SXin Li }
104*67e74705SXin Li 
105*67e74705SXin Li namespace rdar13017229 {
106*67e74705SXin Li   struct Base {
~Baserdar13017229::Base107*67e74705SXin Li     virtual ~Base() {}
108*67e74705SXin Li   };
109*67e74705SXin Li 
110*67e74705SXin Li   struct Derived : Base {
111*67e74705SXin Li     virtual ~Derived();
112*67e74705SXin Li     Typo foo(); // expected-error{{unknown type name 'Typo'}}
113*67e74705SXin Li   };
114*67e74705SXin Li }
115*67e74705SXin Li 
116*67e74705SXin Li namespace InhCtor {
117*67e74705SXin Li   template<int> struct X {};
118*67e74705SXin Li   struct Base {
119*67e74705SXin Li     Base(X<0>) noexcept(true);
120*67e74705SXin Li     Base(X<1>) noexcept(false);
121*67e74705SXin Li     Base(X<2>) throw(X<2>);
122*67e74705SXin Li     template<typename T> Base(T) throw(T);
123*67e74705SXin Li   };
124*67e74705SXin Li   template<typename T> struct Throw {
125*67e74705SXin Li     Throw() throw(T);
126*67e74705SXin Li   };
127*67e74705SXin Li   struct Derived1 : Base, X<5> {
128*67e74705SXin Li     using Base::Base;
129*67e74705SXin Li     int n;
130*67e74705SXin Li   };
131*67e74705SXin Li   struct Derived2 : Base, Throw<X<3>> {
132*67e74705SXin Li     using Base::Base;
133*67e74705SXin Li   };
134*67e74705SXin Li   struct Derived3 : Base {
135*67e74705SXin Li     using Base::Base;
136*67e74705SXin Li     Throw<X<4>> x;
137*67e74705SXin Li   };
138*67e74705SXin Li   static_assert(noexcept(Derived1(X<0>())), "");
139*67e74705SXin Li   static_assert(!noexcept(Derived1(X<1>())), "");
140*67e74705SXin Li   static_assert(!noexcept(Derived1(X<2>())), "");
141*67e74705SXin Li   static_assert(!noexcept(Derived2(X<0>())), "");
142*67e74705SXin Li   static_assert(!noexcept(Derived3(X<0>())), "");
143*67e74705SXin Li }
144