Skip to content

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

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 8 commits into from
Oct 4, 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 @@ -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.

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(),
DGuide->getDeductionCandidateKind());
Function->setAccess(D->getAccess());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,38 @@ int main() {


}

namespace deduceTemplatedConstructor {
template <typename X, typename Y> struct IsSame {
static constexpr bool value = false;
};

template <typename Z> struct IsSame<Z, Z> {
static constexpr bool value = true;
};
template <class T> struct A {
using value_type = T;
A(value_type);
A(const A&);
A(T, T, int);
template<class U>
A(int, T, U);
};

A x(1, 2, 3); // no-error
static_assert(IsSame<decltype(x),A<int>>::value);

template <class T>
A(T) -> A<T>;

A a(42);
static_assert(IsSame<decltype(a),A<int>>::value);
A b = a;
static_assert(IsSame<decltype(b),A<int>>::value);

template <class T>
A(A<T>) -> A<A<T>>;

A b2 = a;
static_assert(IsSame<decltype(b2),A<A<int>>>::value);
}