[CSOptimizer] Average score should reflect number of defaulted parameters

This commit is contained in:
Pavel Yaskevich
2024-09-11 14:57:44 -07:00
parent 66981364fe
commit 23589add74
2 changed files with 31 additions and 2 deletions

View File

@@ -441,6 +441,7 @@ static Constraint *determineBestChoicesInContext(
}
double score = 0.0;
unsigned numDefaulted = 0;
for (unsigned paramIdx = 0, n = overloadType->getNumParams();
paramIdx != n; ++paramIdx) {
const auto &param = overloadType->getParams()[paramIdx];
@@ -448,7 +449,8 @@ static Constraint *determineBestChoicesInContext(
auto argIndices = matchings->parameterBindings[paramIdx];
switch (argIndices.size()) {
case 0:
// Current parameter is defaulted.
// Current parameter is defaulted, mark and continue.
++numDefaulted;
continue;
case 1:
@@ -542,9 +544,14 @@ static Constraint *determineBestChoicesInContext(
score += bestCandidateScore;
}
// An overload whether all of the parameters are defaulted
// that's called without arguments.
if (numDefaulted == overloadType->getNumParams())
return;
// Average the score to avoid disfavoring disjunctions with fewer
// parameters.
score /= overloadType->getNumParams();
score /= (overloadType->getNumParams() - numDefaulted);
// If one of the result types matches exactly, that's a good
// indication that overload choice should be favored.

View File

@@ -39,3 +39,25 @@ do {
}
}
// error: initializer for conditional binding must have Optional type, not 'S'
do {
struct S {
let n: Int
}
func f(_: String, _ p: Bool = false) -> S? {
nil
}
func f(_ x: String) -> S {
fatalError()
}
func g(_ x: String) -> Int? {
guard let y = f(x) else {
return nil
}
return y.n
}
}