From 1b26ab1b5287ec6c00e8e506657abe438c90a71b Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Mon, 6 May 2019 05:04:56 +0000 Subject: P1286R2: Remove restriction that the exception specification of a defaulted special member matches the implicit exception specification. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@360011 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 3 - include/clang/Sema/Sema.h | 19 ----- lib/Sema/Sema.cpp | 1 - lib/Sema/SemaDeclCXX.cpp | 85 +++++----------------- .../dcl.fct.def/dcl.fct.def.default/p2.cpp | 27 +++++-- test/CXX/drs/dr17xx.cpp | 15 ++++ test/CXX/except/except.spec/p14.cpp | 29 +++++--- test/SemaCXX/cxx0x-defaulted-functions.cpp | 23 +++++- test/SemaCXX/member-init.cpp | 2 +- test/SemaTemplate/exception-spec-crash.cpp | 6 +- www/cxx_dr_status.html | 16 ++-- www/cxx_status.html | 2 +- www/make_cxx_dr_status | 2 +- 13 files changed, 105 insertions(+), 125 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index b94b5d3312..64d863210b 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -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">; diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index a98670cf09..74de4f5881 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -682,16 +682,6 @@ public: SmallVector, 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, 2> - DelayedDefaultedMemberExceptionSpecs; - typedef llvm::MapVector> 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); } }; diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp index 8984398eae..7659d7cc00 100644 --- a/lib/Sema/Sema.cpp +++ b/lib/Sema/Sema.cpp @@ -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()); diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index b8740960fc..8ddc5f12cf 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -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(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(); - } - 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(); - - // 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( - 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); diff --git a/test/CXX/dcl.decl/dcl.fct.def/dcl.fct.def.default/p2.cpp b/test/CXX/dcl.decl/dcl.fct.def/dcl.fct.def.default/p2.cpp index c2f3b5a045..2b7886d383 100644 --- a/test/CXX/dcl.decl/dcl.fct.def/dcl.fct.def.default/p2.cpp +++ b/test/CXX/dcl.decl/dcl.fct.def/dcl.fct.def.default/p2.cpp @@ -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))), ""); +static_assert(!noexcept(e2 = e2), ""); +static_assert(!noexcept(e2 = static_cast(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 diff --git a/test/CXX/drs/dr17xx.cpp b/test/CXX/drs/dr17xx.cpp index bf7458ea81..ca55c42977 100644 --- a/test/CXX/drs/dr17xx.cpp +++ b/test/CXX/drs/dr17xx.cpp @@ -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 +} diff --git a/test/CXX/except/except.spec/p14.cpp b/test/CXX/except/except.spec/p14.cpp index c717d97797..b1c8b207b4 100644 --- a/test/CXX/except/except.spec/p14.cpp +++ b/test/CXX/except/except.spec/p14.cpp @@ -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(d1))), ""); + static_assert(!noexcept(Derived(d1)), ""); + static_assert(!noexcept(d1 = static_cast(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(d2))), ""); + static_assert(!noexcept(Derived2(d2)), ""); + static_assert(!noexcept(d2 = static_cast(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 { diff --git a/test/SemaCXX/cxx0x-defaulted-functions.cpp b/test/SemaCXX/cxx0x-defaulted-functions.cpp index 6346e1c235..45a65440d5 100644 --- a/test/SemaCXX/cxx0x-defaulted-functions.cpp +++ b/test/SemaCXX/cxx0x-defaulted-functions.cpp @@ -189,11 +189,11 @@ namespace PR15597 { ~A() noexcept(true) = default; }; template 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 a; - B b; // expected-note {{here}} + B b; } namespace PR27941 { @@ -242,3 +242,20 @@ template E::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()), ""); +} diff --git a/test/SemaCXX/member-init.cpp b/test/SemaCXX/member-init.cpp index 2c4659afa3..3fcee50e63 100644 --- a/test/SemaCXX/member-init.cpp +++ b/test/SemaCXX/member-init.cpp @@ -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; }; diff --git a/test/SemaTemplate/exception-spec-crash.cpp b/test/SemaTemplate/exception-spec-crash.cpp index ebbb30a2c2..1418ba65e1 100644 --- a/test/SemaTemplate/exception-spec-crash.cpp +++ b/test/SemaTemplate/exception-spec-crash.cpp @@ -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 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 > vectorFoo_; }; diff --git a/www/cxx_dr_status.html b/www/cxx_dr_status.html index 8c4de69983..baddb16dda 100755 --- a/www/cxx_dr_status.html +++ b/www/cxx_dr_status.html @@ -4087,7 +4087,7 @@ and POD class 674 C++11 “matching specialization” for a friend declaration - SVN + Clang 8 675 @@ -9955,19 +9955,19 @@ and POD class 1690 C++14 Associated namespace for local type - Clang 9 + SVN 1691 C++14 Argument-dependent lookup and opaque enumerations - Clang 9 + SVN 1692 C++14 Associated namespaces of doubly-nested classes - Clang 9 + SVN 1693 @@ -10147,7 +10147,7 @@ and POD class 1722 CD4 Should lambda to function pointer conversion function be noexcept? - Clang 9 + SVN 1723 @@ -10483,7 +10483,7 @@ and POD class 1778 C++14 exception-specification in explicitly-defaulted functions - Unknown + SVN 1779 @@ -13081,7 +13081,7 @@ and POD class 2211 C++17 Hiding by lambda captures and parameters - SVN + Clang 8 2212 @@ -14137,7 +14137,7 @@ and POD class 2387 DR Linkage of const-qualified variable template - Clang 9 + SVN 2388 diff --git a/www/cxx_status.html b/www/cxx_status.html index ad7f8ab13f..7d17f7ad66 100755 --- a/www/cxx_status.html +++ b/www/cxx_status.html @@ -285,7 +285,7 @@ with libc++ or with gcc's libstdc++. P1286R2 (DR) - No + SVN Deleted functions diff --git a/www/make_cxx_dr_status b/www/make_cxx_dr_status index bc7efcf7ff..2a5a7a7596 100755 --- a/www/make_cxx_dr_status +++ b/www/make_cxx_dr_status @@ -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): -- cgit v1.2.3