Skip to content

[CSOptimizer] Decrease an operator argument score only if requires op… #83414

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
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
35 changes: 14 additions & 21 deletions lib/Sema/CSOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -970,8 +970,6 @@ static void determineBestChoicesInContext(

bool hasArgumentCandidates = false;
bool isOperator = isOperatorDisjunction(disjunction);
bool isNilCoalescingOperator =
isOperator && isOperatorNamed(disjunction, "??");

for (unsigned i = 0, n = argFuncType->getNumParams(); i != n; ++i) {
const auto &param = argFuncType->getParams()[i];
Expand Down Expand Up @@ -1341,17 +1339,26 @@ static void determineBestChoicesInContext(
paramType = paramType->lookThroughAllOptionalTypes(paramOptionals);

if (!candidateOptionals.empty() || !paramOptionals.empty()) {
auto requiresOptionalInjection = [&]() {
return paramOptionals.size() > candidateOptionals.size();
};

// Can match i.e. Int? to Int or T to Int?
if ((paramOptionals.empty() &&
paramType->is<GenericTypeParamType>()) ||
paramOptionals.size() >= candidateOptionals.size()) {
auto score = scoreCandidateMatch(genericSig, choice, candidateType,
paramType, options);
// Injection lowers the score slightly to comply with
// old behavior where exact matches on operator parameter
// types were always preferred.
return score > 0 && choice->isOperator() ? score.value() - 0.1
: score;

if (score > 0) {
// Injection lowers the score slightly to comply with
// old behavior where exact matches on operator parameter
// types were always preferred.
if (choice->isOperator() && requiresOptionalInjection())
return score.value() - 0.1;
}

return score;
}

// Optionality mismatch.
Expand Down Expand Up @@ -1605,20 +1612,6 @@ static void determineBestChoicesInContext(
double bestCandidateScore = 0;
llvm::BitVector mismatches(argumentCandidates[argIdx].size());

// `??` is overloaded on optionality of the second parameter,
// prevent ranking the argument candidates for this parameter
// if there are candidates that come from failable initializer
// overloads because non-optional candidates are always going
// to be better and that can skew the selection.
if (isNilCoalescingOperator && argIdx == 1) {
if (llvm::any_of(argumentCandidates[argIdx],
[](const auto &candidate) {
return candidate.fromInitializerCall &&
candidate.type->getOptionalObjectType();
}))
continue;
}

for (unsigned candidateIdx :
indices(argumentCandidates[argIdx])) {
// If one of the candidates matched exactly there is no reason
Expand Down