[clang] [Sema] Instantiate destructors for initialized anonymous union fields (PR #128866)

Maurice Heumann via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 28 00:17:37 PST 2025


================
@@ -5451,10 +5451,23 @@ bool Sema::SetCtorInitializers(CXXConstructorDecl *Constructor, bool AnyErrors,
            NumInitializers * sizeof(CXXCtorInitializer*));
     Constructor->setCtorInitializers(baseOrMemberInitializers);
 
+    SourceLocation Location = Constructor->getLocation();
+
+    for (CXXCtorInitializer *Initializer : Info.AllToInit) {
+      FieldDecl *Field = Initializer->getAnyMember();
+      if (!Field)
+        continue;
+
+      RecordDecl *FieldRecordDecl = Field->getParent();
+      if (!FieldRecordDecl->isUnion() ||
----------------
momo5502 wrote:

Both examples should work now. Let's hope all other tests still pass :D

The first example throws an additional error in gcc:

```
member 'VSX<int> VS::<unnamed union>::<unnamed struct>::_Tail' with destructor not allowed in anonymous aggregate
```

this error is not thrown by clang. should it though? If so, I don't think implementing that makes sense within the scope of this PR.
However, is it ok that I still added the sample as a test case, even though technically the behaviour clang yields for this test is not 100% correct?

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


More information about the cfe-commits mailing list