[clang] [Clang] Fix an inadvertent overwrite of sub-initializers (PR #140714)
via cfe-commits
cfe-commits at lists.llvm.org
Tue May 20 04:34:49 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Younan Zhang (zyn0217)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/140714.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+1)
- (modified) clang/lib/Sema/SemaInit.cpp (+8-4)
- (modified) clang/test/SemaTemplate/deduction-guide.cpp (+27)
``````````diff
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'
+
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/140714
More information about the cfe-commits
mailing list