[clang] [Clang] Partial implementation of support for P3074 (trivial unions) (PR #146815)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 11 08:18:23 PDT 2025


================
@@ -9543,6 +9543,48 @@ bool SpecialMemberDeletionInfo::shouldDeleteForSubobjectCall(
   if (DiagKind == -1)
     return false;
 
+  if (this->S.Context.getLangOpts().CPlusPlus26 && inUnion() &&
+      CSM == CXXSpecialMemberKind::Destructor) {
+    // [class.dtor]/7 In C++26, a destructor for a union X is only deleted under
+    // the additional conditions that:
+
+    // overload resolution to select a constructor to default-initialize an
+    // object of type X either fails or selects a constructor that is either
+    // deleted or not trivial, or
+    // or X has a variant member V of class type M (or possibly
+    // multi-dimensional array thereof) where V has a default member initializer
+    // and M has a destructor that is non-trivial,
+
+    RecordDecl *Parent = Field->getParent();
+    while (Parent &&
+           (Parent->isAnonymousStructOrUnion() ||
+            (Parent->isUnion() && Parent->getIdentifier() == nullptr))) {
+      if (auto RD = dyn_cast_or_null<RecordDecl>(Parent->getParent())) {
+        Parent = RD;
+      } else {
+        break;
+      }
+    }
+
+    auto ParentDecl = dyn_cast<CXXRecordDecl>(Parent);
+    if (!ParentDecl->isBeingDefined()) {
+      Sema::SpecialMemberOverloadResult SMOR = S.LookupSpecialMember(
+          ParentDecl, CXXSpecialMemberKind::DefaultConstructor, false, false,
+          false, false, false);
+      if (SMOR.getKind() == Sema::SpecialMemberOverloadResult::Success) {
----------------
Sirraide wrote:

At least when it comes to getting the parent type, we do something similar in `GetEnclosingNamedOrTopAnonRecord()` in `SemaBoundsSafety.cpp` so I don’t think there is a helper for that.

`CXXRecordDecl` has `hasTrivialDefaultConstructor()` and friends; you might be able to make use of those here rather than performing a lookup manually (at least the check for `!isBeingDefined()` would imply to me that the flags that those functions query are accurate), but it’s possible I’m missing something here.

https://github.com/llvm/llvm-project/pull/146815


More information about the cfe-commits mailing list