-
Notifications
You must be signed in to change notification settings - Fork 14.1k
[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
Conversation
…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.
@llvm/pr-subscribers-clang Author: Younan Zhang (zyn0217) Changes032ad59 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:
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>
There was a problem hiding this 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!
…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>
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