[clang] Moves destructor type-trait deduction to a public function (PR #238456)

Christopher Di Bella via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 11:53:37 PST 2026


https://github.com/cjdb updated https://github.com/llvm/llvm-project/pull/238456

>From 058eab4922b0811166e4c71046d4fb8d722c9029 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Tue, 3 Nov 2026 01:39:02 +0000
Subject: [PATCH 1/5] Moves destructor type-trait deduction to a public
 function

This provides a canonical way to determine a type's destructor
information for both the standard library and projects using Clang as a
library.
---
 clang/include/clang/Sema/Sema.h   |  32 +++++++++
 clang/lib/Sema/SemaTypeTraits.cpp | 115 ++++++++++++++++++------------
 2 files changed, 102 insertions(+), 45 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index ba215e7e18d7ba..c863e6cdf3406b 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -16057,6 +16057,38 @@ class Sema final : public SemaBase {
   ///@}
 };
 
+enum class DestructorFormat {
+  // SMF is deleted, private, protected, or otherwise inaccessible.
+  NotAvailable,
+
+  // SMF is trivial.
+  Trivial,
+
+  // SMF is non-trivial and nothrow.
+  NonTrivial,
+};
+
+/// Returns whether a type is trivially-destructible, non-trivially
+/// destructible, or cannot be destroyed in the current context.
+///
+/// It is possible for a type to have a trivial destructor without being
+/// trivially-destructible. For example, `struct S { ~S() = delete; };`
+/// has a trivial destructor: its destructor is non-virtual, all of its
+/// bases have trivial destructors, and all of its members are trivially
+/// destructible. `S` isn't trivially-destructible because its destructor
+/// has been deleted. Similarly, `class C { ~C() = default; }` isn't
+/// trivially-destructible because `~C()`---although trivial for the
+/// same reasons---is private.
+///
+/// This would ideally be placed in `QualType`, but libClangBasic
+/// doesn't include `Sema`.
+DestructorFormat GetDestructorFormat(Sema &S, const ASTContext &C,
+                                     SourceLocation Loc, QualType T);
+
+/// Returns whether a type's destructor is nothrow.
+bool IsDestructorNoThrow(Sema &S, const ASTContext &C, SourceLocation Loc,
+                         QualType T);
+
 DeductionFailureInfo
 MakeDeductionFailureInfo(ASTContext &Context, TemplateDeductionResult TDK,
                          sema::TemplateDeductionInfo &Info);
diff --git a/clang/lib/Sema/SemaTypeTraits.cpp b/clang/lib/Sema/SemaTypeTraits.cpp
index cb30ff9fd02da9..91328c09750e8d 100644
--- a/clang/lib/Sema/SemaTypeTraits.cpp
+++ b/clang/lib/Sema/SemaTypeTraits.cpp
@@ -653,6 +653,67 @@ static ComparisonCategoryResult EvaluateTypeOrder(Sema &S, QualType LHS,
                     : ComparisonCategoryResult::Less;
 }
 
+DestructorFormat clang::GetDestructorFormat(Sema &S, const ASTContext &C,
+                                            SourceLocation Loc, QualType T) {
+  // C++14 [meta.unary.prop]:
+  //   For reference types, is_destructible<T>::value is true.
+  if (T->isReferenceType())
+    return DestructorFormat::Trivial;
+
+  // Objective-C++ ARC: autorelease types don't require destruction.
+  if (T->isObjCLifetimeType() &&
+      T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing)
+    return DestructorFormat::Trivial;
+
+  // C++14 [meta.unary.prop]:
+  //   For incomplete types and function types, is_destructible<T>::value is
+  //   false.
+  if (T->isIncompleteType() || T->isFunctionType())
+    return DestructorFormat::NotAvailable;
+
+  DestructorFormat Result = DestructorFormat::Trivial;
+
+  // A type that requires destruction (via a non-trivial destructor or ARC
+  // lifetime semantics) is not trivially-destructible.
+  if (T.isDestructedType())
+    Result = DestructorFormat::NonTrivial;
+
+  // C++14 [meta.unary.prop]:
+  //   For object types and given U equal to remove_all_extents_t<T>, if the
+  //   expression std::declval<U&>().~U() is well-formed when treated as an
+  //   unevaluated operand (Clause 5), then is_destructible<T>::value is true
+  auto *RD = C.getBaseElementType(T)->getAsCXXRecordDecl();
+  if (!RD)
+    return Result;
+
+  CXXDestructorDecl *Destructor = S.LookupDestructor(RD);
+  if (!Destructor)
+    return DestructorFormat::NotAvailable;
+
+  //  C++14 [dcl.fct.def.delete]p2:
+  //    A program that refers to a deleted function implicitly or
+  //    explicitly, other than to declare it, is ill-formed.
+  if (Destructor->isDeleted())
+    return DestructorFormat::NotAvailable;
+
+  if (C.getLangOpts().AccessControl && Destructor->getAccess() != AS_public)
+    return DestructorFormat::NotAvailable;
+
+  return Result;
+}
+
+bool clang::IsDestructorNoThrow(Sema &S, const ASTContext &C,
+                                SourceLocation Loc, QualType T) {
+  auto *RD = C.getBaseElementType(T)->getAsCXXRecordDecl();
+  if (!RD)
+    return true;
+
+  CXXDestructorDecl *Destructor = S.LookupDestructor(RD);
+  auto *CPT = Destructor->getType()->castAs<FunctionProtoType>();
+  CPT = S.ResolveExceptionSpec(Loc, CPT);
+  return CPT && CPT->isNothrow();
+}
+
 static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
                                    SourceLocation KeyLoc,
                                    TypeSourceInfo *TInfo) {
@@ -878,52 +939,16 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
              !RD->hasNonTrivialCopyAssignment();
     return false;
   case UTT_IsDestructible:
+    return clang::GetDestructorFormat(Self, C, KeyLoc, T) !=
+           DestructorFormat::NotAvailable;
   case UTT_IsTriviallyDestructible:
-  case UTT_IsNothrowDestructible:
-    // C++14 [meta.unary.prop]:
-    //   For reference types, is_destructible<T>::value is true.
-    if (T->isReferenceType())
-      return true;
-
-    // Objective-C++ ARC: autorelease types don't require destruction.
-    if (T->isObjCLifetimeType() &&
-        T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing)
-      return true;
-
-    // C++14 [meta.unary.prop]:
-    //   For incomplete types and function types, is_destructible<T>::value is
-    //   false.
-    if (T->isIncompleteType() || T->isFunctionType())
-      return false;
-
-    // A type that requires destruction (via a non-trivial destructor or ARC
-    // lifetime semantics) is not trivially-destructible.
-    if (UTT == UTT_IsTriviallyDestructible && T.isDestructedType())
-      return false;
-
-    // C++14 [meta.unary.prop]:
-    //   For object types and given U equal to remove_all_extents_t<T>, if the
-    //   expression std::declval<U&>().~U() is well-formed when treated as an
-    //   unevaluated operand (Clause 5), then is_destructible<T>::value is true
-    if (auto *RD = C.getBaseElementType(T)->getAsCXXRecordDecl()) {
-      CXXDestructorDecl *Destructor = Self.LookupDestructor(RD);
-      if (!Destructor)
-        return false;
-      //  C++14 [dcl.fct.def.delete]p2:
-      //    A program that refers to a deleted function implicitly or
-      //    explicitly, other than to declare it, is ill-formed.
-      if (Destructor->isDeleted())
-        return false;
-      if (C.getLangOpts().AccessControl && Destructor->getAccess() != AS_public)
-        return false;
-      if (UTT == UTT_IsNothrowDestructible) {
-        auto *CPT = Destructor->getType()->castAs<FunctionProtoType>();
-        CPT = Self.ResolveExceptionSpec(KeyLoc, CPT);
-        if (!CPT || !CPT->isNothrow())
-          return false;
-      }
-    }
-    return true;
+    return clang::GetDestructorFormat(Self, C, KeyLoc, T) ==
+           DestructorFormat::Trivial;
+  case UTT_IsNothrowDestructible: {
+    auto Format = clang::GetDestructorFormat(Self, C, KeyLoc, T);
+    return Format != DestructorFormat::NotAvailable &&
+           clang::IsDestructorNoThrow(Self, C, KeyLoc, T);
+  }
 
   case UTT_HasTrivialDestructor:
     // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html

>From 992a712cc0fcad66a7f6bc0bc67b876d80a82b9b Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Fri, 6 Nov 2026 18:33:49 +0000
Subject: [PATCH 2/5] Have `GetDestroyKind` return destructor info too

---
 clang/include/clang/Sema/Sema.h   | 15 ++++----
 clang/lib/Sema/SemaTypeTraits.cpp | 58 +++++++++++++++++--------------
 2 files changed, 40 insertions(+), 33 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index ff0a2d2893651b..ccaf658e404de6 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -16112,7 +16112,7 @@ class Sema final : public SemaBase {
   ///@}
 };
 
-enum class DestructorFormat {
+enum class DestructorKind {
   // SMF is deleted, private, protected, or otherwise inaccessible.
   NotAvailable,
 
@@ -16124,7 +16124,8 @@ enum class DestructorFormat {
 };
 
 /// Returns whether a type is trivially-destructible, non-trivially
-/// destructible, or cannot be destroyed in the current context.
+/// destructible, or cannot be destroyed in the current context, and a
+/// pointer type's destructor, if it is a class type (otherwise null).
 ///
 /// It is possible for a type to have a trivial destructor without being
 /// trivially-destructible. For example, `struct S { ~S() = delete; };`
@@ -16137,12 +16138,14 @@ enum class DestructorFormat {
 ///
 /// This would ideally be placed in `QualType`, but libClangBasic
 /// doesn't include `Sema`.
-DestructorFormat GetDestructorFormat(Sema &S, const ASTContext &C,
-                                     SourceLocation Loc, QualType T);
+std::pair<DestructorKind, const CXXDestructorDecl *>
+GetDestructorKind(Sema &S, const ASTContext &C, SourceLocation Loc, QualType T);
 
-/// Returns whether a type's destructor is nothrow.
+/// Returns true if a class type's destructor is nothrow.
+///
+///
 bool IsDestructorNoThrow(Sema &S, const ASTContext &C, SourceLocation Loc,
-                         QualType T);
+                         const CXXDestructorDecl *Destructor);
 
 DeductionFailureInfo
 MakeDeductionFailureInfo(ASTContext &Context, TemplateDeductionResult TDK,
diff --git a/clang/lib/Sema/SemaTypeTraits.cpp b/clang/lib/Sema/SemaTypeTraits.cpp
index 91328c09750e8d..ec17ac897fe01e 100644
--- a/clang/lib/Sema/SemaTypeTraits.cpp
+++ b/clang/lib/Sema/SemaTypeTraits.cpp
@@ -653,30 +653,31 @@ static ComparisonCategoryResult EvaluateTypeOrder(Sema &S, QualType LHS,
                     : ComparisonCategoryResult::Less;
 }
 
-DestructorFormat clang::GetDestructorFormat(Sema &S, const ASTContext &C,
-                                            SourceLocation Loc, QualType T) {
+std::pair<DestructorKind, const CXXDestructorDecl *>
+clang::GetDestructorKind(Sema &S, const ASTContext &C, SourceLocation Loc,
+                         QualType T) {
   // C++14 [meta.unary.prop]:
   //   For reference types, is_destructible<T>::value is true.
   if (T->isReferenceType())
-    return DestructorFormat::Trivial;
+    return {DestructorKind::Trivial, nullptr};
 
   // Objective-C++ ARC: autorelease types don't require destruction.
   if (T->isObjCLifetimeType() &&
       T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing)
-    return DestructorFormat::Trivial;
+    return {DestructorKind::Trivial, nullptr};
 
   // C++14 [meta.unary.prop]:
   //   For incomplete types and function types, is_destructible<T>::value is
   //   false.
   if (T->isIncompleteType() || T->isFunctionType())
-    return DestructorFormat::NotAvailable;
+    return {DestructorKind::NotAvailable, nullptr};
 
-  DestructorFormat Result = DestructorFormat::Trivial;
+  DestructorKind Result = DestructorKind::Trivial;
 
   // A type that requires destruction (via a non-trivial destructor or ARC
   // lifetime semantics) is not trivially-destructible.
   if (T.isDestructedType())
-    Result = DestructorFormat::NonTrivial;
+    Result = DestructorKind::NonTrivial;
 
   // C++14 [meta.unary.prop]:
   //   For object types and given U equal to remove_all_extents_t<T>, if the
@@ -684,31 +685,29 @@ DestructorFormat clang::GetDestructorFormat(Sema &S, const ASTContext &C,
   //   unevaluated operand (Clause 5), then is_destructible<T>::value is true
   auto *RD = C.getBaseElementType(T)->getAsCXXRecordDecl();
   if (!RD)
-    return Result;
+    return {Result, nullptr};
 
   CXXDestructorDecl *Destructor = S.LookupDestructor(RD);
   if (!Destructor)
-    return DestructorFormat::NotAvailable;
+    return {DestructorKind::NotAvailable, Destructor};
 
   //  C++14 [dcl.fct.def.delete]p2:
   //    A program that refers to a deleted function implicitly or
   //    explicitly, other than to declare it, is ill-formed.
   if (Destructor->isDeleted())
-    return DestructorFormat::NotAvailable;
+    return {DestructorKind::NotAvailable, Destructor};
 
   if (C.getLangOpts().AccessControl && Destructor->getAccess() != AS_public)
-    return DestructorFormat::NotAvailable;
+    return {DestructorKind::NotAvailable, Destructor};
 
-  return Result;
+  return {Result, Destructor};
 }
 
 bool clang::IsDestructorNoThrow(Sema &S, const ASTContext &C,
-                                SourceLocation Loc, QualType T) {
-  auto *RD = C.getBaseElementType(T)->getAsCXXRecordDecl();
-  if (!RD)
-    return true;
-
-  CXXDestructorDecl *Destructor = S.LookupDestructor(RD);
+                                SourceLocation Loc,
+                                const CXXDestructorDecl *Destructor) {
+  assert(Destructor != nullptr && "this function should only be called when "
+                                  "the type has a destructor declaration");
   auto *CPT = Destructor->getType()->castAs<FunctionProtoType>();
   CPT = S.ResolveExceptionSpec(Loc, CPT);
   return CPT && CPT->isNothrow();
@@ -938,16 +937,21 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
       return RD->hasTrivialCopyAssignment() &&
              !RD->hasNonTrivialCopyAssignment();
     return false;
-  case UTT_IsDestructible:
-    return clang::GetDestructorFormat(Self, C, KeyLoc, T) !=
-           DestructorFormat::NotAvailable;
-  case UTT_IsTriviallyDestructible:
-    return clang::GetDestructorFormat(Self, C, KeyLoc, T) ==
-           DestructorFormat::Trivial;
+  case UTT_IsDestructible: {
+    auto [Kind, _] = clang::GetDestructorKind(Self, C, KeyLoc, T);
+    return Kind != DestructorKind::NotAvailable;
+  }
+  case UTT_IsTriviallyDestructible: {
+    auto [Kind, _] = clang::GetDestructorKind(Self, C, KeyLoc, T);
+    return Kind == DestructorKind::Trivial;
+  }
   case UTT_IsNothrowDestructible: {
-    auto Format = clang::GetDestructorFormat(Self, C, KeyLoc, T);
-    return Format != DestructorFormat::NotAvailable &&
-           clang::IsDestructorNoThrow(Self, C, KeyLoc, T);
+    auto [Format, Destructor] = clang::GetDestructorKind(Self, C, KeyLoc, T);
+    if (Format == DestructorKind::NotAvailable) {
+      return false;
+    }
+    return Destructor == nullptr ||
+           clang::IsDestructorNoThrow(Self, C, KeyLoc, T, Destructor);
   }
 
   case UTT_HasTrivialDestructor:

>From 7a54b956426f6db67935929cf8b3387e4eee3f2d Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Tue, 3 Nov 2026 23:31:59 +0000
Subject: [PATCH 3/5] Rework interface so that it's more self-contained and
 less error-prone

---
 clang/include/clang/Sema/Sema.h   |  67 ++++++++++---------
 clang/lib/Sema/SemaTypeTraits.cpp | 107 ++++++++++++++++++------------
 2 files changed, 101 insertions(+), 73 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index ccaf658e404de6..b9d294287dc85c 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -16112,40 +16112,47 @@ class Sema final : public SemaBase {
   ///@}
 };
 
-enum class DestructorKind {
-  // SMF is deleted, private, protected, or otherwise inaccessible.
-  NotAvailable,
+/// Evaluates whether a type is trivially-destructible, non-trivially
+/// destructible, or cannot be destroyed in the current context. Caches
+/// the results from evaluation to allow for independently determining
+/// if the type's destructor throws.
+class DestructibleTraits {
+public:
+  /// Indicates whether a type is trivially-destructible, non-trivially
+  /// destructible, or not destructible.
+  enum class Destructibility : std::uint8_t {
+    /// Type is not destructible from this context.
+    NotAvailable,
 
-  // SMF is trivial.
-  Trivial,
+    /// Type is trivially-destructible.
+    TriviallyDestructible,
 
-  // SMF is non-trivial and nothrow.
-  NonTrivial,
-};
+    /// Type is destructible, but not trivially-destructible.
+    Destructible,
+  };
 
-/// Returns whether a type is trivially-destructible, non-trivially
-/// destructible, or cannot be destroyed in the current context, and a
-/// pointer type's destructor, if it is a class type (otherwise null).
-///
-/// It is possible for a type to have a trivial destructor without being
-/// trivially-destructible. For example, `struct S { ~S() = delete; };`
-/// has a trivial destructor: its destructor is non-virtual, all of its
-/// bases have trivial destructors, and all of its members are trivially
-/// destructible. `S` isn't trivially-destructible because its destructor
-/// has been deleted. Similarly, `class C { ~C() = default; }` isn't
-/// trivially-destructible because `~C()`---although trivial for the
-/// same reasons---is private.
-///
-/// This would ideally be placed in `QualType`, but libClangBasic
-/// doesn't include `Sema`.
-std::pair<DestructorKind, const CXXDestructorDecl *>
-GetDestructorKind(Sema &S, const ASTContext &C, SourceLocation Loc, QualType T);
+  DestructibleTraits(Sema &S, const ASTContext &C, SourceLocation Loc,
+                     QualType T);
 
-/// Returns true if a class type's destructor is nothrow.
-///
-///
-bool IsDestructorNoThrow(Sema &S, const ASTContext &C, SourceLocation Loc,
-                         const CXXDestructorDecl *Destructor);
+  /// Returns the raw destructor kind.
+  Destructibility GetKind() const;
+
+  /// Returns true if, and only if, the type is destructible.
+  bool IsDestructible() const;
+
+  /// Returns true if, and only if, the type is trivially-destructible.
+  bool IsTriviallyDestructible() const;
+
+  /// Returns true if, and only if, the type is destructible, and the chosen
+  /// destructor is non-throwing.
+  bool IsNoThrowDestructible(Sema &S, const ASTContext &C);
+
+private:
+  SourceLocation Loc;
+  Destructibility Kind = Destructibility::NotAvailable;
+  const CXXDestructorDecl *Destructor = nullptr;
+  std::optional<bool> NoThrow = std::nullopt;
+};
 
 DeductionFailureInfo
 MakeDeductionFailureInfo(ASTContext &Context, TemplateDeductionResult TDK,
diff --git a/clang/lib/Sema/SemaTypeTraits.cpp b/clang/lib/Sema/SemaTypeTraits.cpp
index ec17ac897fe01e..2054e52b113fcb 100644
--- a/clang/lib/Sema/SemaTypeTraits.cpp
+++ b/clang/lib/Sema/SemaTypeTraits.cpp
@@ -653,31 +653,35 @@ static ComparisonCategoryResult EvaluateTypeOrder(Sema &S, QualType LHS,
                     : ComparisonCategoryResult::Less;
 }
 
-std::pair<DestructorKind, const CXXDestructorDecl *>
-clang::GetDestructorKind(Sema &S, const ASTContext &C, SourceLocation Loc,
-                         QualType T) {
+DestructibleTraits::DestructibleTraits(Sema &S, const ASTContext &C,
+                                       SourceLocation Loc, QualType T)
+    : Loc(Loc), Kind(Destructibility::TriviallyDestructible) {
   // C++14 [meta.unary.prop]:
   //   For reference types, is_destructible<T>::value is true.
-  if (T->isReferenceType())
-    return {DestructorKind::Trivial, nullptr};
+  if (T->isReferenceType()) {
+    Kind = Destructibility::TriviallyDestructible;
+    return;
+  }
 
   // Objective-C++ ARC: autorelease types don't require destruction.
   if (T->isObjCLifetimeType() &&
-      T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing)
-    return {DestructorKind::Trivial, nullptr};
+      T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) {
+    Kind = Destructibility::TriviallyDestructible;
+    return;
+  }
 
   // C++14 [meta.unary.prop]:
   //   For incomplete types and function types, is_destructible<T>::value is
   //   false.
-  if (T->isIncompleteType() || T->isFunctionType())
-    return {DestructorKind::NotAvailable, nullptr};
-
-  DestructorKind Result = DestructorKind::Trivial;
+  if (T->isIncompleteType() || T->isFunctionType()) {
+    Kind = Destructibility::NotAvailable;
+    return;
+  }
 
   // A type that requires destruction (via a non-trivial destructor or ARC
   // lifetime semantics) is not trivially-destructible.
   if (T.isDestructedType())
-    Result = DestructorKind::NonTrivial;
+    Kind = Destructibility::Destructible;
 
   // C++14 [meta.unary.prop]:
   //   For object types and given U equal to remove_all_extents_t<T>, if the
@@ -685,32 +689,58 @@ clang::GetDestructorKind(Sema &S, const ASTContext &C, SourceLocation Loc,
   //   unevaluated operand (Clause 5), then is_destructible<T>::value is true
   auto *RD = C.getBaseElementType(T)->getAsCXXRecordDecl();
   if (!RD)
-    return {Result, nullptr};
+    return;
 
-  CXXDestructorDecl *Destructor = S.LookupDestructor(RD);
-  if (!Destructor)
-    return {DestructorKind::NotAvailable, Destructor};
+  Destructor = S.LookupDestructor(RD);
+  if (!Destructor) {
+    Kind = Destructibility::NotAvailable;
+    return;
+  }
 
   //  C++14 [dcl.fct.def.delete]p2:
   //    A program that refers to a deleted function implicitly or
   //    explicitly, other than to declare it, is ill-formed.
-  if (Destructor->isDeleted())
-    return {DestructorKind::NotAvailable, Destructor};
+  if (Destructor->isDeleted()) {
+    Kind = Destructibility::NotAvailable;
+    return;
+  }
+
+  if (C.getLangOpts().AccessControl && Destructor->getAccess() != AS_public) {
+    Kind = Destructibility::NotAvailable;
+    return;
+  }
+}
 
-  if (C.getLangOpts().AccessControl && Destructor->getAccess() != AS_public)
-    return {DestructorKind::NotAvailable, Destructor};
+DestructibleTraits::Destructibility DestructibleTraits::GetKind() const {
+  return Kind;
+}
 
-  return {Result, Destructor};
+bool DestructibleTraits::IsDestructible() const {
+  return Kind != Destructibility::NotAvailable;
 }
 
-bool clang::IsDestructorNoThrow(Sema &S, const ASTContext &C,
-                                SourceLocation Loc,
-                                const CXXDestructorDecl *Destructor) {
-  assert(Destructor != nullptr && "this function should only be called when "
-                                  "the type has a destructor declaration");
+bool DestructibleTraits::IsTriviallyDestructible() const {
+  return Kind == Destructibility::TriviallyDestructible;
+}
+
+bool DestructibleTraits::IsNoThrowDestructible(Sema &S, const ASTContext &C) {
+  if (!IsDestructible())
+    return false;
+
+  // We need to explicitly check `NoThrow` has a value because converting
+  // `NoThrow` to bool is ambiguous.
+  if (NoThrow.has_value())
+    return *NoThrow;
+
+  // A destructible type without a CXXDestructorDecl is always no-throw
+  // destructible.
+  if (!Destructor)
+    return true;
+
   auto *CPT = Destructor->getType()->castAs<FunctionProtoType>();
   CPT = S.ResolveExceptionSpec(Loc, CPT);
-  return CPT && CPT->isNothrow();
+  NoThrow = CPT && CPT->isNothrow();
+  return *NoThrow;
 }
 
 static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
@@ -937,22 +967,13 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
       return RD->hasTrivialCopyAssignment() &&
              !RD->hasNonTrivialCopyAssignment();
     return false;
-  case UTT_IsDestructible: {
-    auto [Kind, _] = clang::GetDestructorKind(Self, C, KeyLoc, T);
-    return Kind != DestructorKind::NotAvailable;
-  }
-  case UTT_IsTriviallyDestructible: {
-    auto [Kind, _] = clang::GetDestructorKind(Self, C, KeyLoc, T);
-    return Kind == DestructorKind::Trivial;
-  }
-  case UTT_IsNothrowDestructible: {
-    auto [Format, Destructor] = clang::GetDestructorKind(Self, C, KeyLoc, T);
-    if (Format == DestructorKind::NotAvailable) {
-      return false;
-    }
-    return Destructor == nullptr ||
-           clang::IsDestructorNoThrow(Self, C, KeyLoc, T, Destructor);
-  }
+  case UTT_IsDestructible:
+    return DestructibleTraits(Self, C, KeyLoc, T).IsDestructible();
+  case UTT_IsTriviallyDestructible:
+    return DestructibleTraits(Self, C, KeyLoc, T).IsTriviallyDestructible();
+  case UTT_IsNothrowDestructible:
+    return DestructibleTraits(Self, C, KeyLoc, T)
+        .IsNoThrowDestructible(Self, C);
 
   case UTT_HasTrivialDestructor:
     // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html

>From 2face65709de8be6dff4cece60d7f83165a19b2f Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb.ns at gmail.com>
Date: Tue, 24 Nov 2026 10:02:05 -0800
Subject: [PATCH 4/5] Apply batched suggestions from code review

Co-authored-by: Timm Baeder <tbaeder at redhat.com>
Co-authored-by: Christopher Di Bella <cjdb.ns at gmail.com>
---
 clang/include/clang/Sema/Sema.h   |  8 ++++----
 clang/lib/Sema/SemaTypeTraits.cpp | 12 ++++++------
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index b9d294287dc85c..68cc507f980bd8 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -16135,17 +16135,17 @@ class DestructibleTraits {
                      QualType T);
 
   /// Returns the raw destructor kind.
-  Destructibility GetKind() const;
+  Destructibility getKind() const;
 
   /// Returns true if, and only if, the type is destructible.
-  bool IsDestructible() const;
+  bool isDestructible() const;
 
   /// Returns true if, and only if, the type is trivially-destructible.
-  bool IsTriviallyDestructible() const;
+  bool isTriviallyDestructible() const;
 
   /// Returns true if, and only if, the type is destructible, and the chosen
   /// destructor is non-throwing.
-  bool IsNoThrowDestructible(Sema &S, const ASTContext &C);
+  bool isNoThrowDestructible(Sema &S, const ASTContext &C);
 
 private:
   SourceLocation Loc;
diff --git a/clang/lib/Sema/SemaTypeTraits.cpp b/clang/lib/Sema/SemaTypeTraits.cpp
index 2054e52b113fcb..3560104df4f2a5 100644
--- a/clang/lib/Sema/SemaTypeTraits.cpp
+++ b/clang/lib/Sema/SemaTypeTraits.cpp
@@ -715,15 +715,15 @@ DestructibleTraits::Destructibility DestructibleTraits::GetKind() const {
   return Kind;
 }
 
-bool DestructibleTraits::IsDestructible() const {
+bool DestructibleTraits::isDestructible() const {
   return Kind != Destructibility::NotAvailable;
 }
 
-bool DestructibleTraits::IsTriviallyDestructible() const {
+bool DestructibleTraits::isTriviallyDestructible() const {
   return Kind == Destructibility::TriviallyDestructible;
 }
 
-bool DestructibleTraits::IsNoThrowDestructible(Sema &S, const ASTContext &C) {
+bool DestructibleTraits::isNoThrowDestructible(Sema &S, const ASTContext &C) {
   if (!IsDestructible())
     return false;
 
@@ -968,12 +968,12 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
              !RD->hasNonTrivialCopyAssignment();
     return false;
   case UTT_IsDestructible:
-    return DestructibleTraits(Self, C, KeyLoc, T).IsDestructible();
+    return DestructibleTraits(Self, C, KeyLoc, T).isDestructible();
   case UTT_IsTriviallyDestructible:
-    return DestructibleTraits(Self, C, KeyLoc, T).IsTriviallyDestructible();
+    return DestructibleTraits(Self, C, KeyLoc, T).isTriviallyDestructible();
   case UTT_IsNothrowDestructible:
     return DestructibleTraits(Self, C, KeyLoc, T)
-        .IsNoThrowDestructible(Self, C);
+        .isNoThrowDestructible(Self, C);
 
   case UTT_HasTrivialDestructor:
     // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html

>From 1686c8d40ae0454c6871e1422b8a743d54e1a2ff Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Tue, 24 Nov 2026 11:53:17 -0800
Subject: [PATCH 5/5] Update SemaTypeTraits.cpp

---
 clang/lib/Sema/SemaTypeTraits.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaTypeTraits.cpp b/clang/lib/Sema/SemaTypeTraits.cpp
index 3560104df4f2a5..9fb963b71c71e4 100644
--- a/clang/lib/Sema/SemaTypeTraits.cpp
+++ b/clang/lib/Sema/SemaTypeTraits.cpp
@@ -711,7 +711,7 @@ DestructibleTraits::DestructibleTraits(Sema &S, const ASTContext &C,
   }
 }
 
-DestructibleTraits::Destructibility DestructibleTraits::GetKind() const {
+DestructibleTraits::Destructibility DestructibleTraits::getKind() const {
   return Kind;
 }
 



More information about the cfe-commits mailing list