Skip to content

Commit 67d907a

Browse files
zyn0217erichkeane
andauthored
[Clang] Reset ArgPackSubstIndex before rewriting CTAD template parameters (#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 #141425 --------- Co-authored-by: Erich Keane <ekeane@nvidia.com>
1 parent 87993eb commit 67d907a

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

clang/lib/Sema/SemaTemplateDeductionGuide.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,10 @@ BuildDeductionGuideForTypeAlias(Sema &SemaRef,
10991099
// parameters, used for building `TemplateArgsForBuildingFPrime`.
11001100
SmallVector<TemplateArgument, 16> TransformedDeducedAliasArgs(
11011101
AliasTemplate->getTemplateParameters()->size());
1102+
// We might be already within a pack expansion, but rewriting template
1103+
// parameters is independent of that. (We may or may not expand new packs
1104+
// when rewriting. So clear the state)
1105+
Sema::ArgPackSubstIndexRAII PackSubstReset(SemaRef, std::nullopt);
11021106

11031107
for (unsigned AliasTemplateParamIdx : DeducedAliasTemplateParams) {
11041108
auto *TP =

clang/test/SemaTemplate/deduction-guide.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,3 +913,56 @@ void f() {
913913
// CHECK-NEXT: `-ParmVarDecl {{.+}} 'int'
914914

915915
}
916+
917+
namespace GH141425 {
918+
919+
template<class... Lambda>
920+
struct Container
921+
{
922+
Container(Lambda...) {}
923+
};
924+
925+
template<class... T>
926+
using Alias = Container<T...>;
927+
928+
template<class = void>
929+
struct Invocable {
930+
using T = decltype([]() {
931+
(void)Alias([]() -> void {});
932+
}());
933+
};
934+
935+
struct Type {
936+
using T = bool;
937+
};
938+
939+
template<class...>
940+
struct ExpandType {
941+
using T = bool;
942+
};
943+
944+
template<class... X>
945+
using Expand = ExpandType<typename X::T...>;
946+
947+
Expand<Type, Invocable<>> _{};
948+
949+
// CHECK-LABEL: Dumping GH141425::<deduction guide for Alias>:
950+
// CHECK-NEXT: FunctionTemplateDecl {{.+}} implicit <deduction guide for Alias>
951+
// CHECK-NEXT: |-TemplateTypeParmDecl {{.+}} class depth 0 index 0 ... T
952+
// CHECK-NEXT: |-TypeTraitExpr {{.+}} 'bool' __is_deducible
953+
// CHECK-NEXT: | |-DeducedTemplateSpecializationType {{.+}} 'GH141425::Alias' dependent
954+
// CHECK-NEXT: | | `-name: 'GH141425::Alias'
955+
// CHECK-NEXT: | | `-TypeAliasTemplateDecl {{.+}} Alias
956+
// CHECK-NEXT: | `-TemplateSpecializationType {{.+}} 'Container<T...>' dependent
957+
// CHECK-NEXT: | |-name: 'Container':'GH141425::Container' qualified
958+
// CHECK-NEXT: | | `-ClassTemplateDecl {{.+}} Container
959+
// CHECK-NEXT: | `-TemplateArgument type 'T...':'type-parameter-0-0...'
960+
// CHECK-NEXT: | `-PackExpansionType {{.+}} 'T...' dependent
961+
// CHECK-NEXT: | `-SubstTemplateTypeParmType {{.+}} 'T' sugar dependent contains_unexpanded_pack class depth 0 index 0 ... Lambda pack_index 0
962+
// CHECK-NEXT: | |-FunctionTemplate {{.+}} '<deduction guide for Container>'
963+
// CHECK-NEXT: | `-TemplateTypeParmType {{.+}} 'T' dependent contains_unexpanded_pack depth 0 index 0 pack
964+
// CHECK-NEXT: | `-TemplateTypeParm {{.+}} 'T'
965+
// CHECK-NEXT: |-CXXDeductionGuideDecl {{.+}} implicit <deduction guide for Alias> 'auto (T...) -> Container<T...>'
966+
// CHECK-NEXT: | `-ParmVarDecl {{.+}} 'T...' pack
967+
968+
}

0 commit comments

Comments
 (0)