blob: ff3d685d912e237e24b9deb3d7e4428c17452990 [file] [log] [blame]
Nico Weber72889432014-09-06 01:25:55 +00001// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall -Wno-unused-local-typedefs %s
Richard Smith938f40b2011-06-11 17:19:42 +00002
3template<bool b> struct ExceptionIf { static int f(); };
4template<> 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.
10namespace 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 {
Reid Klecknerd60b82f2014-11-17 23:36:45 +000020 bool b = noexcept(ConstExpr()) && ThrowSomething(); // expected-error {{cannot use defaulted default constructor of 'ConstExpr' within the class outside of member functions}}
21 // expected-note@-1 {{implicit default constructor for 'InClassInitializers::ConstExpr' first required here}}
Richard Smith938f40b2011-06-11 17:19:42 +000022 };
Richard Smith938f40b2011-06-11 17:19:42 +000023
24 // Much more obviously broken: we can't parse the initializer without already
25 // knowing whether it produces a noexcept expression.
26 struct TemplateArg {
Reid Klecknerd60b82f2014-11-17 23:36:45 +000027 int n = ExceptionIf<noexcept(TemplateArg())>::f(); // expected-error {{cannot use defaulted default constructor of 'TemplateArg' within the class outside of member functions}}
28 // expected-note@-1 {{implicit default constructor for 'InClassInitializers::TemplateArg' first required here}}
Richard Smith938f40b2011-06-11 17:19:42 +000029 };
Richard Smith938f40b2011-06-11 17:19:42 +000030
31 // And within a nested class.
Reid Klecknerd60b82f2014-11-17 23:36:45 +000032 struct Nested { // expected-note {{implicit default constructor for 'InClassInitializers::Nested::Inner' first required here}}
Richard Smith938f40b2011-06-11 17:19:42 +000033 struct Inner {
Reid Klecknerd60b82f2014-11-17 23:36:45 +000034 // expected-error@+1 {{cannot use defaulted default constructor of 'Inner' within 'Nested' outside of member functions}}
Richard Smithe10d3042012-11-07 01:14:25 +000035 int n = ExceptionIf<noexcept(Nested())>::f(); // expected-note {{implicit default constructor for 'InClassInitializers::Nested' first required here}}
36 } inner;
Richard Smith938f40b2011-06-11 17:19:42 +000037 };
Richard Smithd3b5c9082012-07-27 04:22:15 +000038
Reid Klecknerd60b82f2014-11-17 23:36:45 +000039 struct Nested2 { // expected-error {{implicit default constructor for 'InClassInitializers::Nested2' must explicitly initialize the member 'inner' which does not have a default constructor}}
Richard Smithd3b5c9082012-07-27 04:22:15 +000040 struct Inner;
Reid Klecknerd60b82f2014-11-17 23:36:45 +000041 int n = Inner().n; // expected-note {{implicit default constructor for 'InClassInitializers::Nested2::Inner' first required here}}
42 struct Inner { // expected-note {{declared here}}
43 // expected-error@+1 {{cannot use defaulted default constructor of 'Inner' within 'Nested2' outside of member functions}}
Richard Smithe10d3042012-11-07 01:14:25 +000044 int n = ExceptionIf<noexcept(Nested2())>::f();
Reid Klecknerd60b82f2014-11-17 23:36:45 +000045 // expected-note@-1 {{implicit default constructor for 'InClassInitializers::Nested2' first required here}}
46 } inner; // expected-note {{member is declared here}}
Richard Smithd3b5c9082012-07-27 04:22:15 +000047 };
Richard Smith938f40b2011-06-11 17:19:42 +000048}
49
Richard Smith938f40b2011-06-11 17:19:42 +000050namespace ExceptionSpecification {
Richard Smith0b3a4622014-11-13 20:01:57 +000051 // FIXME: This diagnostic is quite useless; we should indicate whose
52 // exception specification we were looking for and why.
53 struct Nested {
Richard Smith938f40b2011-06-11 17:19:42 +000054 struct T {
Richard Smith0b3a4622014-11-13 20:01:57 +000055 T() noexcept(!noexcept(Nested()));
56 } t; // expected-error{{exception specification is not available until end of class definition}}
Richard Smith938f40b2011-06-11 17:19:42 +000057 };
58}
59
Richard Smith938f40b2011-06-11 17:19:42 +000060namespace DefaultArgument {
Richard Smith852265f2012-03-30 20:53:28 +000061 struct Default {
Richard Smith938f40b2011-06-11 17:19:42 +000062 struct T {
Douglas Gregor74f7d502012-02-15 19:33:52 +000063 T(int = ExceptionIf<noexcept(Default())::f()); // expected-error {{call to implicitly-deleted default constructor}}
Richard Smith852265f2012-03-30 20:53:28 +000064 } t; // expected-note {{has no default constructor}}
Richard Smith938f40b2011-06-11 17:19:42 +000065 };
66}
Richard Smith84973e52012-04-21 18:42:51 +000067
68namespace ImplicitDtorExceptionSpec {
69 struct A {
70 virtual ~A();
71
72 struct Inner {
73 ~Inner() throw();
74 };
75 Inner inner;
76 };
77
78 struct B {
79 virtual ~B() {} // expected-note {{here}}
80 };
81
82 struct C : B {
83 virtual ~C() {}
84 A a;
85 };
86
87 struct D : B {
88 ~D(); // expected-error {{more lax than base}}
89 struct E {
90 ~E();
91 struct F {
92 ~F() throw(A);
93 } f;
94 } e;
95 };
96}