Skip to content

[Clang] Fix a pack expansion bug in template argument deduction #141547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025

Conversation

zyn0217
Copy link
Contributor

@zyn0217 zyn0217 commented May 27, 2025

I think the intent of df18ee9 was to substitute only those non-packs into a pack expansion type (e.g. T in T::pack...), so let's hold off pack expansions explicitly, in case there are calls coming from a substitution of pack expansion.

Fixes #53609

I think the intent of df18ee9 was to substitute only those non-packs
into a pack expansion type (e.g. `T` in `T::pack`...), so let's hold off
pack expansions explicitly, in case there are calls coming from a
substitution of pack expansion.
@zyn0217 zyn0217 requested review from cor3ntin and erichkeane May 27, 2025 06:12
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 27, 2025
@llvmbot
Copy link
Member

llvmbot commented May 27, 2025

@llvm/pr-subscribers-clang

Author: Younan Zhang (zyn0217)

Changes

I think the intent of df18ee9 was to substitute only those non-packs into a pack expansion type (e.g. T in T::pack...), so let's hold off pack expansions explicitly, in case there are calls coming from a substitution of pack expansion.

Fixes #53609


Full diff: https://github.com/llvm/llvm-project/pull/141547.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/Sema/SemaTemplateDeduction.cpp (+1)
  • (modified) clang/test/CXX/temp/temp.fct.spec/temp.deduct/p7.cpp (+17)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b93fa33acc2a0..2c46a8f8ac86d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -795,6 +795,7 @@ Bug Fixes to C++ Support
 - Fix instantiation of default-initialized variable template specialization. (#GH140632) (#GH140622)
 - Clang modules now allow a module and its user to differ on TrivialAutoVarInit*
 - Fixed an access checking bug when initializing non-aggregates in default arguments (#GH62444), (#GH83608)
+- Fixed a pack substitution bug in deducing class template partial specializations. (#GH53609)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index 217d57d67f067..75ae04b27d06a 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -2946,6 +2946,7 @@ ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param,
       LocalInstantiationScope Scope(S);
       MultiLevelTemplateArgumentList Args(Template, CTAI.SugaredConverted,
                                           /*Final=*/true);
+      Sema::ArgPackSubstIndexRAII OnlySubstNonPackExpansion(S, std::nullopt);
 
       if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
         Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template,
diff --git a/clang/test/CXX/temp/temp.fct.spec/temp.deduct/p7.cpp b/clang/test/CXX/temp/temp.fct.spec/temp.deduct/p7.cpp
index f0ab7b8ea7612..de6fa0c837e2a 100644
--- a/clang/test/CXX/temp/temp.fct.spec/temp.deduct/p7.cpp
+++ b/clang/test/CXX/temp/temp.fct.spec/temp.deduct/p7.cpp
@@ -54,3 +54,20 @@ namespace reversed_operator_substitution_order {
   float &s = no_adl::f<int>(true);
 }
 #endif
+
+namespace GH53609 {
+
+template <class, int>
+struct a;
+
+template <class, class...>
+struct b;
+
+template <class x, class... y, y... z>
+struct b<x, a<y, z>...> {};
+
+template <class... x> struct c: b<x>...  {};
+
+c<int> d;
+
+}

@zyn0217 zyn0217 merged commit f8d6316 into llvm:main May 27, 2025
14 of 15 checks passed
@zyn0217 zyn0217 deleted the pack-53609 branch May 27, 2025 07:10
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
…#141547)

I think the intent of df18ee9 was to substitute only those non-packs
into a pack expansion type (e.g. `T` in `T::pack...`), so let's hold off
pack expansions explicitly, in case there are calls coming from a
substitution of pack expansion.

Fixes llvm#53609
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash in getPackSubstitutedTemplateArgument
3 participants