Author: Younan Zhang Date: 2025-05-21T10:02:34+08:00 New Revision: 701fe51fd667904d160fb46b6ceba09006fe8291
URL: https://github.com/llvm/llvm-project/commit/701fe51fd667904d160fb46b6ceba09006fe8291 DIFF: https://github.com/llvm/llvm-project/commit/701fe51fd667904d160fb46b6ceba09006fe8291.diff LOG: [Clang] Fix an inadvertent overwrite of sub-initializers (#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 Added: Modified: clang/docs/ReleaseNotes.rst clang/lib/Sema/SemaInit.cpp clang/test/SemaTemplate/deduction-guide.cpp Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index ad8409397ff8a..b466f3758e0b6 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -703,6 +703,7 @@ Bug Fixes to C++ Support certain diff erences 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 0ed1ac495f3e8..3c3f796044471 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