Skip to content

[Clang] Do not assume a perfect match is a better match than a non-template non-perfect match #149504

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
Jul 18, 2025
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
2 changes: 0 additions & 2 deletions clang/include/clang/Sema/Overload.h
Original file line number Diff line number Diff line change
Expand Up @@ -1491,8 +1491,6 @@ class Sema;
OverloadingResult
BestViableFunctionImpl(Sema &S, SourceLocation Loc,
OverloadCandidateSet::iterator &Best);
void PerfectViableFunction(Sema &S, SourceLocation Loc,
OverloadCandidateSet::iterator &Best);
};

bool isBetterOverloadCandidate(Sema &S, const OverloadCandidate &Cand1,
Expand Down
49 changes: 6 additions & 43 deletions clang/lib/Sema/SemaOverload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11354,55 +11354,18 @@ OverloadingResult OverloadCandidateSet::BestViableFunction(Sema &S,
DeferredCandidatesCount != 0 && !ResolutionByPerfectCandidateIsDisabled;

if (TwoPhaseResolution) {

PerfectViableFunction(S, Loc, Best);
if (Best != end())
return ResultForBestCandidate(Best);
OverloadingResult Res = BestViableFunctionImpl(S, Loc, Best);
if (Best != end() && Best->isPerfectMatch(S.Context)) {
if (!(HasDeferredTemplateConstructors &&
isa_and_nonnull<CXXConversionDecl>(Best->Function)))
return Res;
}
}

InjectNonDeducedTemplateCandidates(S);
return BestViableFunctionImpl(S, Loc, Best);
}

void OverloadCandidateSet::PerfectViableFunction(
Sema &S, SourceLocation Loc, OverloadCandidateSet::iterator &Best) {

Best = end();
for (auto It = Candidates.begin(); It != Candidates.end(); ++It) {

if (!It->isPerfectMatch(S.getASTContext()))
continue;

// We found a suitable conversion function
// but if there is a template constructor in the target class
// we might prefer that instead.
if (HasDeferredTemplateConstructors &&
isa_and_nonnull<CXXConversionDecl>(It->Function)) {
Best = end();
break;
}

if (Best == end()) {
Best = It;
continue;
}
if (Best->Function && It->Function) {
FunctionDecl *D =
S.getMoreConstrainedFunction(Best->Function, It->Function);
if (D == nullptr) {
Best = end();
break;
}
if (D == It->Function)
Best = It;
continue;
}
// ambiguous
Best = end();
break;
}
}

OverloadingResult OverloadCandidateSet::BestViableFunctionImpl(
Sema &S, SourceLocation Loc, OverloadCandidateSet::iterator &Best) {

Expand Down
28 changes: 28 additions & 0 deletions clang/test/SemaCXX/overload-resolution-deferred-templates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,31 @@ void f() {
}

#endif

namespace GH147374 {

struct String {};
template <typename T> void operator+(T, String &&) = delete;

struct Bar {
void operator+(String) const; // expected-note {{candidate function}}
friend void operator+(Bar, String) {}; // expected-note {{candidate function}}
};

struct Baz {
void operator+(String); // expected-note {{candidate function}}
friend void operator+(Baz, String) {}; // expected-note {{candidate function}}
};

void test() {
Bar a;
String b;
a + b;
//expected-error@-1 {{use of overloaded operator '+' is ambiguous (with operand types 'Bar' and 'String')}}

Baz z;
z + b;
//expected-error@-1 {{use of overloaded operator '+' is ambiguous (with operand types 'Baz' and 'String')}}
}

}
Loading