Skip to content

Commit

Permalink
[clang] Allow class with anonymous union member to be const-default-c…
Browse files Browse the repository at this point in the history
…onstructible even if a union member has a default member initializer (llvm#95854)

Resolves llvm#95854

  Clang incorrectly considers a class with an anonymous union member to not be
  const-default-constructible even if a union member has a default member initializer.

```
struct A {
  union {
    int n = 0;
    int m;
  };
};
const A a;
```

-- As per https://eel.is/c++draft/dcl.init#general-8.3
  • Loading branch information
Rajveer100 committed Oct 1, 2024
1 parent 79ecb81 commit 1e1e446
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,9 @@ Bug Fixes to C++ Support
containing outer unexpanded parameters were not correctly expanded. (#GH101754)
- Fixed a bug in constraint expression comparison where the ``sizeof...`` expression was not handled properly
in certain friend declarations. (#GH93099)
- Clang incorrectly considered a class with an anonymous union member to not be
const-default-constructible even if a union member has a default member initializer.
(#GH95854).

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
8 changes: 7 additions & 1 deletion clang/lib/AST/DeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,9 @@ void CXXRecordDecl::addedMember(Decl *D) {
if (isUnion() && !Field->isAnonymousStructOrUnion())
data().HasVariantMembers = true;

if (isUnion() && IsFirstField)
data().HasUninitializedFields = true;

// C++0x [class]p9:
// A POD struct is a class that is both a trivial class and a
// standard-layout class, and has no non-static data members of type
Expand Down Expand Up @@ -1128,7 +1131,10 @@ void CXXRecordDecl::addedMember(Decl *D) {
data().DefaultedCopyConstructorIsDeleted = true;
}

if (!Field->hasInClassInitializer() && !Field->isMutable()) {
if (isUnion() && !Field->isMutable()) {
if (Field->hasInClassInitializer())
data().HasUninitializedFields = false;
} else if (!Field->hasInClassInitializer() && !Field->isMutable()) {
if (CXXRecordDecl *FieldType = T->getAsCXXRecordDecl()) {
if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
data().HasUninitializedFields = true;
Expand Down
51 changes: 51 additions & 0 deletions clang/test/SemaCXX/GH95854.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s

struct A {
union {
int n = 0;
int m;
};
};
const A a;

struct B {
union {
struct {
int n = 5;
int m;
};
};
};
const B b; // expected-error {{default initialization of an object of const type 'const B' without a user-provided default constructor}}

struct S {
int i;
int j;
};

struct T {
T() = default;
};

struct C {
union {
S s;
};
};

struct D {
union {
T s;
};
};

const C c; // expected-error {{default initialization of an object of const type 'const C' without a user-provided default constructor}}
const D d; // expected-error {{default initialization of an object of const type 'const D' without a user-provided default constructor}}

struct E {
union {
int n;
int m=0;
};
};
const E e;

0 comments on commit 1e1e446

Please sign in to comment.