[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / implicit-exception-spec.cpp
bloba15df0675a2714458853fd187539562b591f8099
1 // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall -Wno-unused-local-typedefs %s
3 template<bool b> struct ExceptionIf { static int f(); };
4 template<> struct ExceptionIf<false> { typedef int f; };
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...
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}}
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}}
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}}
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;
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}}
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;
66 namespace ImplicitDtorExceptionSpec {
67 struct A {
68 virtual ~A();
70 struct Inner {
71 ~Inner() throw();
73 Inner inner;
76 struct B {
77 virtual ~B() {} // expected-note {{here}}
80 struct C : B {
81 virtual ~C() {}
82 A a;
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;
96 struct nothrow_t {} nothrow;
97 void *operator new(decltype(sizeof(0)), nothrow_t) noexcept;
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);
109 template<bool NE> struct B : virtual A<NE> {};
111 template<bool NE> struct C : virtual A<NE> {
112 virtual void f() = 0; // expected-note 2{{unimplemented}}
115 template<bool NE> struct D final : C<NE> {
116 void f();
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, "");
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, "");
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>();
141 // ... the above trick doesn't work for this case...
142 struct Cfalse : virtual A<false> {
143 virtual void f() = 0;
145 Cfalse() noexcept;
146 Cfalse(const Cfalse&) noexcept;
147 Cfalse(Cfalse&&) noexcept;
149 Cfalse::Cfalse() noexcept = default;
150 Cfalse::Cfalse(const Cfalse&) noexcept = default;
151 Cfalse::Cfalse(Cfalse&&) noexcept = default;