Skip to content

Commit 1515acd

Browse files
zyn0217DanielCChen
authored andcommitted
[Clang] Instantiate Typedefs referenced by type alias deduction guides (llvm#111804)
TypedefNameDecl referenced by a synthesized CTAD guide for type aliases was not transformed previously, resulting in a substitution failure in BuildDeductionGuideForTypeAlias() when substituting into the right-hand-side deduction guide. This patch fixes it in the way we have been doing since https://reviews.llvm.org/D80743. We transform all the function parameters, parenting referenced TypedefNameDecls with the CXXDeductionGuideDecl. Then we instantiate these declarations in FindInstantiatedDecl() as we build up the eventual deduction guide, using the mechanism introduced in D80743 Fixes llvm#111508
1 parent 2df7aa0 commit 1515acd

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

clang/lib/Sema/SemaTemplateDeductionGuide.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class ExtractTypeForDeductionGuide
7070
ExtractTypeForDeductionGuide(
7171
Sema &SemaRef,
7272
llvm::SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs,
73-
ClassTemplateDecl *NestedPattern,
74-
const MultiLevelTemplateArgumentList *OuterInstantiationArgs)
73+
ClassTemplateDecl *NestedPattern = nullptr,
74+
const MultiLevelTemplateArgumentList *OuterInstantiationArgs = nullptr)
7575
: Base(SemaRef), MaterializedTypedefs(MaterializedTypedefs),
7676
NestedPattern(NestedPattern),
7777
OuterInstantiationArgs(OuterInstantiationArgs) {
@@ -1228,10 +1228,25 @@ FunctionTemplateDecl *DeclareAggregateDeductionGuideForTypeAlias(
12281228
getRHSTemplateDeclAndArgs(SemaRef, AliasTemplate).first;
12291229
if (!RHSTemplate)
12301230
return nullptr;
1231+
1232+
llvm::SmallVector<TypedefNameDecl *> TypedefDecls;
1233+
llvm::SmallVector<QualType> NewParamTypes;
1234+
ExtractTypeForDeductionGuide TypeAliasTransformer(SemaRef, TypedefDecls);
1235+
for (QualType P : ParamTypes) {
1236+
QualType Type = TypeAliasTransformer.TransformType(P);
1237+
if (Type.isNull())
1238+
return nullptr;
1239+
NewParamTypes.push_back(Type);
1240+
}
1241+
12311242
auto *RHSDeductionGuide = SemaRef.DeclareAggregateDeductionGuideFromInitList(
1232-
RHSTemplate, ParamTypes, Loc);
1243+
RHSTemplate, NewParamTypes, Loc);
12331244
if (!RHSDeductionGuide)
12341245
return nullptr;
1246+
1247+
for (TypedefNameDecl *TD : TypedefDecls)
1248+
TD->setDeclContext(RHSDeductionGuide->getTemplatedDecl());
1249+
12351250
return BuildDeductionGuideForTypeAlias(SemaRef, AliasTemplate,
12361251
RHSDeductionGuide, Loc);
12371252
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,3 +481,16 @@ struct Out {
481481
Out<float>::B out(100); // deduced to Out<float>::A<float>;
482482
static_assert(__is_same(decltype(out), Out<float>::A<float>));
483483
}
484+
485+
namespace GH111508 {
486+
487+
template <typename V> struct S {
488+
using T = V;
489+
T Data;
490+
};
491+
492+
template <typename V> using Alias = S<V>;
493+
494+
Alias A(42);
495+
496+
} // namespace GH111508

0 commit comments

Comments
 (0)