P1286R2: Remove restriction that the exception specification of a

defaulted special member matches the implicit exception specification.

llvm-svn: 360011
This commit is contained in:
Richard Smith 2019-05-06 05:04:56 +00:00
parent 7d02ca4878
commit 2b45b267da
13 changed files with 105 additions and 125 deletions

View File

@ -7786,9 +7786,6 @@ def err_defaulted_special_member_copy_const_param : Error<
def err_defaulted_copy_assign_not_ref : Error<
"the parameter for an explicitly-defaulted copy assignment operator must be an "
"lvalue reference type">;
def err_incorrect_defaulted_exception_spec : Error<
"exception specification of explicitly defaulted "
"%sub{select_special_member_kind}0 does not match the calculated one">;
def err_incorrect_defaulted_constexpr : Error<
"defaulted definition of %sub{select_special_member_kind}0 "
"is not constexpr">;

View File

@ -682,16 +682,6 @@ public:
SmallVector<std::pair<FunctionDecl*, FunctionDecl*>, 2>
DelayedEquivalentExceptionSpecChecks;
/// All the members seen during a class definition which were both
/// explicitly defaulted and had explicitly-specified exception
/// specifications, along with the function type containing their
/// user-specified exception specification. Those exception specifications
/// were overridden with the default specifications, but we still need to
/// check whether they are compatible with the default specification, and
/// we can't do that until the nesting set of class definitions is complete.
SmallVector<std::pair<CXXMethodDecl*, const FunctionProtoType*>, 2>
DelayedDefaultedMemberExceptionSpecs;
typedef llvm::MapVector<const FunctionDecl *,
std::unique_ptr<LateParsedTemplate>>
LateParsedTemplateMapT;
@ -6070,8 +6060,6 @@ public:
void CheckDeductionGuideTemplate(FunctionTemplateDecl *TD);
void CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD);
void CheckExplicitlyDefaultedMemberExceptionSpec(CXXMethodDecl *MD,
const FunctionProtoType *T);
void CheckDelayedMemberExceptionSpecs();
//===--------------------------------------------------------------------===//
@ -10999,9 +10987,6 @@ private:
"there shouldn't be any pending delayed exception spec checks");
assert(S.DelayedEquivalentExceptionSpecChecks.empty() &&
"there shouldn't be any pending delayed exception spec checks");
assert(S.DelayedDefaultedMemberExceptionSpecs.empty() &&
"there shouldn't be any pending delayed defaulted member "
"exception specs");
assert(S.DelayedDllExportClasses.empty() &&
"there shouldn't be any pending delayed DLL export classes");
swapSavedState();
@ -11013,8 +10998,6 @@ private:
SavedOverridingExceptionSpecChecks;
decltype(DelayedEquivalentExceptionSpecChecks)
SavedEquivalentExceptionSpecChecks;
decltype(DelayedDefaultedMemberExceptionSpecs)
SavedDefaultedMemberExceptionSpecs;
decltype(DelayedDllExportClasses) SavedDllExportClasses;
void swapSavedState() {
@ -11022,8 +11005,6 @@ private:
S.DelayedOverridingExceptionSpecChecks);
SavedEquivalentExceptionSpecChecks.swap(
S.DelayedEquivalentExceptionSpecChecks);
SavedDefaultedMemberExceptionSpecs.swap(
S.DelayedDefaultedMemberExceptionSpecs);
SavedDllExportClasses.swap(S.DelayedDllExportClasses);
}
};

View File

