[clang] [clang] Allow class with anonymous union member to be const-default-constructible even if a union member has a default member initializer (#95854) (PR #96301)
Rajveer Singh Bharadwaj via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 22 12:06:27 PDT 2024
https://github.com/Rajveer100 updated https://github.com/llvm/llvm-project/pull/96301
>From b964923b9610c9cd53e4d1de8f5d51d8fcebc78c Mon Sep 17 00:00:00 2001
From: Rajveer <rajveer.developer at icloud.com>
Date: Fri, 21 Jun 2024 18:26:36 +0530
Subject: [PATCH] [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
---
clang/docs/ReleaseNotes.rst | 6 ++++++
clang/include/clang/AST/DeclCXX.h | 4 +++-
clang/test/SemaCXX/GH95854.cpp | 21 +++++++++++++++++++++
3 files changed, 30 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaCXX/GH95854.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 45676a02b760b..c107304ea95f7 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -624,6 +624,12 @@ Bug Fixes in This Version
- ``__is_array`` and ``__is_bounded_array`` no longer return ``true`` for
zero-sized arrays. Fixes (#GH54705).
+
+- 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).
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h
index fb52ac804849d..e4dc50c9b9e00 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1392,7 +1392,9 @@ class CXXRecordDecl : public RecordDecl {
bool allowConstDefaultInit() const {
return !data().HasUninitializedFields ||
!(data().HasDefaultedDefaultConstructor ||
- needsImplicitDefaultConstructor());
+ needsImplicitDefaultConstructor()) ||
+ (!hasUninitializedReferenceMember() && isUnion() &&
+ hasInClassInitializer());
}
/// Determine whether this class has a destructor which has no
diff --git a/clang/test/SemaCXX/GH95854.cpp b/clang/test/SemaCXX/GH95854.cpp
new file mode 100644
index 0000000000000..820cf15e68287
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+//
+// expected-no-diagnostics
+
+struct A {
+ union {
+ int n = 0;
+ int m;
+ };
+};
+const A a;
+
+struct B {
+ union {
+ struct {
+ int n = 5;
+ int m;
+ };
+ };
+};
+const B b;
More information about the cfe-commits
mailing list