[clang] [Clang] Reset ArgPackSubstIndex before rewriting CTAD template parameters (PR #141741)
via cfe-commits
cfe-commits at lists.llvm.org
Wed May 28 03:45:54 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Younan Zhang (zyn0217)
<details>
<summary>Changes</summary>
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 https://github.com/llvm/llvm-project/issues/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
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
+
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/141741
More information about the cfe-commits
mailing list