[clang] [clang][Sema] fix incorrect ambiguous function call which use designated-initializer as template argument (PR #167159)
Zhikai Zeng via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 8 09:21:29 PST 2025
https://github.com/Backl1ght created https://github.com/llvm/llvm-project/pull/167159
Fixes https://github.com/llvm/llvm-project/issues/166784
The cause is that we will cache `KnownField` after check of `B`, and use the cache incorrectly when performing check of type `A`. This pr add a check to make sure we use correct cache.
https://github.com/llvm/llvm-project/blob/545c3022d28164f5040036a7b515a85f74dbd5cc/clang/lib/Sema/SemaInit.cpp#L2939-L2957
>From 967b4d0a9341b88ee4c205299958dfdd897be592 Mon Sep 17 00:00:00 2001
From: Backl1ght <backlight.zzk at gmail.com>
Date: Sat, 25 Oct 2025 14:25:55 +0000
Subject: [PATCH 1/2] fix
---
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 4d58f00168298..2d3d8ef5279ce 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -7060,8 +7060,15 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
// anonymous unions in class templates).
}
- if (!ParentDependsOnArgs)
+ if (!ParentDependsOnArgs) {
+ if (auto Found =
+ CurrentInstantiationScope
+ ? CurrentInstantiationScope->getInstantiationOfIfExists(D)
+ : nullptr) {
+ return cast<NamedDecl>(Found->dyn_cast<Decl *>());
+ }
return D;
+ }
ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
if (!ParentDC)
>From f5a30cf44cef6da79039b40c828cd9a27bf10ebe Mon Sep 17 00:00:00 2001
From: Backl1ght <backlight.zzk at gmail.com>
Date: Sat, 8 Nov 2025 17:13:21 +0000
Subject: [PATCH 2/2] add check
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/Sema/SemaInit.cpp | 2 ++
.../SemaTemplate/temp_arg_nontype_cxx2c.cpp | 23 +++++++++++++++++++
3 files changed, 27 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6b396e7ba63f3..869223512ab29 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -459,6 +459,8 @@ Bug Fixes in This Version
- Fixed a crash triggered by unterminated ``__has_embed``. (#GH162953)
- Accept empty enumerations in MSVC-compatible C mode. (#GH114402)
- Fixed false-positive shadow diagnostics for lambdas in explicit object member functions. (#GH163731)
+- Fixed a incorrect diagnostic for ambiguous function call that use a
+ designated-initializer as template argument. (#GH166784)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index cc6ddf568d346..58c1c01c9b3ba 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -2937,6 +2937,8 @@ InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity,
}
FieldDecl *KnownField = D->getFieldDecl();
+ if (KnownField && KnownField->getParent() != RD)
+ KnownField = nullptr;
if (!KnownField) {
const IdentifierInfo *FieldName = D->getFieldName();
ValueDecl *VD = SemaRef.tryLookupUnambiguousFieldDecl(RD, FieldName);
diff --git a/clang/test/SemaTemplate/temp_arg_nontype_cxx2c.cpp b/clang/test/SemaTemplate/temp_arg_nontype_cxx2c.cpp
index c4ac36e263bc8..613956324cb35 100644
--- a/clang/test/SemaTemplate/temp_arg_nontype_cxx2c.cpp
+++ b/clang/test/SemaTemplate/temp_arg_nontype_cxx2c.cpp
@@ -134,3 +134,26 @@ namespace error_on_type_instantiation {
template void g<int>();
// expected-note at -1 {{in instantiation of function template specialization}}
}
+
+namespace GH166784 {
+
+struct A {
+ int a;
+};
+struct B {
+ int b;
+};
+template <A a> void f() {
+ static_assert(a.a == 42);
+}
+template <B b> void f() {
+ static_assert(b.b == 42);
+}
+
+using T1 = decltype(f<{.a = 42}>());
+using T2 = decltype(f<A{.a = 42}>());
+
+using T3 = decltype(f<{.b = 42}>());
+using T4 = decltype(f<B{.b = 42}>());
+
+} // namespace GH166784
More information about the cfe-commits
mailing list