Skip to content

[clang][Sema] Use original template pattern when declaring implicit deduction guides for nested template classes #68379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,11 @@ Bug Fixes to C++ Support
rather than prefer the non-templated constructor as specified in
[standard.group]p3.

- Fix a bug where implicit deduction guides are not correctly generated for nested template
classes. Fixes:
(`#46200 <https://github.com/llvm/llvm-project/issues/46200>`_)
(`#57812 <https://github.com/llvm/llvm-project/issues/57812>`_)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
- Fixed an import failure of recursive friend class template.
Expand Down
22 changes: 21 additions & 1 deletion clang/lib/Sema/SemaTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,7 @@ struct ConvertConstructorToDeductionGuideTransform {

Sema &SemaRef;
ClassTemplateDecl *Template;
ClassTemplateDecl *NestedPattern = nullptr;

DeclContext *DC = Template->getDeclContext();
CXXRecordDecl *Primary = Template->getTemplatedDecl();
Expand Down Expand Up @@ -2327,6 +2328,8 @@ struct ConvertConstructorToDeductionGuideTransform {
if (FTD) {
Args.addOuterTemplateArguments(SubstArgs);
Args.addOuterRetainedLevel();
if (NestedPattern)
Args.addOuterRetainedLevels(NestedPattern->getTemplateDepth());
}

FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc()
Expand Down Expand Up @@ -2438,10 +2441,17 @@ struct ConvertConstructorToDeductionGuideTransform {
SmallVector<QualType, 4> ParamTypes;
const FunctionProtoType *T = TL.getTypePtr();

MultiLevelTemplateArgumentList OuterInstantiationArgs;
if (NestedPattern)
OuterInstantiationArgs = SemaRef.getTemplateInstantiationArgs(Template);

// -- The types of the function parameters are those of the constructor.
for (auto *OldParam : TL.getParams()) {
ParmVarDecl *NewParam =
transformFunctionTypeParam(OldParam, Args, MaterializedTypedefs);
if (NestedPattern && NewParam)
NewParam = transformFunctionTypeParam(NewParam, OuterInstantiationArgs,
MaterializedTypedefs);
if (!NewParam)
return QualType();
ParamTypes.push_back(NewParam->getType());
Expand Down Expand Up @@ -2647,13 +2657,23 @@ void Sema::DeclareImplicitDeductionGuides(TemplateDecl *Template,
if (BuildingDeductionGuides.isInvalid())
return;

// If the template is nested, then we need to use the original
// pattern to iterate over the constructors.
ClassTemplateDecl *Pattern = Transform.Template;
while (Pattern->getInstantiatedFromMemberTemplate()) {
if (Pattern->isMemberSpecialization())
break;
Pattern = Pattern->getInstantiatedFromMemberTemplate();
Transform.NestedPattern = Pattern;
}

// Convert declared constructors into deduction guide templates.
// FIXME: Skip constructors for which deduction must necessarily fail (those
// for which some class template parameter without a default argument never
// appears in a deduced context).
llvm::SmallPtrSet<NamedDecl *, 8> ProcessedCtors;
bool AddedAny = false;
for (NamedDecl *D : LookupConstructors(Transform.Primary)) {
for (NamedDecl *D : LookupConstructors(Pattern->getTemplatedDecl())) {
D = D->getUnderlyingDecl();
if (D->isInvalidDecl() || D->isImplicit())
continue;
Expand Down
12 changes: 12 additions & 0 deletions clang/test/SemaTemplate/nested-implicit-deduction-guides.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %clang_cc1 -std=c++17 -verify %s
// expected-no-diagnostics

template<class T> struct S {
template<class U> struct N {
N(T) {}
N(T, U) {}
template<class V> N(V, U) {}
};
};

S<int>::N x{"a", 1};