mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[CSOptimizer] Average score should reflect number of defaulted parameters
This commit is contained in:
@@ -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 ¶m = 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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user