[clang] [Clang] Partial implementation of support for P3074 (trivial unions) (PR #146815)
Barry Revzin via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 7 06:34:39 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) {
----------------
brevzin wrote:
I'm guessing this is the _wrong_ way to do this, but I don't know how to do it better.
Also this doesn't exactly implement the wording I quoted above because the wording is just wrong (see future CWG issue [here](https://github.com/cplusplus/cwg/issues/722)).
https://github.com/llvm/llvm-project/pull/146815
More information about the cfe-commits
mailing list