[clang] [clang] Fix designated initializers inside templates (PR #69712)
Mariya Podchishchaeva via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 20 04:33:35 PDT 2023
https://github.com/Fznamznon created https://github.com/llvm/llvm-project/pull/69712
Skip anonymous members when rebuilding DesignatedInitExpr since designated inits for them are meant to be created during
`InitListChecker::CheckDesignatedInitializer` routine.
Fixes https://github.com/llvm/llvm-project/issues/65143
>From 60d90fc60363d1b45734fe4d6099f7a7db6f4497 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <mariya.podchishchaeva at intel.com>
Date: Fri, 20 Oct 2023 04:26:41 -0700
Subject: [PATCH] [clang] Fix designated initializers inside templates
Skip anonymous members when rebuilding DesignatedInitExpr since designated
inits for them are meant to be created during
`InitListChecker::CheckDesignatedInitializer` routine.
Fixes https://github.com/llvm/llvm-project/issues/65143
---
clang/docs/ReleaseNotes.rst | 3 +++
clang/lib/Sema/TreeTransform.h | 6 +++--
.../SemaCXX/cxx2b-designated-initializers.cpp | 23 +++++++++++++++++--
3 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index fc8caf9221b9d29..ffb32c254b256dd 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -412,6 +412,9 @@ Bug Fixes in This Version
- Clang no longer permits using the `_BitInt` types as an underlying type for an
enumeration as specified in the C23 Standard.
Fixes (`#69619 <https://github.com/llvm/llvm-project/issues/69619>`_)
+- Clang now accepts anonymous members initialized with designated initializers
+ inside templates.
+ Fixes (`#65143 <https://github.com/llvm/llvm-project/issues/65143>`_)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 8fafdd4f5caa1ed..094e5efa939f4d1 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -11783,8 +11783,6 @@ TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
bool ExprChanged = false;
for (const DesignatedInitExpr::Designator &D : E->designators()) {
if (D.isFieldDesignator()) {
- Desig.AddDesignator(Designator::CreateFieldDesignator(
- D.getFieldName(), D.getDotLoc(), D.getFieldLoc()));
if (D.getFieldDecl()) {
FieldDecl *Field = cast_or_null<FieldDecl>(
getDerived().TransformDecl(D.getFieldLoc(), D.getFieldDecl()));
@@ -11792,12 +11790,16 @@ TreeTransform<Derived>::TransformDesignatedInitExpr(DesignatedInitExpr *E) {
// Rebuild the expression when the transformed FieldDecl is
// different to the already assigned FieldDecl.
ExprChanged = true;
+ if (Field->isAnonymousStructOrUnion())
+ continue;
} else {
// Ensure that the designator expression is rebuilt when there isn't
// a resolved FieldDecl in the designator as we don't want to assign
// a FieldDecl to a pattern designator that will be instantiated again.
ExprChanged = true;
}
+ Desig.AddDesignator(Designator::CreateFieldDesignator(
+ D.getFieldName(), D.getDotLoc(), D.getFieldLoc()));
continue;
}
diff --git a/clang/test/SemaCXX/cxx2b-designated-initializers.cpp b/clang/test/SemaCXX/cxx2b-designated-initializers.cpp
index 5588926f419a932..1d9b9183b3679f1 100644
--- a/clang/test/SemaCXX/cxx2b-designated-initializers.cpp
+++ b/clang/test/SemaCXX/cxx2b-designated-initializers.cpp
@@ -9,17 +9,36 @@ union S {
};
void f(int x, auto) {
- const S result { // expected-error {{field designator (null) does not refer to any field in type 'const S'}}
+ const S result {
.a = x
};
}
void g(void) {
- f(0, 0); // expected-note {{in instantiation of function template specialization 'PR61118::f<int>' requested here}}
+ f(0, 0);
}
} // end namespace PR61118
+namespace GH65143 {
+struct Inner {
+ int a;
+};
+
+struct Outer {
+ struct {
+ Inner inner;
+ };
+};
+
+template <int val> void f() {
+ constexpr Outer x{.inner = {val}};
+ static_assert(x.inner.a == val);
+}
+
+void bar() { f<4>(); }
+}
+
namespace GH62156 {
union U1 {
int x;
More information about the cfe-commits
mailing list