Skip to content
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

[clang] Choose non-templated ctor as deduction guide unambiguously #66487

Merged
merged 8 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -433,6 +433,11 @@ Bug Fixes to C++ Support
we now produce a diagnostic. Fixes:
(`#65522 <https://github.com/llvm/llvm-project/issues/65522>`_)

- Fixed a bug where clang incorrectly considered implicitly generated deduction
guides from a non-templated constructor and a templated constructor as ambiguous,
rather than prefer the non-templated constructor as specified in
[standard.group]p3.

HoBoIs marked this conversation as resolved.
Show resolved Hide resolved
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
- Fixed an import failure of recursive friend class template.
Expand Down
17 changes: 16 additions & 1 deletion clang/lib/Sema/SemaOverload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10440,6 +10440,21 @@ bool clang::isBetterOverloadCandidate(
// -- F1 is the copy deduction candidate(16.3.1.8) and F2 is not
if (Guide1->getDeductionCandidateKind() == DeductionCandidate::Copy)
return true;
if (Guide2->getDeductionCandidateKind() == DeductionCandidate::Copy)
return false;

// --F1 is generated from a non-template constructor and F2 is generated
// from a constructor template
const auto *Constructor1 = Guide1->getCorrespondingConstructor();
const auto *Constructor2 = Guide2->getCorrespondingConstructor();
if (Constructor1 && Constructor2) {
bool isC1Templated = Constructor1->getTemplatedKind() !=
FunctionDecl::TemplatedKind::TK_NonTemplate;
bool isC2Templated = Constructor2->getTemplatedKind() !=
FunctionDecl::TemplatedKind::TK_NonTemplate;
if (isC1Templated != isC2Templated)
return isC2Templated;
}
}
}

Expand Down Expand Up @@ -10483,7 +10498,7 @@ bool clang::isBetterOverloadCandidate(
if (AS1 != AS2) {
if (Qualifiers::isAddressSpaceSupersetOf(AS2, AS1))
return true;
if (Qualifiers::isAddressSpaceSupersetOf(AS2, AS1))
if (Qualifiers::isAddressSpaceSupersetOf(AS1, AS2))
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,7 @@ Decl *TemplateDeclInstantiator::VisitFunctionDecl(
Function = CXXDeductionGuideDecl::Create(
SemaRef.Context, DC, D->getInnerLocStart(),
InstantiatedExplicitSpecifier, NameInfo, T, TInfo,
D->getSourceRange().getEnd(), /*Ctor=*/nullptr,
D->getSourceRange().getEnd(), DGuide->getCorrespondingConstructor(),
whisperity marked this conversation as resolved.
Show resolved Hide resolved
DGuide->getDeductionCandidateKind());
Function->setAccess(D->getAccess());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,13 @@ int main() {


}

namespace deduceTemplatedConstructor{
HoBoIs marked this conversation as resolved.
Show resolved Hide resolved
whisperity marked this conversation as resolved.
Show resolved Hide resolved
template <class T> struct A {
A(T, T, int);
template<class U>
A(int, T, U);
};

A x(1, 2, 3); // no-error
}