Skip to content

Commit 1553b21

Browse files
authored
[clang] CTAD alias: Fix missing template arg packs during the transformation (#92535)
clang rejects some valid code (see testcases) because of an incorrect transformed deduction guides. This patch fixes it. We miss the template argument packs during the transformation (`auto (type-parameter-0-0...) -> Foo<>`). In `TreeTransform::TransformTemplateArguments `, we have a logic of handling template argument packs which were originally added to support CTAD alias, it doesn't seem to be needed, we need to unpack them.
1 parent d108fa0 commit 1553b21

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

clang/lib/Sema/TreeTransform.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4818,14 +4818,6 @@ bool TreeTransform<Derived>::TransformTemplateArguments(
48184818
TemplateArgumentLoc In = *First;
48194819

48204820
if (In.getArgument().getKind() == TemplateArgument::Pack) {
4821-
// When building the deduction guides, we rewrite the argument packs
4822-
// instead of unpacking.
4823-
if (getSema().CodeSynthesisContexts.back().Kind ==
4824-
Sema::CodeSynthesisContext::BuildingDeductionGuides) {
4825-
if (getDerived().TransformTemplateArgument(In, Out, Uneval))
4826-
return true;
4827-
continue;
4828-
}
48294821
// Unpack argument packs, which we translate them into separate
48304822
// arguments.
48314823
// FIXME: We could do much better if we could guarantee that the

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,23 @@ Out2<double>::AInner t(1.0);
4848
// CHECK-NEXT: | |-TemplateArgument type 'double'
4949
// CHECK-NEXT: | | `-BuiltinType {{.*}} 'double'
5050
// CHECK-NEXT: | `-ParmVarDecl {{.*}} 'double'
51+
52+
template <typename... T1>
53+
struct Foo {
54+
Foo(T1...);
55+
};
56+
57+
template <typename...T2>
58+
using AFoo = Foo<T2...>;
59+
AFoo a(1, 2);
60+
// CHECK: |-CXXDeductionGuideDecl {{.*}} implicit <deduction guide for AFoo> 'auto (type-parameter-0-0...) -> Foo<type-parameter-0-0...>'
61+
// CHECK-NEXT: | | `-ParmVarDecl {{.*}} 'type-parameter-0-0...' pack
62+
// CHECK-NEXT: | `-CXXDeductionGuideDecl {{.*}} implicit used <deduction guide for AFoo> 'auto (int, int) -> Foo<int, int>' implicit_instantiation
63+
64+
template <typename T>
65+
using BFoo = Foo<T, T>;
66+
BFoo b2(1.0, 2.0);
67+
// CHECK: |-CXXDeductionGuideDecl {{.*}} implicit <deduction guide for BFoo> 'auto (type-parameter-0-0, type-parameter-0-0) -> Foo<type-parameter-0-0, type-parameter-0-0>'
68+
// CHECK-NEXT: | | |-ParmVarDecl {{.*}} 'type-parameter-0-0'
69+
// CHECK-NEXT: | | `-ParmVarDecl {{.*}} 'type-parameter-0-0'
70+
// CHECK-NEXT: | `-CXXDeductionGuideDecl {{.*}} implicit used <deduction guide for BFoo> 'auto (double, double) -> Foo<double, double>' implicit_instantiation

clang/test/SemaCXX/cxx20-ctad-type-alias.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ template <typename... Ts>
173173
using AFoo = Foo<Ts...>;
174174

175175
auto b = AFoo{};
176+
AFoo a(1, 2);
177+
178+
template <typename T>
179+
using BFoo = Foo<T, T>;
180+
BFoo b2(1.0, 2.0);
176181
} // namespace test13
177182

178183
namespace test14 {

0 commit comments

Comments
 (0)