[llvm-branch-commits] [clang] 3bb5ebc - [Clang] Adjust triviality computation in QualType::isTrivialType to C++20 cases.

Tom Stellard via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Mar 3 23:57:48 PST 2023


Author: Roy Jacobson
Date: 2023-03-03T23:57:20-08:00
New Revision: 3bb5ebc4eddf517d7e36c9b61dd7474e202cf216

URL: https://github.com/llvm/llvm-project/commit/3bb5ebc4eddf517d7e36c9b61dd7474e202cf216
DIFF: https://github.com/llvm/llvm-project/commit/3bb5ebc4eddf517d7e36c9b61dd7474e202cf216.diff

LOG: [Clang] Adjust triviality computation in QualType::isTrivialType to C++20 cases.

Up to C++20, hasDefaultConstructor and !hasNonTrivialDefaultConstructor together implied
hasTrivialDefaultConstructor. In C++20, a constructor might be ineligible and can set
hasDefaultConstructor without setting hasNonTrivialDefaultConstructor.

Fix this by querying hasTrivialDefaultConstructor instead of hasDefaultConstructor.

I'd like to backport this to Clang 16.
I only change isTrivialType and in a way that should only affect code that uses
constrained constructors, so I think this is relatively safe to backport.

Fixes https://github.com/llvm/llvm-project/issues/60697

Reviewed By: aaron.ballman

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

Added: 
    

Modified: 
    clang/lib/AST/Type.cpp
    clang/test/SemaCXX/constrained-special-member-functions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index a713d6e3bd030..54e62a1939f74 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -2487,11 +2487,13 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
     return true;
   if (const auto *RT = CanonicalType->getAs<RecordType>()) {
     if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
-      // C++11 [class]p6:
-      //   A trivial class is a class that has a default constructor,
-      //   has no non-trivial default constructors, and is trivially
-      //   copyable.
-      return ClassDecl->hasDefaultConstructor() &&
+      // C++20 [class]p6:
+      //   A trivial class is a class that is trivially copyable, and
+      //     has one or more eligible default constructors such that each is
+      //     trivial.
+      // FIXME: We should merge this definition of triviality into
+      // CXXRecordDecl::isTrivial. Currently it computes the wrong thing.
+      return ClassDecl->hasTrivialDefaultConstructor() &&
              !ClassDecl->hasNonTrivialDefaultConstructor() &&
              ClassDecl->isTriviallyCopyable();
     }

diff  --git a/clang/test/SemaCXX/constrained-special-member-functions.cpp b/clang/test/SemaCXX/constrained-special-member-functions.cpp
index 69d50eb04c572..389f80d80e563 100644
--- a/clang/test/SemaCXX/constrained-special-member-functions.cpp
+++ b/clang/test/SemaCXX/constrained-special-member-functions.cpp
@@ -265,3 +265,37 @@ static_assert(__is_trivially_constructible(C), "");
 static_assert(__is_trivially_constructible(D), "");
 
 }
+
+namespace GH60697 {
+
+template <class T>
+struct X {
+    X() requires false = default;
+};
+static_assert(!__is_trivial(X<int>));
+
+template <class T>
+struct S {
+    S() requires(__is_trivially_constructible(T)) = default;
+
+    S() requires(!__is_trivially_constructible(T) &&
+                  __is_constructible(T)) {}
+
+    T t;
+};
+
+struct D {
+    D(int i) : i(i) {}
+    int i;
+};
+static_assert(!__is_trivially_constructible(D));
+static_assert(!__is_constructible(D));
+static_assert(!__is_trivial(D));
+
+static_assert(!__is_trivially_constructible(S<D>));
+static_assert(!__is_constructible(S<D>));
+
+static_assert(__is_trivial(S<int>));
+static_assert(!__is_trivial(S<D>));
+
+}


        


More information about the llvm-branch-commits mailing list