@@ -970,6 +970,8 @@ static void determineBestChoicesInContext(
970970
971971 bool hasArgumentCandidates = false ;
972972 bool isOperator = isOperatorDisjunction(disjunction);
973+ bool isNilCoalescingOperator =
974+ isOperator && isOperatorNamed(disjunction, " ??" );
973975
974976 for (unsigned i = 0 , n = argFuncType->getNumParams (); i != n; ++i) {
975977 const auto ¶m = argFuncType->getParams ()[i];
@@ -1603,6 +1605,20 @@ static void determineBestChoicesInContext(
16031605 double bestCandidateScore = 0 ;
16041606 llvm::BitVector mismatches (argumentCandidates[argIdx].size ());
16051607
1608+ // `??` is overloaded on optionality of the second parameter,
1609+ // prevent ranking the argument candidates for this parameter
1610+ // if there are candidates that come from failable initializer
1611+ // overloads because non-optional candidates are always going
1612+ // to be better and that can skew the selection.
1613+ if (isNilCoalescingOperator && argIdx == 1 ) {
1614+ if (llvm::any_of (argumentCandidates[argIdx],
1615+ [](const auto &candidate) {
1616+ return candidate.fromInitializerCall &&
1617+ candidate.type ->getOptionalObjectType ();
1618+ }))
1619+ continue ;
1620+ }
1621+
16061622 for (unsigned candidateIdx :
16071623 indices (argumentCandidates[argIdx])) {
16081624 // If one of the candidates matched exactly there is no reason
0 commit comments