Skip to content

Commit c08b80e

Browse files
authored
[Clang] Remove the PackExpansion restrictions for rewrite substitution (#126206)
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 cases 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. Fixes #124715
1 parent 2a7487c commit c08b80e

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

clang/lib/Sema/SemaTemplate.cpp

+1-1
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

+16-16
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,18 @@ namespace {
14661466
}
14671467
}
14681468

1469+
static TemplateArgument
1470+
getTemplateArgumentPackPatternForRewrite(const TemplateArgument &TA) {
1471+
if (TA.getKind() != TemplateArgument::Pack)
1472+
return TA;
1473+
assert(TA.pack_size() == 1 &&
1474+
"unexpected pack arguments in template rewrite");
1475+
TemplateArgument Arg = *TA.pack_begin();
1476+
if (Arg.isPackExpansion())
1477+
Arg = Arg.getPackExpansionPattern();
1478+
return Arg;
1479+
}
1480+
14691481
/// Transform the given declaration by instantiating a reference to
14701482
/// this declaration.
14711483
Decl *TransformDecl(SourceLocation Loc, Decl *D);
@@ -1627,7 +1639,7 @@ namespace {
16271639
TemplateArgumentLoc Input = SemaRef.getTrivialTemplateArgumentLoc(
16281640
pack, QualType(), SourceLocation{});
16291641
TemplateArgumentLoc Output;
1630-
if (SemaRef.SubstTemplateArgument(Input, TemplateArgs, Output))
1642+
if (TransformTemplateArgument(Input, Output, Uneval))
16311643
return true; // fails
16321644
TArgs.push_back(Output.getArgument());
16331645
}
@@ -2040,11 +2052,7 @@ TemplateName TemplateInstantiator::TransformTemplateName(
20402052
if (TemplateArgs.isRewrite()) {
20412053
// We're rewriting the template parameter as a reference to another
20422054
// template parameter.
2043-
if (Arg.getKind() == TemplateArgument::Pack) {
2044-
assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion() &&
2045-
"unexpected pack arguments in template rewrite");
2046-
Arg = Arg.pack_begin()->getPackExpansionPattern();
2047-
}
2055+
Arg = getTemplateArgumentPackPatternForRewrite(Arg);
20482056
assert(Arg.getKind() == TemplateArgument::Template &&
20492057
"unexpected nontype template argument kind in template rewrite");
20502058
return Arg.getAsTemplate();
@@ -2125,11 +2133,7 @@ TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
21252133
if (TemplateArgs.isRewrite()) {
21262134
// We're rewriting the template parameter as a reference to another
21272135
// template parameter.
2128-
if (Arg.getKind() == TemplateArgument::Pack) {
2129-
assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion() &&
2130-
"unexpected pack arguments in template rewrite");
2131-
Arg = Arg.pack_begin()->getPackExpansionPattern();
2132-
}
2136+
Arg = getTemplateArgumentPackPatternForRewrite(Arg);
21332137
assert(Arg.getKind() == TemplateArgument::Expression &&
21342138
"unexpected nontype template argument kind in template rewrite");
21352139
// FIXME: This can lead to the same subexpression appearing multiple times
@@ -2591,11 +2595,7 @@ TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
25912595
if (TemplateArgs.isRewrite()) {
25922596
// We're rewriting the template parameter as a reference to another
25932597
// template parameter.
2594-
if (Arg.getKind() == TemplateArgument::Pack) {
2595-
assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion() &&
2596-
"unexpected pack arguments in template rewrite");
2597-
Arg = Arg.pack_begin()->getPackExpansionPattern();
2598-
}
2598+
Arg = getTemplateArgumentPackPatternForRewrite(Arg);
25992599
assert(Arg.getKind() == TemplateArgument::Type &&
26002600
"unexpected nontype template argument kind in template rewrite");
26012601
QualType NewT = Arg.getAsType();

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

+46
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)