Skip to content

Commit 2ce86d8

Browse files
committed
[Clang] Remove the PackExpansion restrictions for rewrite substitution
When substituting for rewrite purposes, as in rebuilding constraints for a synthesized deduction guide, it assumed that packs were in PackExpansion* form, such that the instantiator could extract a pattern. For type aliases CTAD, while rebuilding their associated constraints, this might not be the case because we'll call TransformTemplateArgument() for the alias template arguments, where there might be e.g. a non-pack expansion type into a pack expansion, so the assumption wouldn't hold. This patch fixes that by making it treat the non-pack expansions as direct patterns when rewriting.
1 parent 83a8bb3 commit 2ce86d8

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4905,7 +4905,7 @@ bool Sema::CheckTemplateTypeArgument(
49054905
[[fallthrough]];
49064906
}
49074907
default: {
4908-
// We allow instantiateing a template with template argument packs when
4908+
// We allow instantiating a template with template argument packs when
49094909
// building deduction guides.
49104910
if (Arg.getKind() == TemplateArgument::Pack &&
49114911
CodeSynthesisContexts.back().Kind ==

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ namespace {
16271627
TemplateArgumentLoc Input = SemaRef.getTrivialTemplateArgumentLoc(
16281628
pack, QualType(), SourceLocation{});
16291629
TemplateArgumentLoc Output;
1630-
if (SemaRef.SubstTemplateArgument(Input, TemplateArgs, Output))
1630+
if (TransformTemplateArgument(Input, Output, Uneval))
16311631
return true; // fails
16321632
TArgs.push_back(Output.getArgument());
16331633
}
@@ -2041,9 +2041,11 @@ TemplateName TemplateInstantiator::TransformTemplateName(
20412041
// We're rewriting the template parameter as a reference to another
20422042
// template parameter.
20432043
if (Arg.getKind() == TemplateArgument::Pack) {
2044-
assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion() &&
2044+
assert(Arg.pack_size() == 1 &&
20452045
"unexpected pack arguments in template rewrite");
2046-
Arg = Arg.pack_begin()->getPackExpansionPattern();
2046+
Arg = *Arg.pack_begin();
2047+
if (Arg.isPackExpansion())
2048+
Arg = Arg.getPackExpansionPattern();
20472049
}
20482050
assert(Arg.getKind() == TemplateArgument::Template &&
20492051
"unexpected nontype template argument kind in template rewrite");
@@ -2126,9 +2128,11 @@ TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
21262128
// We're rewriting the template parameter as a reference to another
21272129
// template parameter.
21282130
if (Arg.getKind() == TemplateArgument::Pack) {
2129-
assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion() &&
2131+
assert(Arg.pack_size() == 1 &&
21302132
"unexpected pack arguments in template rewrite");
2131-
Arg = Arg.pack_begin()->getPackExpansionPattern();
2133+
Arg = *Arg.pack_begin();
2134+
if (Arg.isPackExpansion())
2135+
Arg = Arg.getPackExpansionPattern();
21322136
}
21332137
assert(Arg.getKind() == TemplateArgument::Expression &&
21342138
"unexpected nontype template argument kind in template rewrite");
@@ -2592,9 +2596,11 @@ TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
25922596
// We're rewriting the template parameter as a reference to another
25932597
// template parameter.
25942598
if (Arg.getKind() == TemplateArgument::Pack) {
2595-
assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion() &&
2599+
assert(Arg.pack_size() == 1 &&
25962600
"unexpected pack arguments in template rewrite");
2597-
Arg = Arg.pack_begin()->getPackExpansionPattern();
2601+
Arg = *Arg.pack_begin();
2602+
if (Arg.isPackExpansion())
2603+
Arg = Arg.getPackExpansionPattern();
25982604
}
25992605
assert(Arg.getKind() == TemplateArgument::Type &&
26002606
"unexpected nontype template argument kind in template rewrite");

clang/test/AST/ast-dump-ctad-alias.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,49 @@ ATemplatedClass2 test2(list);
156156
// CHECK-NEXT: |-TypeTraitExpr {{.*}} 'bool' __is_deducible
157157

158158
} // namespace GH90209
159+
160+
namespace GH124715 {
161+
162+
template <class T, class... Args>
163+
concept invocable = true;
164+
165+
template <class T, class... Args> struct Struct {
166+
template <class U>
167+
requires invocable<U, Args...>
168+
Struct(U, Args...) {}
169+
};
170+
171+
template <class...> struct Packs {};
172+
173+
template <class Lambda, class... Args>
174+
Struct(Lambda lambda, Args... args) -> Struct<Lambda, Args...>;
175+
176+
template <class T, class... Ts> using Alias = Struct<T, Packs<Ts...>>;
177+
178+
void foo() {
179+
Alias([](int) {}, Packs<int>());
180+
}
181+
182+
// CHECK: |-FunctionTemplateDecl {{.*}} implicit <deduction guide for Alias>
183+
// CHECK-NEXT: | |-TemplateTypeParmDecl {{.*}} class depth 0 index 0 T
184+
// CHECK-NEXT: | |-TemplateTypeParmDecl {{.*}} class depth 0 index 1 ... Ts
185+
// CHECK-NEXT: | |-TemplateTypeParmDecl {{.*}} class depth 0 index 2 U
186+
// CHECK-NEXT: | |-BinaryOperator {{.*}} 'bool' '&&'
187+
// CHECK-NEXT: | | |-ConceptSpecializationExpr {{.*}} 'bool' Concept {{.*}} 'invocable'
188+
// CHECK-NEXT: | | | |-ImplicitConceptSpecializationDecl {{.*}}
189+
// CHECK-NEXT: | | | | |-TemplateArgument type 'type-parameter-0-2'
190+
// CHECK-NEXT: | | | | | `-TemplateTypeParmType {{.*}} 'type-parameter-0-2' dependent depth 0 index 2
191+
// CHECK-NEXT: | | | | `-TemplateArgument pack '<Packs<type-parameter-0-1...>>'
192+
// CHECK-NEXT: | | | | `-TemplateArgument type 'Packs<type-parameter-0-1...>'
193+
// CHECK-NEXT: | | | | `-TemplateSpecializationType {{.*}} 'Packs<type-parameter-0-1...>' dependent
194+
// CHECK-NEXT: | | | | |-name: 'GH124715::Packs'
195+
// CHECK-NEXT: | | | | | `-ClassTemplateDecl {{.*}} Packs
196+
// CHECK-NEXT: | | | | `-TemplateArgument pack '<type-parameter-0-1...>'
197+
// CHECK-NEXT: | | | | `-TemplateArgument type 'type-parameter-0-1...'
198+
// CHECK-NEXT: | | | | `-PackExpansionType {{.*}} 'type-parameter-0-1...' dependent
199+
// CHECK-NEXT: | | | | `-TemplateTypeParmType {{.*}} 'type-parameter-0-1' dependent contains_unexpanded_pack depth 0 index 1 pack
200+
// CHECK-NEXT: | | | |-TemplateArgument {{.*}} type 'U':'type-parameter-0-2'
201+
// CHECK-NEXT: | | | | `-TemplateTypeParmType {{.*}} 'U' dependent depth 0 index 2
202+
// CHECK-NEXT: | | | | `-TemplateTypeParm {{.*}} 'U'
203+
204+
} // namespace GH124715

0 commit comments

Comments
 (0)