[clang] 0f59bee - [Sema] Mark ineligibility of special member functions correctly

Roy Jacobson via cfe-commits cfe-commits at lists.llvm.org
Fri May 19 07:29:18 PDT 2023


Author: Roy Jacobson
Date: 2023-05-19T17:29:12+03:00
New Revision: 0f59bee3d58f5dc7914d58aa520e65f3ba986705

URL: https://github.com/llvm/llvm-project/commit/0f59bee3d58f5dc7914d58aa520e65f3ba986705
DIFF: https://github.com/llvm/llvm-project/commit/0f59bee3d58f5dc7914d58aa520e65f3ba986705.diff

LOG: [Sema] Mark ineligibility of special member functions correctly

I checked if the member function declaration was a copy constructor, but it's not sufficient; We need to check
the arguments against the instantiated class.

Fixed https://github.com/llvm/llvm-project/issues/62555

Reviewed By: erichkeane

Differential Revision: https://reviews.llvm.org/D149961

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
    clang/test/SemaCXX/constrained-special-member-functions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b0f848d47a67..1dd3d8a0ed80 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -58,7 +58,8 @@ C++ Specific Potentially Breaking Changes
 
 ABI Changes in This Version
 ---------------------------
-
+- A bug in evaluating the ineligibility of some special member functions has been fixed. This can
+  make some classes trivially copyable that were not trivially copyable before. (`#62555 <https://github.com/llvm/llvm-project/issues/62555>`_)
 
 What's New in Clang |release|?
 ==============================

diff  --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 3c78343eb861..480cefd1efcd 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -2524,9 +2524,6 @@ Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(
         Constructor->getConstexprKind(), InheritedConstructor(),
         TrailingRequiresClause);
     Method->setRangeEnd(Constructor->getEndLoc());
-    if (Constructor->isDefaultConstructor() ||
-        Constructor->isCopyOrMoveConstructor())
-      Method->setIneligibleOrNotSelected(true);
   } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
     Method = CXXDestructorDecl::Create(
         SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo,
@@ -2549,8 +2546,6 @@ Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(
         SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, SC,
         D->UsesFPIntrin(), D->isInlineSpecified(), D->getConstexprKind(),
         D->getEndLoc(), TrailingRequiresClause);
-    if (D->isMoveAssignmentOperator() || D->isCopyAssignmentOperator())
-      Method->setIneligibleOrNotSelected(true);
   }
 
   if (D->isInlined())
@@ -2753,6 +2748,22 @@ Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(
   if (IsExplicitSpecialization && !isFriend)
     SemaRef.CompleteMemberSpecialization(Method, Previous);
 
+  // If the method is a special member function, we need to mark it as
+  // ineligible so that Owner->addDecl() won't mark the class as non trivial.
+  // At the end of the class instantiation, we calculate eligibility again and
+  // then we adjust trivility if needed.
+  // We need this check to happen only after the method parameters are set,
+  // because being e.g. a copy constructor depends on the instantiated
+  // arguments.
+  if (auto *Constructor = dyn_cast<CXXConstructorDecl>(Method)) {
+    if (Constructor->isDefaultConstructor() ||
+        Constructor->isCopyOrMoveConstructor())
+      Method->setIneligibleOrNotSelected(true);
+  } else if (Method->isCopyAssignmentOperator() ||
+             Method->isMoveAssignmentOperator()) {
+    Method->setIneligibleOrNotSelected(true);
+  }
+
   // If there's a function template, let our caller handle it.
   if (FunctionTemplate) {
     // do nothing

diff  --git a/clang/test/SemaCXX/constrained-special-member-functions.cpp b/clang/test/SemaCXX/constrained-special-member-functions.cpp
index 389f80d80e56..8ac17e7838ef 100644
--- a/clang/test/SemaCXX/constrained-special-member-functions.cpp
+++ b/clang/test/SemaCXX/constrained-special-member-functions.cpp
@@ -299,3 +299,16 @@ static_assert(__is_trivial(S<int>));
 static_assert(!__is_trivial(S<D>));
 
 }
+
+namespace GH62555 {
+
+template <bool B>
+struct ExplicitTemplateArgs {
+    ExplicitTemplateArgs(ExplicitTemplateArgs&&) = default;
+    ExplicitTemplateArgs(ExplicitTemplateArgs<false>&&) requires B {};
+};
+
+static_assert(__is_trivially_copyable(ExplicitTemplateArgs<false>));
+static_assert(__is_trivially_copyable(ExplicitTemplateArgs<true>));
+
+}


        


More information about the cfe-commits mailing list