[clang] [Clang] Partial implementation of support for P3074 (trivial unions) (PR #146815)
Bruno Cardoso Lopes via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 9 08:00:54 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) {
+ CXXConstructorDecl *Ctor =
+ dyn_cast<CXXConstructorDecl>(SMOR.getMethod());
+ if (Ctor->isTrivial()) {
+ return false;
+ }
+
+ if (!Ctor->isUserProvided() && !Field->hasInClassInitializer()) {
----------------
bcardosolopes wrote:
and here
https://github.com/llvm/llvm-project/pull/146815
More information about the cfe-commits
mailing list