Skip to content

[Clang] Reset ArgPackSubstIndex before rewriting CTAD template parameters #141741

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 2 commits into from
May 28, 2025

Conversation

zyn0217
Copy link
Contributor

@zyn0217 zyn0217 commented May 28, 2025

032ad59 taught the instantiator to expand template argument packs for rewrite. However we might already be in a pack expansion when we synthesizing the CTAD guide, so we reset the ArgPackSubstIndex to ensure it doesn't get confused.

No release note because this is a regression in Clang 21.

Fixes #141425

…ters

032ad59 taught the instantiator to expand template argument
packs for rewrite. However we might already be in a pack expansion when
we synthesizing the CTAD guide, so we reset the ArgPackSubstIndex to
ensure it doesn't get confused.
@zyn0217 zyn0217 requested review from cor3ntin and erichkeane May 28, 2025 10:45
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 28, 2025
@llvmbot
Copy link
Member

llvmbot commented May 28, 2025

@llvm/pr-subscribers-clang

Author: Younan Zhang (zyn0217)

Changes

032ad59 taught the instantiator to expand template argument packs for rewrite. However we might already be in a pack expansion when we synthesizing the CTAD guide, so we reset the ArgPackSubstIndex to ensure it doesn't get confused.

No release note because this is a regression in Clang 21.

Fixes #141425


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaTemplateDeductionGuide.cpp (+4)
  • (modified) clang/test/SemaTemplate/deduction-guide.cpp (+53)
diff --git a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
index 29c5736a9bf9e..b5394a75479f1 100644
--- a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
+++ b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
@@ -1099,6 +1099,10 @@ BuildDeductionGuideForTypeAlias(Sema &SemaRef,
   // parameters, used for building `TemplateArgsForBuildingFPrime`.
   SmallVector<TemplateArgument, 16> TransformedDeducedAliasArgs(
       AliasTemplate->getTemplateParameters()->size());
+  // We might be already within a pack expansion, but rewriting template
+  // parameters is independent of that. (We may or may not expand new packs
+  // when rewriting. So clear the state)
+  Sema::ArgPackSubstIndexRAII _(SemaRef, std::nullopt);
 
   for (unsigned AliasTemplateParamIdx : DeducedAliasTemplateParams) {
     auto *TP =
diff --git a/clang/test/SemaTemplate/deduction-guide.cpp b/clang/test/SemaTemplate/deduction-guide.cpp
index c1ce55e1c8029..faabba5539503 100644
--- a/clang/test/SemaTemplate/deduction-guide.cpp
+++ b/clang/test/SemaTemplate/deduction-guide.cpp
@@ -913,3 +913,56 @@ void f() {
 // CHECK-NEXT:   `-ParmVarDecl {{.+}} 'int'
 
 }
+
+namespace GH141425 {
+
+template<class... Lambda>
+struct Container
+{
+    Container(Lambda...) {}
+};
+
+template<class... T>
+using Alias = Container<T...>;
+
+template<class = void>
+struct Invocable {
+    using T = decltype([]() {
+        (void)Alias([]() -> void {});
+    }());
+};
+
+struct Type {
+    using T = bool;
+};
+
+template<class...>
+struct ExpandType {
+    using T = bool;
+};
+
+template<class... X>
+using Expand = ExpandType<typename X::T...>;
+
+Expand<Type, Invocable<>> _{};
+
+// CHECK-LABEL: Dumping GH141425::<deduction guide for Alias>:
+// CHECK-NEXT:  FunctionTemplateDecl {{.+}} implicit <deduction guide for Alias>
+// CHECK-NEXT:   |-TemplateTypeParmDecl {{.+}} class depth 0 index 0 ... T
+// CHECK-NEXT:   |-TypeTraitExpr {{.+}} 'bool' __is_deducible
+// CHECK-NEXT:   | |-DeducedTemplateSpecializationType {{.+}} 'GH141425::Alias' dependent
+// CHECK-NEXT:   | | `-name: 'GH141425::Alias'
+// CHECK-NEXT:   | |   `-TypeAliasTemplateDecl {{.+}} Alias
+// CHECK-NEXT:   | `-TemplateSpecializationType {{.+}} 'Container<T...>' dependent
+// CHECK-NEXT:   |   |-name: 'Container':'GH141425::Container' qualified
+// CHECK-NEXT:   |   | `-ClassTemplateDecl {{.+}} Container
+// CHECK-NEXT:   |   `-TemplateArgument type 'T...':'type-parameter-0-0...'
+// CHECK-NEXT:   |     `-PackExpansionType {{.+}} 'T...' dependent
+// CHECK-NEXT:   |       `-SubstTemplateTypeParmType {{.+}} 'T' sugar dependent contains_unexpanded_pack class depth 0 index 0 ... Lambda pack_index 0
+// CHECK-NEXT:   |         |-FunctionTemplate {{.+}} '<deduction guide for Container>'
+// CHECK-NEXT:   |         `-TemplateTypeParmType {{.+}} 'T' dependent contains_unexpanded_pack depth 0 index 0 pack
+// CHECK-NEXT:   |           `-TemplateTypeParm {{.+}} 'T'
+// CHECK-NEXT:   |-CXXDeductionGuideDecl {{.+}} implicit <deduction guide for Alias> 'auto (T...) -> Container<T...>'
+// CHECK-NEXT:   | `-ParmVarDecl {{.+}} 'T...' pack
+
+}

Co-authored-by: Erich Keane <ekeane@nvidia.com>
@zyn0217 zyn0217 merged commit 67d907a into llvm:main May 28, 2025
6 of 9 checks passed
@zyn0217 zyn0217 deleted the GH141425 branch May 28, 2025 15:33
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the quick fix!

sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
…ters (llvm#141741)

032ad59 taught the instantiator to expand template argument packs for
rewrite. However we might already be in a pack expansion when we
synthesizing the CTAD guide, so we reset the ArgPackSubstIndex to ensure
it doesn't get confused.

No release note because this is a regression in Clang 21.

Fixes llvm#141425

---------

Co-authored-by: Erich Keane <ekeane@nvidia.com>
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.

[Clang] ICE when lambda is passed to alias template using CTAD
4 participants