Skip to content

Commit 19c571a

Browse files
zyn0217tru
authored andcommitted
[Clang] Instantiate Typedefs referenced by type alias deduction guides (#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 #111508 (cherry picked from commit 0bc02b9)
1 parent 74b2743 commit 19c571a

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
@@ -69,8 +69,8 @@ class ExtractTypeForDeductionGuide
6969
ExtractTypeForDeductionGuide(
7070
Sema &SemaRef,
7171
llvm::SmallVectorImpl<TypedefNameDecl *> &MaterializedTypedefs,
72-
ClassTemplateDecl *NestedPattern,
73-
const MultiLevelTemplateArgumentList *OuterInstantiationArgs)
72+
ClassTemplateDecl *NestedPattern = nullptr,
73+
const MultiLevelTemplateArgumentList *OuterInstantiationArgs = nullptr)
7474
: Base(SemaRef), MaterializedTypedefs(MaterializedTypedefs),
7575
NestedPattern(NestedPattern),
7676
OuterInstantiationArgs(OuterInstantiationArgs) {
@@ -1263,10 +1263,25 @@ FunctionTemplateDecl *DeclareAggregateDeductionGuideForTypeAlias(
12631263
getRHSTemplateDeclAndArgs(SemaRef, AliasTemplate).first;
12641264
if (!RHSTemplate)
12651265
return nullptr;
1266+
1267+
llvm::SmallVector<TypedefNameDecl *> TypedefDecls;
1268+
llvm::SmallVector<QualType> NewParamTypes;
1269+
ExtractTypeForDeductionGuide TypeAliasTransformer(SemaRef, TypedefDecls);
1270+
for (QualType P : ParamTypes) {
1271+
QualType Type = TypeAliasTransformer.TransformType(P);
1272+
if (Type.isNull())
1273+
return nullptr;
1274+
NewParamTypes.push_back(Type);
1275+
}
1276+
12661277
auto *RHSDeductionGuide = SemaRef.DeclareAggregateDeductionGuideFromInitList(
1267-
RHSTemplate, ParamTypes, Loc);
1278+
RHSTemplate, NewParamTypes, Loc);
12681279
if (!RHSDeductionGuide)
12691280
return nullptr;
1281+
1282+
for (TypedefNameDecl *TD : TypedefDecls)
1283+
TD->setDeclContext(RHSDeductionGuide->getTemplatedDecl());
1284+
12701285
return BuildDeductionGuideForTypeAlias(SemaRef, AliasTemplate,
12711286
RHSDeductionGuide, Loc);
12721287
}

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)