@ -958,7 +958,6 @@ void Sema::ActOnEndOfTranslationUnit() {
// incompatible declarations.
assert(DelayedOverridingExceptionSpecChecks.empty());
assert(DelayedEquivalentExceptionSpecChecks.empty());
assert(DelayedDefaultedMemberExceptionSpecs.empty());
// All dllexport classes should have been processed already.
assert(DelayedDllExportClasses.empty());

View File

@ -6636,6 +6636,8 @@ void Sema::CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD) {
// makes such functions always instantiate to constexpr functions. For
// functions which cannot be constexpr (for non-constructors in C++11 and for
// destructors in C++1y), this is checked elsewhere.
//
// FIXME: This should not apply if the member is deleted.
bool Constexpr = defaultedSpecialMemberIsConstexpr(*this, RD, CSM,
HasConstParam);
if ((getLangOpts().CPlusPlus14 ? !isa<CXXDestructorDecl>(MD)
@ -6647,38 +6649,26 @@ void Sema::CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD) {
HadError = true;
}
// and may have an explicit exception-specification only if it is compatible
// with the exception-specification on the implicit declaration.
if (Type->hasExceptionSpec()) {
// Delay the check if this is the first declaration of the special member,
// since we may not have parsed some necessary in-class initializers yet.
if (First) {
// If the exception specification needs to be instantiated, do so now,
// before we clobber it with an EST_Unevaluated specification below.
if (Type->getExceptionSpecType() == EST_Uninstantiated) {
InstantiateExceptionSpec(MD->getBeginLoc(), MD);
Type = MD->getType()->getAs<FunctionProtoType>();
}
DelayedDefaultedMemberExceptionSpecs.push_back(std::make_pair(MD, Type));
} else
CheckExplicitlyDefaultedMemberExceptionSpec(MD, Type);
}
// If a function is explicitly defaulted on its first declaration,
if (First) {
// -- it is implicitly considered to be constexpr if the implicit
// definition would be,
// C++2a [dcl.fct.def.default]p3:
// If a function is explicitly defaulted on its first declaration, it is
// implicitly considered to be constexpr if the implicit declaration
// would be.
MD->setConstexpr(Constexpr);
// -- it is implicitly considered to have the same exception-specification
// as if it had been implicitly declared,
FunctionProtoType::ExtProtoInfo EPI = Type->getExtProtoInfo();
EPI.ExceptionSpec.Type = EST_Unevaluated;
EPI.ExceptionSpec.SourceDecl = MD;
MD->setType(Context.getFunctionType(ReturnType,
llvm::makeArrayRef(&ArgType,
ExpectedParams),
EPI));
if (!Type->hasExceptionSpec()) {
// C++2a [except.spec]p3:
// If a declaration of a function does not have a noexcept-specifier
// [and] is defaulted on its first declaration, [...] the exception
// specification is as specified below
FunctionProtoType::ExtProtoInfo EPI = Type->getExtProtoInfo();
EPI.ExceptionSpec.Type = EST_Unevaluated;
EPI.ExceptionSpec.SourceDecl = MD;
MD->setType(Context.getFunctionType(ReturnType,
llvm::makeArrayRef(&ArgType,
ExpectedParams),
EPI));
}
}
if (ShouldDeleteForTypeMismatch || ShouldDeleteSpecialMember(MD, CSM)) {
@ -6711,43 +6701,12 @@ void Sema::CheckExplicitlyDefaultedSpecialMember(CXXMethodDecl *MD) {
MD->setInvalidDecl();
}
/// Check whether the exception specification provided for an
/// explicitly-defaulted special member matches the exception specification
/// that would have been generated for an implicit special member, per
/// C++11 [dcl.fct.def.default]p2.
void Sema::CheckExplicitlyDefaultedMemberExceptionSpec(
CXXMethodDecl *MD, const FunctionProtoType *SpecifiedType) {
// If the exception specification was explicitly specified but hadn't been
// parsed when the method was defaulted, grab it now.
if (SpecifiedType->getExceptionSpecType() == EST_Unparsed)
SpecifiedType =
MD->getTypeSourceInfo()->getType()->castAs<FunctionProtoType>();
// Compute the implicit exception specification.
CallingConv CC = Context.getDefaultCallingConvention(/*IsVariadic=*/false,
/*IsCXXMethod=*/true);
FunctionProtoType::ExtProtoInfo EPI(CC);
auto IES = computeImplicitExceptionSpec(*this, MD->getLocation(), MD);
EPI.ExceptionSpec = IES.getExceptionSpec();
const FunctionProtoType *ImplicitType = cast<FunctionProtoType>(
Context.getFunctionType(Context.VoidTy, None, EPI));
// Ensure that it matches.
CheckEquivalentExceptionSpec(
PDiag(diag::err_incorrect_defaulted_exception_spec)
<< getSpecialMember(MD), PDiag(),
ImplicitType, SourceLocation(),
SpecifiedType, MD->getLocation());
}
void Sema::CheckDelayedMemberExceptionSpecs() {
decltype(DelayedOverridingExceptionSpecChecks) Overriding;
decltype(DelayedEquivalentExceptionSpecChecks) Equivalent;
decltype(DelayedDefaultedMemberExceptionSpecs) Defaulted;
std::swap(Overriding, DelayedOverridingExceptionSpecChecks);
std::swap(Equivalent, DelayedEquivalentExceptionSpecChecks);
std::swap(Defaulted, DelayedDefaultedMemberExceptionSpecs);
// Perform any deferred checking of exception specifications for virtual
// destructors.
@ -6758,11 +6717,6 @@ void Sema::CheckDelayedMemberExceptionSpecs() {
// special members.
for (auto &Check : Equivalent)
CheckEquivalentExceptionSpec(Check.second, Check.first);
// Check that any explicitly-defaulted methods have exception specifications
// compatible with their implicit exception specifications.
for (auto &Spec : Defaulted)
CheckExplicitlyDefaultedMemberExceptionSpec(Spec.first, Spec.second);
}
namespace {
@ -11398,7 +11352,6 @@ void Sema::ActOnFinishCXXMemberDecls() {
if (Record->isInvalidDecl()) {
DelayedOverridingExceptionSpecChecks.clear();
DelayedEquivalentExceptionSpecChecks.clear();
DelayedDefaultedMemberExceptionSpecs.clear();
return;
}
checkForMultipleExportedDefaultConstructors(*this, Record);

View File

@ -56,8 +56,8 @@ constexpr S5::S5() = default;
static_assert(S5().m == 4, "");
// An explicitly-defaulted function may have an exception specification only if
// it is compatible with the exception specification on an implicit declaration.
// An explicitly-defaulted function may have a different exception specification
// from the exception specification on an implicit declaration.
struct E1 {
E1() noexcept = default;
E1(const E1&) noexcept = default;
@ -67,13 +67,24 @@ struct E1 {
~E1() noexcept = default;
};
struct E2 {
E2() noexcept(false) = default; // expected-error {{exception specification of explicitly defaulted default constructor does not match the calculated one}}
E2(const E2&) noexcept(false) = default; // expected-error {{exception specification of explicitly defaulted copy constructor does not match the calculated one}}
E2(E2&&) noexcept(false) = default; // expected-error {{exception specification of explicitly defaulted move constructor does not match the calculated one}}
E2 &operator=(const E2&) noexcept(false) = default; // expected-error {{exception specification of explicitly defaulted copy assignment operator does not match the calculated one}}
E2 &operator=(E2&&) noexcept(false) = default; // expected-error {{exception specification of explicitly defaulted move assignment operator does not match the calculated one}}
~E2() noexcept(false) = default; // expected-error {{exception specification of explicitly defaulted destructor does not match the calculated one}}
E2() noexcept(false) = default;
E2(const E2&) noexcept(false) = default;
E2(E2&&) noexcept(false) = default;
E2 &operator=(const E2&) noexcept(false) = default;
E2 &operator=(E2&&) noexcept(false) = default;
~E2() noexcept(false) = default;
};
E2 e2;
E2 make_e2() noexcept;
void take_e2(E2&&) noexcept;
static_assert(!noexcept(E2()), "");
static_assert(!noexcept(E2(e2)), "");
static_assert(!noexcept(E2(static_cast<E2&&>(e2))), "");
static_assert(!noexcept(e2 = e2), "");
static_assert(!noexcept(e2 = static_cast<E2&&>(e2)), "");
// FIXME: This expression results in destruction of an E2 temporary; the
// noexcept expression should evaluate to false.
static_assert(noexcept(take_e2(make_e2())), "");
// If a function is explicitly defaulted on its first declaration
// -- it is implicitly considered to have the same exception-specification as

View File

@ -88,3 +88,18 @@ void f() {
}
#endif
} // namespace dr1722
namespace dr1778 { // dr1778: 9
// Superseded by P1286R2.
#if __cplusplus >= 201103L
struct A { A() noexcept(true) = default; };
struct B { B() noexcept(false) = default; };
static_assert(noexcept(A()), "");
static_assert(!noexcept(B()), "");
struct C { A a; C() noexcept(false) = default; };
struct D { B b; D() noexcept(true) = default; };
static_assert(!noexcept(C()), "");
static_assert(noexcept(D()), "");
#endif
}

View File

@ -83,7 +83,12 @@ namespace PR14141 {
Derived &operator=(const Derived&) noexcept(false) = default;
Derived &operator=(Derived&&) noexcept(false) = default;
~Derived() noexcept(false) = default;
};
} d1;
static_assert(!noexcept(Derived()), "");
static_assert(!noexcept(Derived(static_cast<Derived&&>(d1))), "");
static_assert(!noexcept(Derived(d1)), "");
static_assert(!noexcept(d1 = static_cast<Derived&&>(d1)), "");
static_assert(!noexcept(d1 = d1), "");
struct Derived2 : ThrowingBase {
Derived2() = default;
Derived2(const Derived2&) = default;
@ -91,15 +96,21 @@ namespace PR14141 {
Derived2 &operator=(const Derived2&) = default;
Derived2 &operator=(Derived2&&) = default;
~Derived2() = default;
};
} d2;
static_assert(!noexcept(Derived2()), "");
static_assert(!noexcept(Derived2(static_cast<Derived2&&>(d2))), "");
static_assert(!noexcept(Derived2(d2)), "");
static_assert(!noexcept(d2 = static_cast<Derived2&&>(d2)), "");
static_assert(!noexcept(d2 = d2), "");
struct Derived3 : ThrowingBase {
Derived3() noexcept(true) = default; // expected-error {{does not match the calculated}}
Derived3(const Derived3&) noexcept(true) = default; // expected-error {{does not match the calculated}}
Derived3(Derived3&&) noexcept(true) = default; // expected-error {{does not match the calculated}}
Derived3 &operator=(const Derived3&) noexcept(true) = default; // expected-error {{does not match the calculated}}
Derived3 &operator=(Derived3&&) noexcept(true) = default; // expected-error {{does not match the calculated}}
~Derived3() noexcept(true) = default; // expected-error {{does not match the calculated}}
};
Derived3() noexcept(true) = default;
Derived3(const Derived3&) noexcept(true) = default;
Derived3(Derived3&&) noexcept(true) = default;
Derived3 &operator=(const Derived3&) noexcept(true) = default;
Derived3 &operator=(Derived3&&) noexcept(true) = default;
~Derived3() noexcept(true) = default;
} d3;
static_assert(noexcept(Derived3(), Derived3(Derived3()), Derived3(d3), d3 = Derived3(), d3 = d3), "");
}
namespace rdar13017229 {

View File

@ -189,11 +189,11 @@ namespace PR15597 {
~A() noexcept(true) = default;
};
template<typename T> struct B {
B() noexcept(false) = default; // expected-error {{does not match the calculated one}}
~B() noexcept(false) = default; // expected-error {{does not match the calculated one}}
B() noexcept(false) = default;
~B() noexcept(false) = default;
};
A<int> a;
B<int> b; // expected-note {{here}}
B<int> b;
}
namespace PR27941 {
@ -242,3 +242,20 @@ template <typename Type>
E<Type>::E(const int&) {} // expected-error {{definition of explicitly defaulted function}}
}
namespace P1286R2 {
struct X {
X();
};
struct A {
struct B {
B() noexcept(A::value) = default;
X x;
};
decltype(B()) b;
static constexpr bool value = true;
};
A::B b;
static_assert(noexcept(A::B()), "");
}

View File

@ -51,7 +51,7 @@ struct CheckExcSpec {
int n = 0;
};
struct CheckExcSpecFail {
CheckExcSpecFail() noexcept(true) = default; // expected-error {{exception specification of explicitly defaulted default constructor does not match the calculated one}}
CheckExcSpecFail() noexcept(true) = default; // ok, but calls terminate() on exception
ThrowCtor tc = 123;
};

View File

@ -1,5 +1,6 @@
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s -Wno-defaulted-function-deleted
// RUN: %clang_cc1 -std=c++11 -fcxx-exceptions -DCXX_EXCEPTIONS -fsyntax-only -verify %s -Wno-defaulted-function-deleted
// expected-no-diagnostics
template <class _Tp> struct is_nothrow_move_constructible {
static const bool value = false;
@ -20,11 +21,6 @@ class basic_string {
class Foo {
Foo(Foo &&) noexcept = default;
#ifdef CXX_EXCEPTIONS
// expected-error@-2 {{does not match the calculated}}
#else
// expected-no-diagnostics
#endif
Foo &operator=(Foo &&) noexcept = default;
basic_string<allocator<char> > vectorFoo_;
};

View File

@ -4087,7 +4087,7 @@ and <I>POD class</I></td>
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#674">674</a></td>
<td>C++11</td>
<td>&#8220;matching specialization&#8221; for a friend declaration</td>
<td class="svn" align="center">SVN</td>
<td class="full" align="center">Clang 8</td>
</tr>
<tr id="675">
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#675">675</a></td>
@ -9955,19 +9955,19 @@ and <I>POD class</I></td>
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1690">1690</a></td>
<td>C++14</td>
<td>Associated namespace for local type</td>
<td class="full" align="center">Clang 9</td>
<td class="svn" align="center">SVN</td>
</tr>
<tr id="1691">
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1691">1691</a></td>
<td>C++14</td>
<td>Argument-dependent lookup and opaque enumerations</td>
<td class="full" align="center">Clang 9</td>
<td class="svn" align="center">SVN</td>
</tr>
<tr id="1692">
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1692">1692</a></td>
<td>C++14</td>
<td>Associated namespaces of doubly-nested classes</td>
<td class="full" align="center">Clang 9</td>
<td class="svn" align="center">SVN</td>
</tr>
<tr id="1693">
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1693">1693</a></td>
@ -10147,7 +10147,7 @@ and <I>POD class</I></td>
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1722">1722</a></td>
<td>CD4</td>
<td>Should lambda to function pointer conversion function be <TT>noexcept</TT>?</td>
<td class="full" align="center">Clang 9</td>
<td class="svn" align="center">SVN</td>
</tr>
<tr class="open" id="1723">
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1723">1723</a></td>
@ -10483,7 +10483,7 @@ and <I>POD class</I></td>
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1778">1778</a></td>
<td>C++14</td>
<td><I>exception-specification</I> in explicitly-defaulted functions</td>
<td class="none" align="center">Unknown</td>
<td class="svn" align="center">SVN</td>
</tr>
<tr id="1779">
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1779">1779</a></td>
@ -13081,7 +13081,7 @@ and <I>POD class</I></td>
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2211">2211</a></td>
<td>C++17</td>
<td>Hiding by lambda captures and parameters</td>
<td class="svn" align="center">SVN</td>
<td class="full" align="center">Clang 8</td>
</tr>
<tr class="open" id="2212">
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#2212">2212</a></td>
@ -14137,7 +14137,7 @@ and <I>POD class</I></td>
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2387">2387</a></td>
<td>DR</td>
<td>Linkage of const-qualified variable template</td>
<td class="full" align="center">Clang 9</td>
<td class="svn" align="center">SVN</td>
</tr>
<tr class="open" id="2388">
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#2388">2388</a></td>

View File

@ -285,7 +285,7 @@ with <a href="http://libcxx.llvm.org/">libc++</a> or with gcc's libstdc++.
</tr>
<tr> <!-- from Kona 2019-->
<td><a href="http://wg21.link/p1286r2">P1286R2</a> (<a href="#dr">DR</a>)</td>
<td class="none" align="center">No</td>
<td class="svn" align="center">SVN</td>
</tr>
<tr>
<td>Deleted functions</td>

View File

@ -108,7 +108,7 @@ def availability(issue):
if status == 'unknown':
avail = 'Unknown'
avail_style = ' class="none"'
elif status == '8':
elif status == '9':
avail = 'SVN'
avail_style = ' class="svn"'
elif re.match('^[0-9]+\.?[0-9]*', status):