[clang] [Clang] Reset ArgPackSubstIndex before rewriting CTAD template parameters (PR #141741)
Younan Zhang via cfe-commits
cfe-commits at lists.llvm.org
Wed May 28 08:33:02 PDT 2025
https://github.com/zyn0217 updated https://github.com/llvm/llvm-project/pull/141741
>From 4fdb0069e260e36d8cbd021536adc14f6b9ddef1 Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Wed, 28 May 2025 18:37:38 +0800
Subject: [PATCH 1/2] [Clang] Reset ArgPackSubstIndex before rewriting CTAD
template parameters
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.
---
clang/lib/Sema/SemaTemplateDeductionGuide.cpp | 4 ++
clang/test/SemaTemplate/deduction-guide.cpp | 53 +++++++++++++++++++
2 files changed, 57 insertions(+)
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
+
+}
>From baacafbf8ec3249bad7f827501db6f9291ee26cf Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Wed, 28 May 2025 23:32:52 +0800
Subject: [PATCH 2/2] Address feedback
Co-authored-by: Erich Keane <ekeane at nvidia.com>
---
clang/lib/Sema/SemaTemplateDeductionGuide.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
index b5394a75479f1..c75013b05d990 100644
--- a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
+++ b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
@@ -1102,7 +1102,7 @@ BuildDeductionGuideForTypeAlias(Sema &SemaRef,
// 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);
+ Sema::ArgPackSubstIndexRAII PackSubstReset(SemaRef, std::nullopt);
for (unsigned AliasTemplateParamIdx : DeducedAliasTemplateParams) {
auto *TP =
More information about the cfe-commits
mailing list