Richard Smith | 762bb9d | 2011-10-13 22:29:44 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall %s |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 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 { |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 20 | bool b = noexcept(ConstExpr()) && ThrowSomething(); // expected-error {{cannot be used by non-static data member initializer}} |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 21 | }; |
| 22 | // We can use it now. |
| 23 | bool w = noexcept(ConstExpr()); |
| 24 | |
| 25 | // Much more obviously broken: we can't parse the initializer without already |
| 26 | // knowing whether it produces a noexcept expression. |
| 27 | struct TemplateArg { |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 28 | int n = ExceptionIf<noexcept(TemplateArg())>::f(); // expected-error {{cannot be used by non-static data member initializer}} |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 29 | }; |
| 30 | bool x = noexcept(TemplateArg()); |
| 31 | |
| 32 | // And within a nested class. |
Richard Smith | ce2661f | 2012-11-07 01:14:25 +0000 | [diff] [blame] | 33 | struct Nested { // expected-error {{cannot be used by non-static data member initializer}} |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 34 | struct Inner { |
Richard Smith | ce2661f | 2012-11-07 01:14:25 +0000 | [diff] [blame] | 35 | int n = ExceptionIf<noexcept(Nested())>::f(); // expected-note {{implicit default constructor for 'InClassInitializers::Nested' first required here}} |
| 36 | } inner; |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 37 | }; |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 38 | |
| 39 | struct Nested2 { |
| 40 | struct Inner; |
| 41 | int n = Inner().n; // expected-error {{cannot be used by non-static data member initializer}} |
| 42 | struct Inner { |
Richard Smith | ce2661f | 2012-11-07 01:14:25 +0000 | [diff] [blame] | 43 | int n = ExceptionIf<noexcept(Nested2())>::f(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 44 | } inner; |
| 45 | }; |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 48 | namespace ExceptionSpecification { |
Richard Smith | 6deb820 | 2012-05-02 01:29:43 +0000 | [diff] [blame] | 49 | // A type is permitted to be used in a dynamic exception specification when it |
| 50 | // is still being defined, but isn't complete within such an exception |
| 51 | // specification. |
| 52 | struct Nested { // expected-note {{not complete}} |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 53 | struct T { |
Richard Smith | 6deb820 | 2012-05-02 01:29:43 +0000 | [diff] [blame] | 54 | T() noexcept(!noexcept(Nested())); // expected-error{{incomplete type}} |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 55 | } t; |
| 56 | }; |
| 57 | } |
| 58 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 59 | namespace DefaultArgument { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 60 | struct Default { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 61 | struct T { |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 62 | T(int = ExceptionIf<noexcept(Default())::f()); // expected-error {{call to implicitly-deleted default constructor}} |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 63 | } t; // expected-note {{has no default constructor}} |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 64 | }; |
| 65 | } |
Richard Smith | a4156b8 | 2012-04-21 18:42:51 +0000 | [diff] [blame] | 66 | |
| 67 | namespace ImplicitDtorExceptionSpec { |
| 68 | struct A { |
| 69 | virtual ~A(); |
| 70 | |
| 71 | struct Inner { |
| 72 | ~Inner() throw(); |
| 73 | }; |
| 74 | Inner inner; |
| 75 | }; |
| 76 | |
| 77 | struct B { |
| 78 | virtual ~B() {} // expected-note {{here}} |
| 79 | }; |
| 80 | |
| 81 | struct C : B { |
| 82 | virtual ~C() {} |
| 83 | A a; |
| 84 | }; |
| 85 | |
| 86 | struct D : B { |
| 87 | ~D(); // expected-error {{more lax than base}} |
| 88 | struct E { |
| 89 | ~E(); |
| 90 | struct F { |
| 91 | ~F() throw(A); |
| 92 | } f; |
| 93 | } e; |
| 94 | }; |
| 95 | } |