| 1 |
// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall -Wno-unused-local-typedefs %s |
| 2 |
|
| 3 |
template<bool b> struct ExceptionIf { static int f(); }; |
| 4 |
template<> struct ExceptionIf<false> { typedef int f; }; |
| 5 |
|
| 6 |
// The exception specification of a defaulted default constructor depends on |
| 7 |
// the contents of in-class member initializers. However, the in-class member |
| 8 |
// initializers can depend on the exception specification of the constructor, |
| 9 |
// since the class is considered complete within them. We reject any such cases. |
| 10 |
namespace InClassInitializers { |
| 11 |
// Noexcept::Noexcept() is implicitly declared as noexcept(false), because it |
| 12 |
// directly invokes ThrowSomething(). However... |
| 13 |
// |
| 14 |
// If noexcept(Noexcept()) is false, then Noexcept() is a constant expression, |
| 15 |
// so noexcept(Noexcept()) is true. But if noexcept(Noexcept()) is true, then |
| 16 |
// Noexcept::Noexcept is not declared constexpr, therefore noexcept(Noexcept()) |
| 17 |
// is false. |
| 18 |
bool ThrowSomething() noexcept(false); |
| 19 |
struct ConstExpr { // expected-error {{default member initializer for 'b' needed}} |
| 20 |
bool b = // expected-note {{declared here}} |
| 21 |
noexcept(ConstExpr()) && ThrowSomething(); // expected-note {{in evaluation of exception spec}} |
| 22 |
}; |
| 23 |
|
| 24 |
// Much more obviously broken: we can't parse the initializer without already |
| 25 |
// knowing whether it produces a noexcept expression. |
| 26 |
struct TemplateArg { // expected-error {{default member initializer for 'n' needed}} |
| 27 |
int n = // expected-note {{declared here}} |
| 28 |
ExceptionIf<noexcept(TemplateArg())>::f(); // expected-note {{in evaluation of exception spec}} |
| 29 |
}; |
| 30 |
|
| 31 |
// And within a nested class. |
| 32 |
struct Nested { |
| 33 |
struct Inner { // expected-error {{default member initializer for 'n' needed}} |
| 34 |
int n = // expected-note {{declared here}} |
| 35 |
ExceptionIf<noexcept(Nested())>::f(); // expected-note {{in evaluation of exception spec}} |
| 36 |
} inner; // expected-note {{in evaluation of exception spec}} |
| 37 |
}; |
| 38 |
|
| 39 |
struct Nested2 { |
| 40 |
struct Inner; |
| 41 |
int n = Inner().n; // expected-note {{in evaluation of exception spec}} |
| 42 |
struct Inner { // expected-error {{initializer for 'n' needed}} |
| 43 |
int n = ExceptionIf<noexcept(Nested2())>::f(); // expected-note {{declared here}} |
| 44 |
} inner; |
| 45 |
}; |
| 46 |
} |
| 47 |
|
| 48 |
namespace ExceptionSpecification { |
| 49 |
struct Nested { |
| 50 |
struct T { |
| 51 |
T() noexcept(!noexcept(Nested())); // expected-note {{in evaluation of exception spec}} |
| 52 |
} t; // expected-error{{exception specification is not available until end of class definition}} |
| 53 |
}; |
| 54 |
} |
| 55 |
|
| 56 |
namespace DefaultArgument { |
| 57 |
// FIXME: We should detect and diagnose the cyclic dependence of |
| 58 |
// noexcept(Default()) on itself here. |
| 59 |
struct Default { |
| 60 |
struct T { |
| 61 |
T(int = ExceptionIf<noexcept(Default())>::f()); |
| 62 |
} t; |
| 63 |
}; |
| 64 |
} |
| 65 |
|
| 66 |
namespace ImplicitDtorExceptionSpec { |
| 67 |
struct A { |
| 68 |
virtual ~A(); |
| 69 |
|
| 70 |
struct Inner { |
| 71 |
~Inner() throw(); |
| 72 |
}; |
| 73 |
Inner inner; |
| 74 |
}; |
| 75 |
|
| 76 |
struct B { |
| 77 |
virtual ~B() {} // expected-note {{here}} |
| 78 |
}; |
| 79 |
|
| 80 |
struct C : B { |
| 81 |
virtual ~C() {} |
| 82 |
A a; |
| 83 |
}; |
| 84 |
|
| 85 |
struct D : B { |
| 86 |
~D(); // expected-error {{more lax than base}} |
| 87 |
struct E { |
| 88 |
~E(); |
| 89 |
struct F { |
| 90 |
~F() throw(A); |
| 91 |
} f; |
| 92 |
} e; |
| 93 |
}; |
| 94 |
} |
| 95 |
|
| 96 |
struct nothrow_t {} nothrow; |
| 97 |
void *operator new(decltype(sizeof(0)), nothrow_t) noexcept; |
| 98 |
|
| 99 |
namespace PotentiallyConstructed { |
| 100 |
template<bool NE> struct A { |
| 101 |
A() noexcept(NE); |
| 102 |
A(const A&) noexcept(NE); |
| 103 |
A(A&&) noexcept(NE); |
| 104 |
A &operator=(const A&) noexcept(NE); |
| 105 |
A &operator=(A&&) noexcept(NE); |
| 106 |
~A() noexcept(NE); |
| 107 |
}; |
| 108 |
|
| 109 |
template<bool NE> struct B : virtual A<NE> {}; |
| 110 |
|
| 111 |
template<bool NE> struct C : virtual A<NE> { |
| 112 |
virtual void f() = 0; // expected-note 2{{unimplemented}} |
| 113 |
}; |
| 114 |
|
| 115 |
template<bool NE> struct D final : C<NE> { |
| 116 |
void f(); |
| 117 |
}; |
| 118 |
|
| 119 |
template<typename T, bool A, bool B, bool C, bool D, bool E, bool F> void check() { |
| 120 |
T *p = nullptr; |
| 121 |
T &a = *p; |
| 122 |
static_assert(noexcept(a = a) == D, ""); |
| 123 |
static_assert(noexcept(a = static_cast<T&&>(a)) == E, ""); |
| 124 |
static_assert(noexcept(delete &a) == F, ""); |
| 125 |
|
| 126 |
// These are last because the first failure here causes instantiation to bail out. |
| 127 |
static_assert(noexcept(new (nothrow) T()) == A, ""); // expected-error 2{{abstract}} |
| 128 |
static_assert(noexcept(new (nothrow) T(a)) == B, ""); |
| 129 |
static_assert(noexcept(new (nothrow) T(static_cast<T&&>(a))) == C, ""); |
| 130 |
} |
| 131 |
|
| 132 |
template void check<A<false>, 0, 0, 0, 0, 0, 0>(); |
| 133 |
template void check<A<true >, 1, 1, 1, 1, 1, 1>(); |
| 134 |
template void check<B<false>, 0, 0, 0, 0, 0, 0>(); |
| 135 |
template void check<B<true >, 1, 1, 1, 1, 1, 1>(); |
| 136 |
template void check<C<false>, 1, 1, 1, 0, 0, 0>(); // expected-note {{instantiation}} |
| 137 |
template void check<C<true >, 1, 1, 1, 1, 1, 1>(); // expected-note {{instantiation}} |
| 138 |
template void check<D<false>, 0, 0, 0, 0, 0, 0>(); |
| 139 |
template void check<D<true >, 1, 1, 1, 1, 1, 1>(); |
| 140 |
|
| 141 |
// ... the above trick doesn't work for this case... |
| 142 |
struct Cfalse : virtual A<false> { |
| 143 |
virtual void f() = 0; |
| 144 |
|
| 145 |
Cfalse() noexcept; |
| 146 |
Cfalse(const Cfalse&) noexcept; |
| 147 |
Cfalse(Cfalse&&) noexcept; |
| 148 |
}; |
| 149 |
Cfalse::Cfalse() noexcept = default; |
| 150 |
Cfalse::Cfalse(const Cfalse&) noexcept = default; |
| 151 |
Cfalse::Cfalse(Cfalse&&) noexcept = default; |
| 152 |
} |