[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)

2024-10-03 Thread via cfe-commits

https://github.com/cor3ntin approved this pull request.

LGTM, Thanks!
Give a couple of days to @shafik before merging

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-09-25 Thread Rajveer Singh Bharadwaj via cfe-commits


@@ -0,0 +1,43 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+
+struct A {

Rajveer100 wrote:

Added.

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-09-25 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From f42cea250e79efc6ae51139d51ac72adc4fe7bfd Mon Sep 17 00:00:00 2001
From: Rajveer 
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.

```
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|  3 ++
 clang/lib/AST/DeclCXX.cpp  |  8 +-
 clang/test/SemaCXX/GH95854.cpp | 51 ++
 3 files changed, 61 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaCXX/GH95854.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 69b2aea52aa9d3..0b7d927f9e7ea3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -391,6 +391,9 @@ Bug Fixes to C++ Support
 - Fixed a crash when clang tries to subtitute parameter pack while retaining 
the parameter
   pack. #GH63819, #GH107560
 - Fix a crash when a static assert declaration has an invalid close location. 
(#GH108687)
+- 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
 ^
diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp
index 01143391edab40..9178e1263be530 100644
--- a/clang/lib/AST/DeclCXX.cpp
+++ b/clang/lib/AST/DeclCXX.cpp
@@ -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
@@ -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;
diff --git a/clang/test/SemaCXX/GH95854.cpp b/clang/test/SemaCXX/GH95854.cpp
new file mode 100644
index 00..aa470c6ac8e5b4
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -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;

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-09-23 Thread Shafik Yaghmour via cfe-commits


@@ -0,0 +1,43 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+
+struct A {

shafik wrote:

We should also test this case: 

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

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-27 Thread via cfe-commits

cor3ntin wrote:

Neither GCC, EDG nor MSVC agree with us https://godbolt.org/z/Wqrhnz63a
The behavior of GCC is a bit weird but I am pretty sure the last two tests 
should pass
@hubert-reinterpretcast 

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-27 Thread Rajveer Singh Bharadwaj via cfe-commits

Rajveer100 wrote:

@cor3ntin 
CI looks good.

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-27 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From dfc44453e9168048d60071ba6e28e7b01be22114 Mon Sep 17 00:00:00 2001
From: Rajveer 
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.

```
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|  3 +++
 clang/lib/AST/DeclCXX.cpp  |  8 ++-
 clang/test/SemaCXX/GH95854.cpp | 43 ++
 3 files changed, 53 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaCXX/GH95854.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2c29d49ba20f03..63f3ebd9c2984c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -318,6 +318,9 @@ Bug Fixes to C++ Support
   of the current instantiation in all cases.
 - Fix evaluation of the index of dependent pack indexing expressions/types 
specifiers (#GH105900)
 - Correctly handle subexpressions of an immediate invocation in the presence 
of implicit casts. (#GH105558)
+- 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
 ^
diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp
index 9a3ede426e9143..07ce398292d8ce 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,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;
diff --git a/clang/test/SemaCXX/GH95854.cpp b/clang/test/SemaCXX/GH95854.cpp
new file mode 100644
index 00..bd6f503ffdd2b3
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -0,0 +1,43 @@
+// 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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-27 Thread via cfe-commits


@@ -318,6 +318,9 @@ Bug Fixes to C++ Support
   of the current instantiation in all cases.
 - Fix evaluation of the index of dependent pack indexing expressions/types 
specifiers (#GH105900)
 - Correctly handle subexpressions of an immediate invocation in the presence 
of implicit casts. (#GH105558)
+- Clang incorrectly considers a class with an anonymous union member to not be

cor3ntin wrote:

```suggestion
- Clang incorrectly considered a class with an anonymous union member to not be
```

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-27 Thread via cfe-commits


@@ -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;
+  }

cor3ntin wrote:

```suggestion
  if (Field->hasInClassInitializer())
data().HasUninitializedFields = false;
```

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-27 Thread via cfe-commits

https://github.com/cor3ntin edited 
https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-27 Thread via cfe-commits

https://github.com/cor3ntin commented:

Here are more tests to consider https://godbolt.org/z/M6fj5zTfq 
Thanks!

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-27 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From 387e83880994ddd71898680e421bc4590a3dbd63 Mon Sep 17 00:00:00 2001
From: Rajveer 
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.

```
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|  3 +++
 clang/lib/AST/DeclCXX.cpp  |  9 -
 clang/test/SemaCXX/GH95854.cpp | 19 +++
 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 2c29d49ba20f03..0e6036262fd1e7 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -318,6 +318,9 @@ Bug Fixes to C++ Support
   of the current instantiation in all cases.
 - Fix evaluation of the index of dependent pack indexing expressions/types 
specifiers (#GH105900)
 - Correctly handle subexpressions of an immediate invocation in the presence 
of implicit casts. (#GH105558)
+- 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.
+  (#GH95854).
 
 Bug Fixes to AST Handling
 ^
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 00..68fcf1397dd901
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -0,0 +1,19 @@
+// 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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-27 Thread Rajveer Singh Bharadwaj via cfe-commits

Rajveer100 wrote:

Only windows failed.

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-26 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From 8796f60f0255ab8810d6019b4c470d64a44a6ce2 Mon Sep 17 00:00:00 2001
From: Rajveer 
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.

```
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|  3 +++
 clang/lib/AST/DeclCXX.cpp  |  9 -
 clang/test/SemaCXX/GH95854.cpp | 19 +++
 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 39e1b0fcb09bbd..6994a74a35c43c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -219,6 +219,9 @@ Bug Fixes to C++ Support
 - Fixed a bug when diagnosing ambiguous explicit specializations of 
constrained member functions.
 - Fixed an assertion failure when selecting a function from an overload set 
that includes a 
   specialization of a conversion function template.
+- 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.
+  (#GH95854).
 
 Bug Fixes to AST Handling
 ^
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 00..68fcf1397dd901
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -0,0 +1,19 @@
+// 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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-26 Thread Shafik Yaghmour via cfe-commits

https://github.com/shafik commented:

I don't think the test failures look related to the changes can you push and 
empty change to see if rerunning the tests comes up clean?

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-13 Thread Rajveer Singh Bharadwaj via cfe-commits

Rajveer100 wrote:

@Sirraide The tests pass, not sure about the failure though.

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-13 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From 3ccb1c969621894a9a6dabb2f9adfc213d50e886 Mon Sep 17 00:00:00 2001
From: Rajveer 
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.

```
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|  3 +++
 clang/lib/AST/DeclCXX.cpp  |  9 -
 clang/test/SemaCXX/GH95854.cpp | 19 +++
 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 39e1b0fcb09bbd..6994a74a35c43c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -219,6 +219,9 @@ Bug Fixes to C++ Support
 - Fixed a bug when diagnosing ambiguous explicit specializations of 
constrained member functions.
 - Fixed an assertion failure when selecting a function from an overload set 
that includes a 
   specialization of a conversion function template.
+- 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.
+  (#GH95854).
 
 Bug Fixes to AST Handling
 ^
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 00..68fcf1397dd901
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -0,0 +1,19 @@
+// 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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-13 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From 49e6fb5481ab0589f653345f1c3cdbe161aefb34 Mon Sep 17 00:00:00 2001
From: Rajveer 
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.

```
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   |  3 +++
 clang/include/clang/AST/DeclCXX.h |  2 +-
 clang/lib/AST/DeclCXX.cpp |  9 -
 clang/test/SemaCXX/GH95854.cpp| 19 +++
 4 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/SemaCXX/GH95854.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 39e1b0fcb09bbd..6994a74a35c43c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -219,6 +219,9 @@ Bug Fixes to C++ Support
 - Fixed a bug when diagnosing ambiguous explicit specializations of 
constrained member functions.
 - Fixed an assertion failure when selecting a function from an overload set 
that includes a 
   specialization of a conversion function template.
+- 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.
+  (#GH95854).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/AST/DeclCXX.h 
b/clang/include/clang/AST/DeclCXX.h
index 0d72cc6a08dcb4..bc3cbd6da1bc37 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1162,7 +1162,7 @@ class CXXRecordDecl : public RecordDecl {
   ///value-initialization of an entity of reference type is ill-formed.
   bool hasUninitializedReferenceMember() const {
 return !isUnion() && !hasUserDeclaredConstructor() &&
-   data().HasUninitializedReferenceMember;
+data().HasUninitializedReferenceMember;
   }
 
   /// Whether this class is a POD-type (C++ [class]p4)
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 00..68fcf1397dd901
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -0,0 +1,19 @@
+// 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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-13 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From 975a91bd8cb8c9e04f1dcfc455f83f7115d1cf71 Mon Sep 17 00:00:00 2001
From: Rajveer 
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.

```
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   |  3 +++
 clang/include/clang/AST/DeclCXX.h |  4 ++--
 clang/lib/AST/DeclCXX.cpp |  9 -
 clang/test/SemaCXX/GH95854.cpp| 19 +++
 4 files changed, 32 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/SemaCXX/GH95854.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 39e1b0fcb09bbd..6994a74a35c43c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -219,6 +219,9 @@ Bug Fixes to C++ Support
 - Fixed a bug when diagnosing ambiguous explicit specializations of 
constrained member functions.
 - Fixed an assertion failure when selecting a function from an overload set 
that includes a 
   specialization of a conversion function template.
+- 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.
+  (#GH95854).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/AST/DeclCXX.h 
b/clang/include/clang/AST/DeclCXX.h
index 0d72cc6a08dcb4..846c564e912675 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1161,8 +1161,8 @@ class CXXRecordDecl : public RecordDecl {
   ///value-initialized [...] A program that calls for [...]
   ///value-initialization of an entity of reference type is ill-formed.
   bool hasUninitializedReferenceMember() const {
-return !isUnion() && !hasUserDeclaredConstructor() &&
-   data().HasUninitializedReferenceMember;
+return (!isUnion() && !hasUserDeclaredConstructor() &&
+data().HasUninitializedReferenceMember);
   }
 
   /// Whether this class is a POD-type (C++ [class]p4)
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 00..68fcf1397dd901
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -0,0 +1,19 @@
+// 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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-13 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From 13648d1c897f73b04ada715fe7d1f9338c2e3591 Mon Sep 17 00:00:00 2001
From: Rajveer 
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.

```
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   |  3 +++
 clang/include/clang/AST/DeclCXX.h |  8 +---
 clang/lib/AST/DeclCXX.cpp |  9 -
 clang/test/SemaCXX/GH95854.cpp| 19 +++
 4 files changed, 35 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/SemaCXX/GH95854.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 39e1b0fcb09bbd..6994a74a35c43c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -219,6 +219,9 @@ Bug Fixes to C++ Support
 - Fixed a bug when diagnosing ambiguous explicit specializations of 
constrained member functions.
 - Fixed an assertion failure when selecting a function from an overload set 
that includes a 
   specialization of a conversion function template.
+- 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.
+  (#GH95854).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/AST/DeclCXX.h 
b/clang/include/clang/AST/DeclCXX.h
index 0d72cc6a08dcb4..81105e32d40240 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1161,8 +1161,9 @@ class CXXRecordDecl : public RecordDecl {
   ///value-initialized [...] A program that calls for [...]
   ///value-initialization of an entity of reference type is ill-formed.
   bool hasUninitializedReferenceMember() const {
-return !isUnion() && !hasUserDeclaredConstructor() &&
-   data().HasUninitializedReferenceMember;
+return (!isUnion() && !hasUserDeclaredConstructor() &&
+data().HasUninitializedReferenceMember) ||
+   needsImplicitDefaultConstructor();
   }
 
   /// Whether this class is a POD-type (C++ [class]p4)
@@ -1396,7 +1397,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 00..68fcf1397dd901
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -0,0 +1,19 @@
+// 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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-13 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From 950586bd017175a1039952f7cf090c85d7bfc6e0 Mon Sep 17 00:00:00 2001
From: Rajveer 
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.

```
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   |  3 +++
 clang/include/clang/AST/DeclCXX.h |  5 +++--
 clang/lib/AST/DeclCXX.cpp |  9 -
 clang/test/SemaCXX/GH95854.cpp| 19 +++
 4 files changed, 33 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/SemaCXX/GH95854.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 39e1b0fcb09bbd..6994a74a35c43c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -219,6 +219,9 @@ Bug Fixes to C++ Support
 - Fixed a bug when diagnosing ambiguous explicit specializations of 
constrained member functions.
 - Fixed an assertion failure when selecting a function from an overload set 
that includes a 
   specialization of a conversion function template.
+- 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.
+  (#GH95854).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/AST/DeclCXX.h 
b/clang/include/clang/AST/DeclCXX.h
index 0d72cc6a08dcb4..67339d55071526 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1161,8 +1161,9 @@ class CXXRecordDecl : public RecordDecl {
   ///value-initialized [...] A program that calls for [...]
   ///value-initialization of an entity of reference type is ill-formed.
   bool hasUninitializedReferenceMember() const {
-return !isUnion() && !hasUserDeclaredConstructor() &&
-   data().HasUninitializedReferenceMember;
+return (!isUnion() && !hasUserDeclaredConstructor() &&
+data().HasUninitializedReferenceMember) ||
+   needsImplicitDefaultConstructor();
   }
 
   /// Whether this class is a POD-type (C++ [class]p4)
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 00..68fcf1397dd901
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -0,0 +1,19 @@
+// 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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-13 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From b46a96d5fb7abe08877fcf6b16996f4105c6559e Mon Sep 17 00:00:00 2001
From: Rajveer 
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.

```
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   |  3 +++
 clang/include/clang/AST/DeclCXX.h |  5 +++--
 clang/lib/AST/DeclCXX.cpp |  9 -
 clang/test/SemaCXX/GH95854.cpp| 19 +++
 4 files changed, 33 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/SemaCXX/GH95854.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 39e1b0fcb09bbd..6994a74a35c43c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -219,6 +219,9 @@ Bug Fixes to C++ Support
 - Fixed a bug when diagnosing ambiguous explicit specializations of 
constrained member functions.
 - Fixed an assertion failure when selecting a function from an overload set 
that includes a 
   specialization of a conversion function template.
+- 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.
+  (#GH95854).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/AST/DeclCXX.h 
b/clang/include/clang/AST/DeclCXX.h
index 0d72cc6a08dcb4..29e082020baa2a 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1161,8 +1161,9 @@ class CXXRecordDecl : public RecordDecl {
   ///value-initialized [...] A program that calls for [...]
   ///value-initialization of an entity of reference type is ill-formed.
   bool hasUninitializedReferenceMember() const {
-return !isUnion() && !hasUserDeclaredConstructor() &&
-   data().HasUninitializedReferenceMember;
+return (!isUnion() && !hasUserDeclaredConstructor() &&
+   data().HasUninitializedReferenceMember) ||
+   needsImplicitDefaultConstructor();
   }
 
   /// Whether this class is a POD-type (C++ [class]p4)
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 00..68fcf1397dd901
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -0,0 +1,19 @@
+// 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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-13 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From f31e4ec4f7374723afb0dc91409b95a8d2855573 Mon Sep 17 00:00:00 2001
From: Rajveer 
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.

```
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   |  3 +++
 clang/include/clang/AST/DeclCXX.h |  3 ++-
 clang/lib/AST/DeclCXX.cpp |  9 -
 clang/test/SemaCXX/GH95854.cpp| 19 +++
 4 files changed, 32 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/SemaCXX/GH95854.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 39e1b0fcb09bbd..6994a74a35c43c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -219,6 +219,9 @@ Bug Fixes to C++ Support
 - Fixed a bug when diagnosing ambiguous explicit specializations of 
constrained member functions.
 - Fixed an assertion failure when selecting a function from an overload set 
that includes a 
   specialization of a conversion function template.
+- 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.
+  (#GH95854).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/AST/DeclCXX.h 
b/clang/include/clang/AST/DeclCXX.h
index 0d72cc6a08dcb4..c943b5a15a03e5 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1162,7 +1162,8 @@ class CXXRecordDecl : public RecordDecl {
   ///value-initialization of an entity of reference type is ill-formed.
   bool hasUninitializedReferenceMember() const {
 return !isUnion() && !hasUserDeclaredConstructor() &&
-   data().HasUninitializedReferenceMember;
+   data().HasUninitializedReferenceMember ||
+   needsImplicitDefaultConstructor();
   }
 
   /// Whether this class is a POD-type (C++ [class]p4)
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 00..68fcf1397dd901
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -0,0 +1,19 @@
+// 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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-13 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From 326e0f5bdb8d543ddd42345e6a9ffb8a7fb2b823 Mon Sep 17 00:00:00 2001
From: Rajveer 
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.

```
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   |  5 -
 clang/include/clang/AST/DeclCXX.h |  3 ++-
 clang/lib/AST/DeclCXX.cpp |  9 -
 clang/test/SemaCXX/GH95854.cpp| 19 +++
 4 files changed, 33 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/SemaCXX/GH95854.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 39e1b0fcb09bbd..d95577b0649059 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -217,8 +217,11 @@ Bug Fixes to C++ Support
 - Clang now preserves the unexpanded flag in a lambda transform used for pack 
expansion. (#GH56852), (#GH85667),
   (#GH99877).
 - Fixed a bug when diagnosing ambiguous explicit specializations of 
constrained member functions.
-- Fixed an assertion failure when selecting a function from an overload set 
that includes a 
+- Fixed an assertion failure when selecting a function from an overload set 
that includes a
   specialization of a conversion function template.
+- 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.
+  (#GH95854).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/AST/DeclCXX.h 
b/clang/include/clang/AST/DeclCXX.h
index 0d72cc6a08dcb4..c943b5a15a03e5 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1162,7 +1162,8 @@ class CXXRecordDecl : public RecordDecl {
   ///value-initialization of an entity of reference type is ill-formed.
   bool hasUninitializedReferenceMember() const {
 return !isUnion() && !hasUserDeclaredConstructor() &&
-   data().HasUninitializedReferenceMember;
+   data().HasUninitializedReferenceMember ||
+   needsImplicitDefaultConstructor();
   }
 
   /// Whether this class is a POD-type (C++ [class]p4)
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 00..68fcf1397dd901
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -0,0 +1,19 @@
+// 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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-13 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From 7e0b2c0a08c63a01309991980d30c64bcea325fe Mon Sep 17 00:00:00 2001
From: Rajveer 
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.

```
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   |  5 -
 clang/include/clang/AST/DeclCXX.h |  2 +-
 clang/lib/AST/DeclCXX.cpp |  9 -
 clang/test/SemaCXX/GH95854.cpp| 19 +++
 4 files changed, 32 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/SemaCXX/GH95854.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 39e1b0fcb09bbd..d95577b0649059 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -217,8 +217,11 @@ Bug Fixes to C++ Support
 - Clang now preserves the unexpanded flag in a lambda transform used for pack 
expansion. (#GH56852), (#GH85667),
   (#GH99877).
 - Fixed a bug when diagnosing ambiguous explicit specializations of 
constrained member functions.
-- Fixed an assertion failure when selecting a function from an overload set 
that includes a 
+- Fixed an assertion failure when selecting a function from an overload set 
that includes a
   specialization of a conversion function template.
+- 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.
+  (#GH95854).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/AST/DeclCXX.h 
b/clang/include/clang/AST/DeclCXX.h
index 0d72cc6a08dcb4..7157caa8e2ab2d 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1162,7 +1162,7 @@ class CXXRecordDecl : public RecordDecl {
   ///value-initialization of an entity of reference type is ill-formed.
   bool hasUninitializedReferenceMember() const {
 return !isUnion() && !hasUserDeclaredConstructor() &&
-   data().HasUninitializedReferenceMember;
+   data().HasUninitializedReferenceMember || 
needsImplicitDefaultConstructor();
   }
 
   /// Whether this class is a POD-type (C++ [class]p4)
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 00..68fcf1397dd901
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -0,0 +1,19 @@
+// 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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-13 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From 03c02eaafdccb3f049e6909af7255fcf7d4d1784 Mon Sep 17 00:00:00 2001
From: Rajveer 
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.

```
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|  4 
 clang/lib/AST/DeclCXX.cpp  |  9 -
 clang/test/SemaCXX/GH95854.cpp | 19 +++
 3 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaCXX/GH95854.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 351b41b1c0c588..b7296f19449bf1 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -217,6 +217,10 @@ Bug Fixes to C++ Support
 - Clang now preserves the unexpanded flag in a lambda transform used for pack 
expansion. (#GH56852), (#GH85667),
   (#GH99877).
 - Fixed a bug when diagnosing ambiguous explicit specializations of 
constrained member functions.
+- 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 
`_.
+  (#GH95854).
 
 Bug Fixes to AST Handling
 ^
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 00..68fcf1397dd901
--- /dev/null
+++ b/clang/test/SemaCXX/GH95854.cpp
@@ -0,0 +1,19 @@
+// 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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-12 Thread via cfe-commits

https://github.com/Sirraide edited 
https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-12 Thread via cfe-commits


@@ -1395,7 +1395,8 @@ class CXXRecordDecl : public RecordDecl {
   bool allowConstDefaultInit() const {
 return !data().HasUninitializedFields ||
!(data().HasDefaultedDefaultConstructor ||
- needsImplicitDefaultConstructor());
+ needsImplicitDefaultConstructor()) ||
+   (isUnion() && isEmpty());

Sirraide wrote:

Er, why do we special-case empty unions here? Shouldn’t that just work 
automatically because an empty union has no fields at all and therefore no 
uninitialised fields either?

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-12 Thread via cfe-commits


@@ -217,6 +217,11 @@ Bug Fixes to C++ Support
 - Clang now preserves the unexpanded flag in a lambda transform used for pack 
expansion. (#GH56852), (#GH85667),
   (#GH99877).
 - Fixed a bug when diagnosing ambiguous explicit specializations of 
constrained member functions.
+- 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 
`_.
+  The ``allowConstDefaultInit`` member function in ``CXXRecordDecl`` needs
+  special-case unions to handle the rule. (#GH95854).

Sirraide wrote:

```suggestion
  (#GH95854).
```
I don’t think any of that needs to be here.

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-12 Thread via cfe-commits

https://github.com/Sirraide commented:

I think the changes generally make sense.

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-12 Thread Jessica Clarke via cfe-commits


@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+//
+// -expected-no-diagnostics

jrtc27 wrote:

?

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-10 Thread Rajveer Singh Bharadwaj via cfe-commits

Rajveer100 wrote:

@zygoloid 
Could you review this?

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-10 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From 7efe2bd186cb0a97b8f8b66b9c69da92c79fc60a Mon Sep 17 00:00:00 2001
From: Rajveer 
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 
`_.
  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   |  5 +
 clang/include/clang/AST/DeclCXX.h |  3 ++-
 clang/lib/AST/DeclCXX.cpp |  9 -
 clang/test/SemaCXX/GH95854.cpp| 21 +
 4 files changed, 36 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..890b9054b84401 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -217,6 +217,11 @@ Bug Fixes to C++ Support
 - Clang now preserves the unexpanded flag in a lambda transform used for pack 
expansion. (#GH56852), (#GH85667),
   (#GH99877).
 - Fixed a bug when diagnosing ambiguous explicit specializations of 
constrained member functions.
+- 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 
`_.
+  The ``allowConstDefaultInit`` member function in ``CXXRecordDecl`` needs
+  special-case unions to handle the rule. (#GH95854).
 
 Bug Fixes to AST Handling
 ^
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 00..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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-08-10 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From 1a25f021b797e5591f1ae324c8a8b5244047d5f4 Mon Sep 17 00:00:00 2001
From: Rajveer 
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 
`_.
  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 `_).
+
+- 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]){}``,
+  

[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)

2024-07-05 Thread Rajveer Singh Bharadwaj via cfe-commits

Rajveer100 wrote:

@zygoloid 
Let me know if the new changes work well.

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-07-05 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From 751e630dbf54ef6f3a1209a5d09f99093ad84cae Mon Sep 17 00:00:00 2001
From: Rajveer 
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 
`_.
  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 |  3 ++-
 clang/lib/AST/DeclCXX.cpp |  9 -
 clang/test/SemaCXX/GH95854.cpp| 21 +
 4 files changed, 37 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/SemaCXX/GH95854.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 36cf615a4287cc..35259760adc60f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -756,6 +756,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 
`_.
+  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
diff --git a/clang/include/clang/AST/DeclCXX.h 
b/clang/include/clang/AST/DeclCXX.h
index fb52ac804849d8..f4d90e19e0e05e 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1392,7 +1392,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 d5c140fd343895..16f0a2b4592ed0 100644
--- a/clang/lib/AST/DeclCXX.cpp
+++ b/clang/lib/AST/DeclCXX.cpp
@@ -1018,6 +1018,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
@@ -1086,7 +1089,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 00..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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-07-05 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From 3fb29e52b7227c2778942b3ca941112596ce89c1 Mon Sep 17 00:00:00 2001
From: Rajveer 
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 
`_.
  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 |  3 ++-
 clang/lib/AST/DeclCXX.cpp |  9 -
 clang/test/SemaCXX/GH95854.cpp| 21 +
 4 files changed, 37 insertions(+), 2 deletions(-)
 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 
`_.
+  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..f4d90e19e0e05 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1392,7 +1392,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 75c441293d62e..0b963cb305d27 100644
--- a/clang/lib/AST/DeclCXX.cpp
+++ b/clang/lib/AST/DeclCXX.cpp
@@ -1018,6 +1018,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
@@ -1086,7 +1089,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 0..62ae549f2496f
--- /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}}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-27 Thread via cfe-commits


@@ -1392,7 +1392,8 @@ class CXXRecordDecl : public RecordDecl {
   bool allowConstDefaultInit() const {
 return !data().HasUninitializedFields ||
!(data().HasDefaultedDefaultConstructor ||
- needsImplicitDefaultConstructor());
+ needsImplicitDefaultConstructor()) ||
+   hasInClassInitializer();

ofAlpaca wrote:

Hi @zygoloid 
I apologize if it's a silly question, I'm new here and want to learn something 
from you.
@Rajveer100's commit has already fixed the test (GH95854.cpp).
> The two tests that were broken:

And these two mentioned testcases' errors are expected. The behavior of the fix 
is correct.
Then, what are you planning to do next?
Are you planning to improve the code by changing the behavior of 
`HasUninitializedFields`?

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-25 Thread Richard Smith via cfe-commits


@@ -1392,7 +1392,8 @@ class CXXRecordDecl : public RecordDecl {
   bool allowConstDefaultInit() const {
 return !data().HasUninitializedFields ||
!(data().HasDefaultedDefaultConstructor ||
- needsImplicitDefaultConstructor());
+ needsImplicitDefaultConstructor()) ||
+   hasInClassInitializer();

zygoloid wrote:

I don't think so. Maybe we can change the behavior of `HasUninitializedFields` 
-- it only exists to support this check, so we can change it to do whatever 
this check needs. What I'm thinking is:

* If the class is not a union, then proceed as we currently do: start it off as 
`false` and set it to `true` if we find an uninitialized field.
* If the class is a union, then do the opposite: start it off as `true` and set 
it to `false` if we find an initialized field.

(So for a non-union, the field is true if all fields are const default 
initializable, and for a union, the field is true if *any* field is const 
default initializable.)

Then I think the only change we'd need here would be to also treat empty unions 
as const default initializable.

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-25 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 edited 
https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-25 Thread Rajveer Singh Bharadwaj via cfe-commits


@@ -1392,7 +1392,8 @@ class CXXRecordDecl : public RecordDecl {
   bool allowConstDefaultInit() const {
 return !data().HasUninitializedFields ||
!(data().HasDefaultedDefaultConstructor ||
- needsImplicitDefaultConstructor());
+ needsImplicitDefaultConstructor()) ||
+   hasInClassInitializer();

Rajveer100 wrote:

@zygoloid 
Is there an existing method for checking nested structures for cases like you 
mentioned above?

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-22 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 updated 
https://github.com/llvm/llvm-project/pull/96301

>From b964923b9610c9cd53e4d1de8f5d51d8fcebc78c Mon Sep 17 00:00:00 2001
From: Rajveer 
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 
`_.
  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 
`_.
+  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 0..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;

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-22 Thread Rajveer Singh Bharadwaj via cfe-commits


@@ -1392,7 +1392,8 @@ class CXXRecordDecl : public RecordDecl {
   bool allowConstDefaultInit() const {
 return !data().HasUninitializedFields ||
!(data().HasDefaultedDefaultConstructor ||
- needsImplicitDefaultConstructor());
+ needsImplicitDefaultConstructor()) ||
+   hasInClassInitializer();

Rajveer100 wrote:

The two tests that were broken:

```c++
struct some_init {
  int a = 0;
  int b;
  int c = 0;
};

void constobjs() {
  // ...
  const some_init si;
  // ...
}
```

```c++
struct Ints2 {
  int a = 10;
  int b;
};
constexpr Ints2 ints22;
```

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-22 Thread Rajveer Singh Bharadwaj via cfe-commits


@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s

Rajveer100 wrote:

Indeed.

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-22 Thread Rajveer Singh Bharadwaj via cfe-commits


@@ -1392,7 +1392,8 @@ class CXXRecordDecl : public RecordDecl {
   bool allowConstDefaultInit() const {
 return !data().HasUninitializedFields ||
!(data().HasDefaultedDefaultConstructor ||
- needsImplicitDefaultConstructor());
+ needsImplicitDefaultConstructor()) ||
+   hasInClassInitializer();

Rajveer100 wrote:

> This is a language extension so we get to choose its semantics, but I think 
> the most logical choice is to reject because the union member is not _fully_ 
> initialized.

At the moment, this gets accepted.

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-21 Thread Richard Smith via cfe-commits


@@ -1392,7 +1392,8 @@ class CXXRecordDecl : public RecordDecl {
   bool allowConstDefaultInit() const {
 return !data().HasUninitializedFields ||
!(data().HasDefaultedDefaultConstructor ||
- needsImplicitDefaultConstructor());
+ needsImplicitDefaultConstructor()) ||
+   hasInClassInitializer();

zygoloid wrote:

What happens for:

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

This is a language extension so we get to choose its semantics, but I think the 
most logical choice is to reject because the union member is not *fully* 
initialized.

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-21 Thread Shafik Yaghmour via cfe-commits


@@ -1392,7 +1392,8 @@ class CXXRecordDecl : public RecordDecl {
   bool allowConstDefaultInit() const {
 return !data().HasUninitializedFields ||
!(data().HasDefaultedDefaultConstructor ||
- needsImplicitDefaultConstructor());
+ needsImplicitDefaultConstructor()) ||
+   hasInClassInitializer();

shafik wrote:

Based on the comment in the bug report I think this needs to be `(isUnion() && 
hasInClassInitializer())`

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-21 Thread Shafik Yaghmour via cfe-commits


@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s

shafik wrote:

I think this may be a more appropriate place for the test:

https://github.com/llvm/llvm-project/blob/main/clang/test/SemaCXX/anonymous-union.cpp

We usually wrap tests from github issues in a namespace starting with `GH` and 
followed by the issue number for this case `namespace GH95854`

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-21 Thread Shafik Yaghmour via cfe-commits

https://github.com/shafik edited https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-21 Thread Shafik Yaghmour via cfe-commits

https://github.com/shafik commented:

Thank you for this fix. It needs a release note and your summary should have 
some more details on what the cause of the bug is and how your PR fixes it. 
efriedma-quic's comment provides a good framework for explaining the cause.

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-21 Thread Rajveer Singh Bharadwaj via cfe-commits

Rajveer100 wrote:

@zygoloid 

https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-21 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Rajveer Singh Bharadwaj (Rajveer100)


Changes

Resolves #95854

-- As per https://eel.is/c++draft/dcl.init#general-8.3

---
Full diff: https://github.com/llvm/llvm-project/pull/96301.diff


2 Files Affected:

- (modified) clang/include/clang/AST/DeclCXX.h (+2-1) 
- (added) clang/test/Sema/debug-95854.cpp (+11) 


``diff
diff --git a/clang/include/clang/AST/DeclCXX.h 
b/clang/include/clang/AST/DeclCXX.h
index fb52ac804849d..1d1083a5d6205 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1392,7 +1392,8 @@ class CXXRecordDecl : public RecordDecl {
   bool allowConstDefaultInit() const {
 return !data().HasUninitializedFields ||
!(data().HasDefaultedDefaultConstructor ||
- needsImplicitDefaultConstructor());
+ needsImplicitDefaultConstructor()) ||
+   hasInClassInitializer();
   }
 
   /// Determine whether this class has a destructor which has no
diff --git a/clang/test/Sema/debug-95854.cpp b/clang/test/Sema/debug-95854.cpp
new file mode 100644
index 0..1fb976558650d
--- /dev/null
+++ b/clang/test/Sema/debug-95854.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+//
+// expected-no-diagnostics
+
+struct A {
+  union {
+int n = 0;
+int m;
+  };
+};
+const A a;

``




https://github.com/llvm/llvm-project/pull/96301
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[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)

2024-06-21 Thread Rajveer Singh Bharadwaj via cfe-commits

https://github.com/Rajveer100 created 
https://github.com/llvm/llvm-project/pull/96301

Resolves #95854

-- As per https://eel.is/c++draft/dcl.init#general-8.3

>From c8f2496e91d58c8704911665e1bf1dd7dfbb1d2e Mon Sep 17 00:00:00 2001
From: Rajveer 
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

-- As per https://eel.is/c++draft/dcl.init#general-8.3
---
 clang/include/clang/AST/DeclCXX.h |  3 ++-
 clang/test/Sema/debug-95854.cpp   | 11 +++
 2 files changed, 13 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Sema/debug-95854.cpp

diff --git a/clang/include/clang/AST/DeclCXX.h 
b/clang/include/clang/AST/DeclCXX.h
index fb52ac804849d..1d1083a5d6205 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -1392,7 +1392,8 @@ class CXXRecordDecl : public RecordDecl {
   bool allowConstDefaultInit() const {
 return !data().HasUninitializedFields ||
!(data().HasDefaultedDefaultConstructor ||
- needsImplicitDefaultConstructor());
+ needsImplicitDefaultConstructor()) ||
+   hasInClassInitializer();
   }
 
   /// Determine whether this class has a destructor which has no
diff --git a/clang/test/Sema/debug-95854.cpp b/clang/test/Sema/debug-95854.cpp
new file mode 100644
index 0..1fb976558650d
--- /dev/null
+++ b/clang/test/Sema/debug-95854.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+//
+// expected-no-diagnostics
+
+struct A {
+  union {
+int n = 0;
+int m;
+  };
+};
+const A a;

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits