https://github.com/zyn0217 created 
https://github.com/llvm/llvm-project/pull/140714

When using InitChecker with VerifyOnly, we create a new designated initializer 
to handle anonymous fields. However in the last call to 
CheckDesignatedInitializer, the subinitializer isn't properly preserved but it 
gets overwritten by the cloned one. Which causes the initializer to reference 
the dependent field, breaking assumptions when we initialize the instantiated 
specialization.

Fixes https://github.com/llvm/llvm-project/issues/67173

>From ec6c540f17d767b3fd882f60b0757e6e4e35442a Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7...@gmail.com>
Date: Tue, 20 May 2025 19:19:47 +0800
Subject: [PATCH] [Clang] Fix an inadvertent overwrite of sub-initializers

When using InitChecker with VerifyOnly, we create a new designated
initializer to handle anonymous fields. However in the last call to
CheckDesignatedInitializer, the subinitializer isn't properly
preserved but it gets overwritten by the cloned one. Which causes
the initializer to reference the dependent field, breaking
assumptions when we initialize the instantiated specialization.
---
 clang/docs/ReleaseNotes.rst                 |  1 +
 clang/lib/Sema/SemaInit.cpp                 | 12 ++++++---
 clang/test/SemaTemplate/deduction-guide.cpp | 27 +++++++++++++++++++++
 3 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f04cb7b91788c..6f97cf79c5058 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -690,6 +690,7 @@ Bug Fixes to C++ Support
   certain differences in qualifiers (this could happen during template argument
   deduction or when building a ternary operator). (#GH97005)
 - Fixed type alias CTAD issues involving default template arguments. 
(#GH134471)
+- Fixed CTAD issues when initializing anonymous fields with designated 
initializers. (#GH67173)
 - The initialization kind of elements of structured bindings
   direct-list-initialized from an array is corrected to direct-initialization.
 - Clang no longer crashes when a coroutine is declared ``[[noreturn]]``. 
(#GH127327)
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 9ee8603ff7811..1f204f8306842 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -2791,16 +2791,20 @@ InitListChecker::CheckDesignatedInitializer(const 
InitializedEntity &Entity,
     // initializer list that the child calls see, so that we don't try
     // to re-process the designator.
     unsigned OldIndex = Index;
-    IList->setInit(OldIndex, DIE->getInit());
+    auto *OldDIE =
+        dyn_cast_if_present<DesignatedInitExpr>(IList->getInit(OldIndex));
+    if (!OldDIE)
+      OldDIE = DIE;
+    IList->setInit(OldIndex, OldDIE->getInit());
 
     CheckSubElementType(Entity, IList, CurrentObjectType, Index, 
StructuredList,
                         StructuredIndex, /*DirectlyDesignated=*/true);
 
     // Restore the designated initializer expression in the syntactic
     // form of the initializer list.
-    if (IList->getInit(OldIndex) != DIE->getInit())
-      DIE->setInit(IList->getInit(OldIndex));
-    IList->setInit(OldIndex, DIE);
+    if (IList->getInit(OldIndex) != OldDIE->getInit())
+      OldDIE->setInit(IList->getInit(OldIndex));
+    IList->setInit(OldIndex, OldDIE);
 
     return hadError && !prevHadError;
   }
diff --git a/clang/test/SemaTemplate/deduction-guide.cpp 
b/clang/test/SemaTemplate/deduction-guide.cpp
index dabd0cf12f77e..c1ce55e1c8029 100644
--- a/clang/test/SemaTemplate/deduction-guide.cpp
+++ b/clang/test/SemaTemplate/deduction-guide.cpp
@@ -886,3 +886,30 @@ CC c{};
 // CHECK-NEXT:        `-ClassTemplateSpecialization {{.+}} 'A'
 
 }
+
+namespace GH67173 {
+
+template <class T> struct Vec2d {
+  struct {
+    T x;
+    T y;
+  };
+};
+
+void f() {
+  Vec2d v{.x = 1, .y = 2};
+}
+
+// CHECK-LABEL: Dumping GH67173::<deduction guide for Vec2d>:
+// CHECK-NEXT: FunctionTemplateDecl {{.+}} implicit <deduction guide for Vec2d>
+// CHECK-NEXT: |-TemplateTypeParmDecl {{.+}} referenced class depth 0 index 0 T
+// CHECK:      |-CXXDeductionGuideDecl {{.+}} implicit <deduction guide for 
Vec2d> 'auto (T, T) -> Vec2d<T>' aggregate
+// CHECK-NEXT: | |-ParmVarDecl {{.+}} col:27 'T'
+// CHECK-NEXT: | `-ParmVarDecl {{.+}} col:27 'T'
+// CHECK-NEXT: `-CXXDeductionGuideDecl {{.+}} implicit used <deduction guide 
for Vec2d> 'auto (int, int) -> GH67173::Vec2d<int>' implicit_instantiation 
aggregate
+// CHECK-NEXT:   |-TemplateArgument type 'int'
+// CHECK-NEXT:   | `-BuiltinType {{.+}} 'int'
+// CHECK-NEXT:   |-ParmVarDecl {{.+}} 'int'
+// CHECK-NEXT:   `-ParmVarDecl {{.+}} 'int'
+
+}

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

Reply via email to