https://github.com/zwuis created 
https://github.com/llvm/llvm-project/pull/215235

`getDepthAndIndex(const NamedDecl *)` assumes its parameter refers to a 
template parameter. Bail out before calling it for function parameter packs.

Fix #28877. Fix #213760.

>From 2fb93a84edce0012e09c4c633c091f25b96001b0 Mon Sep 17 00:00:00 2001
From: Yanzuo Liu <[email protected]>
Date: Mon, 10 Aug 2026 18:15:58 +0800
Subject: [PATCH] Handle function parameter packs in
 `getDepthAndIndex(UnexpandedParameterPack)`

---
 clang/docs/ReleaseNotes.md                  |  2 ++
 clang/include/clang/Sema/SemaInternal.h     | 12 +++++++-----
 clang/test/SemaTemplate/deduction-crash.cpp | 16 ++++++++++++++++
 3 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index fc947d05fad83..75a5afd7a2648 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -445,6 +445,8 @@ features cannot lower the translation-unit ABI level;
   affect C++26 constexpr structured bindings and expansion statements, but
   also affects some uses of plain structured bindings. (#GH211930)
 
+- Fixed a crash during template argument deduction where a function parameter 
pack is referenced by other types in the function type. (GH28877, GH213760)
+
 #### Bug Fixes to AST Handling
 
 - Fixed a non-deterministic ordering of unused local typedefs that made
diff --git a/clang/include/clang/Sema/SemaInternal.h 
b/clang/include/clang/Sema/SemaInternal.h
index 8f6041b5f00e4..58b9ec537b4bd 100644
--- a/clang/include/clang/Sema/SemaInternal.h
+++ b/clang/include/clang/Sema/SemaInternal.h
@@ -77,11 +77,13 @@ inline std::optional<std::pair<unsigned, unsigned>>
 getDepthAndIndex(UnexpandedParameterPack UPP) {
   if (const auto *TTP = dyn_cast<const TemplateTypeParmType *>(UPP.first))
     return std::make_pair(TTP->getDepth(), TTP->getIndex());
-  if (isa<NamedDecl *>(UPP.first))
-    return getDepthAndIndex(cast<NamedDecl *>(UPP.first));
-  assert((isa<const TemplateSpecializationType *,
-              const SubstBuiltinTemplatePackType *>(UPP.first)));
-  return std::nullopt;
+  if (isa<const TemplateSpecializationType *,
+          const SubstBuiltinTemplatePackType *>(UPP.first))
+    return std::nullopt;
+  const auto *ND = cast<NamedDecl *>(UPP.first);
+  if (isa<ParmVarDecl>(ND))
+    return std::nullopt;
+  return getDepthAndIndex(ND);
 }
 
 class TypoCorrectionConsumer : public VisibleDeclConsumer {
diff --git a/clang/test/SemaTemplate/deduction-crash.cpp 
b/clang/test/SemaTemplate/deduction-crash.cpp
index e7018fd0d8338..2b0befb34a2df 100644
--- a/clang/test/SemaTemplate/deduction-crash.cpp
+++ b/clang/test/SemaTemplate/deduction-crash.cpp
@@ -177,3 +177,19 @@ namespace GH177545 {
   template<decltype(auto)()() volatile throw() -> char> // expected-error 
{{'decltype(auto)' can only be used as a return type in a function declaration}}
   struct T2;                                            // expected-error@* 
{{function cannot return function type 'auto () volatile throw() -> 
decltype(auto)'}}
 }
+
+namespace GH28877 {
+template <typename...> struct S;
+template <typename... Ts> auto f(Ts... args) -> S<decltype(args)...>;
+extern template auto f() -> S<>;
+}
+
+namespace GH46548 {
+template <typename... Ts> void a(Ts... args1, char... args2[][sizeof args1]);
+extern template void a();
+}
+
+namespace GH213760 {
+template <typename... Ts> void f(Ts... args, decltype(args)...);
+void g() { f(); }
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to