Skip to content

Commit b964923

Browse files
committed
[clang] Allow class with anonymous union member to be const-default-constructible even if a union member has a default member initializer (#95854)
Resolves #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. This is valid as per ``8.3`` in `Draft C++ Standard <https://eel.is/c++draft/dcl.init#general-8.3>`_. The ``allowConstDefaultInit`` member function in ``CXXRecordDecl`` needs special-case unions to handle the rule. (#GH95854). ``` struct A { union { int n = 0; int m; }; }; const A a; ``` -- As per https://eel.is/c++draft/dcl.init#general-8.3
1 parent 1430405 commit b964923

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,12 @@ Bug Fixes in This Version
624624

625625
- ``__is_array`` and ``__is_bounded_array`` no longer return ``true`` for
626626
zero-sized arrays. Fixes (#GH54705).
627+
628+
- Clang incorrectly considers a class with an anonymous union member to not be
629+
const-default-constructible even if a union member has a default member initializer.
630+
This is valid as per ``8.3`` in `Draft C++ Standard <https://eel.is/c++draft/dcl.init#general-8.3>`_.
631+
The ``allowConstDefaultInit`` member function in ``CXXRecordDecl`` needs
632+
special-case unions to handle the rule. (#GH95854).
627633

628634
Bug Fixes to Compiler Builtins
629635
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/AST/DeclCXX.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,9 @@ class CXXRecordDecl : public RecordDecl {
13921392
bool allowConstDefaultInit() const {
13931393
return !data().HasUninitializedFields ||
13941394
!(data().HasDefaultedDefaultConstructor ||
1395-
needsImplicitDefaultConstructor());
1395+
needsImplicitDefaultConstructor()) ||
1396+
(!hasUninitializedReferenceMember() && isUnion() &&
1397+
hasInClassInitializer());
13961398
}
13971399

13981400
/// Determine whether this class has a destructor which has no

clang/test/SemaCXX/GH95854.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
2+
//
3+
// expected-no-diagnostics
4+
5+
struct A {
6+
union {
7+
int n = 0;
8+
int m;
9+
};
10+
};
11+
const A a;
12+
13+
struct B {
14+
union {
15+
struct {
16+
int n = 5;
17+
int m;
18+
};
19+
};
20+
};
21+
const B b;

0 commit comments

Comments
 (0)