[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 Aug 10 02:34:49 PDT 2024
https://github.com/Rajveer100 updated https://github.com/llvm/llvm-project/pull/96301
>From 1a25f021b797e5591f1ae324c8a8b5244047d5f4 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 | 88 +++++++++++++++++++++++++++++++
clang/include/clang/AST/DeclCXX.h | 3 +-
clang/lib/AST/DeclCXX.cpp | 9 +++-
clang/test/SemaCXX/GH95854.cpp | 21 ++++++++
4 files changed, 119 insertions(+), 2 deletions(-)
create mode 100644 clang/test/SemaCXX/GH95854.cpp
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 351b41b1c0c588..2927888b31f47c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -192,6 +192,94 @@ Bug Fixes in This Version
- Fixed a crash when diagnosing format strings and encountering an empty
delimited escape sequence (e.g., ``"\o{}"``). #GH102218
+- Fixed missing warnings when comparing mismatched enumeration constants
+ in C (#GH29217)
+
+- Clang now accepts elaborated-type-specifiers that explicitly specialize
+ a member class template for an implicit instantiation of a class template.
+
+- Fixed missing warnings when doing bool-like conversions in C23 (#GH79435).
+- Clang's ``-Wshadow`` no longer warns when an init-capture is named the same as
+ a class field unless the lambda can capture this.
+ Fixes (#GH71976)
+
+- Clang now accepts qualified partial/explicit specializations of variable templates that
+ are not nominable in the lookup context of the specialization.
+
+- Clang now doesn't produce false-positive warning `-Wconstant-logical-operand`
+ for logical operators in C23.
+ Fixes (#GH64356).
+
+- ``__is_trivially_relocatable`` no longer returns ``false`` for volatile-qualified types.
+ Fixes (#GH77091).
+
+- Clang no longer produces a false-positive `-Wunused-variable` warning
+ for variables created through copy initialization having side-effects in C++17 and later.
+ Fixes (#GH64356) (#GH79518).
+
+- Fix value of predefined macro ``__FUNCTION__`` in MSVC compatibility mode.
+ Fixes (#GH66114).
+
+- Clang now emits errors for explicit specializations/instatiations of lambda call
+ operator.
+ Fixes (#GH83267).
+
+- Fix crash on ill-formed partial specialization with CRTP.
+ Fixes (#GH89374).
+
+- Clang now correctly generates overloads for bit-precise integer types for
+ builtin operators in C++. Fixes #GH82998.
+
+- Fix crash when destructor definition is preceded with an equals sign.
+ Fixes (#GH89544).
+
+- When performing mixed arithmetic between ``_Complex`` floating-point types and integers,
+ Clang now correctly promotes the integer to its corresponding real floating-point
+ type only rather than to the complex type (e.g. ``_Complex float / int`` is now evaluated
+ as ``_Complex float / float`` rather than ``_Complex float / _Complex float``), as mandated
+ by the C standard. This significantly improves codegen of `*` and `/` especially.
+ Fixes #GH31205.
+
+- Fixes an assertion failure on invalid code when trying to define member
+ functions in lambdas.
+
+- Fixed a regression in CTAD that a friend declaration that befriends itself may cause
+ incorrect constraint substitution. (#GH86769).
+
+- Fixed an assertion failure on invalid InitListExpr in C89 mode (#GH88008).
+
+- Fixed missing destructor calls when we branch from middle of an expression.
+ This could happen through a branch in stmt-expr or in an expression containing a coroutine
+ suspension. Fixes (#GH63818) (#GH88478).
+
+- Clang will no longer diagnose an erroneous non-dependent ``switch`` condition
+ during instantiation, and instead will only diagnose it once, during checking
+ of the function template.
+
+- Clang now allows the value of unroll count to be zero in ``#pragma GCC unroll`` and ``#pragma unroll``.
+ The values of 0 and 1 block any unrolling of the loop. This keeps the same behavior with GCC.
+ Fixes (`#88624 <https://github.com/llvm/llvm-project/issues/88624>`_).
+
+- Clang will no longer emit a duplicate -Wunused-value warning for an expression
+ `(A, B)` which evaluates to glvalue `B` that can be converted to non ODR-use. (#GH45783)
+
+- Clang now correctly disallows VLA type compound literals, e.g. ``(int[size]){}``,
+ as the C standard mandates. (#GH89835)
+
+- ``__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).
+
+- Correctly reject declarations where a statement is required in C.
+ Fixes #GH92775
+
+- Fixed `static_cast` to array of unknown bound. Fixes (#GH62863).
+
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h
index bf6a5ce92d438d..9988a4c84ad008 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1395,7 +1395,8 @@ class CXXRecordDecl : public RecordDecl {
bool allowConstDefaultInit() const {
return !data().HasUninitializedFields ||
!(data().HasDefaultedDefaultConstructor ||
- needsImplicitDefaultConstructor());
+ needsImplicitDefaultConstructor()) ||
+ (isUnion() && isEmpty());
}
/// Determine whether this class has a destructor which has no
diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp
index 9a3ede426e9143..e8597adcf6fd4a 100644
--- a/clang/lib/AST/DeclCXX.cpp
+++ b/clang/lib/AST/DeclCXX.cpp
@@ -1057,6 +1057,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
@@ -1125,7 +1128,11 @@ 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;
diff --git a/clang/test/SemaCXX/GH95854.cpp b/clang/test/SemaCXX/GH95854.cpp
new file mode 100644
index 00000000000000..62ae549f2496f0
--- /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; // expected-error {{default initialization of an object of const type 'const B' without a user-provided default constructor}}
More information about the cfe-commits
mailing list