[CSOptimizer] Fix Double<->CGFloat implicit conversion support when arguments are literals

Passing Double to CGFloat as well as vice versa constitutes an exact match.
This commit is contained in:
Pavel Yaskevich
2024-10-07 19:57:50 +09:00
parent e30587bda4
commit 6caf1ccbb2
2 changed files with 18 additions and 3 deletions

View File

@@ -454,7 +454,15 @@ static Constraint *determineBestChoicesInContext(
scoreCandidateMatch = [&](GenericSignature genericSig,
Type candidateType, Type paramType,
MatchOptions options) -> double {
auto areEqual = [](Type a, Type b) {
auto areEqual = [&options](Type a, Type b) {
// Double<->CGFloat implicit conversion support for literals
// only since in this case the conversion might not result in
// score penalty.
if (options.contains(MatchFlag::Literal) &&
((a->isDouble() && b->isCGFloat()) ||
(a->isCGFloat() && b->isDouble())))
return true;
return a->getDesugaredType()->isEqual(b->getDesugaredType());
};
@@ -462,11 +470,13 @@ static Constraint *determineBestChoicesInContext(
return areEqual(candidateType, paramType) ? 1 : 0;
// Exact match between candidate and parameter types.
if (areEqual(candidateType, paramType))
if (areEqual(candidateType, paramType)) {
return options.contains(MatchFlag::Literal) ? 0.3 : 1;
}
if (options.contains(MatchFlag::Literal))
if (options.contains(MatchFlag::Literal)) {
return 0;
}
// Check whether match would require optional injection.
{

View File

@@ -361,3 +361,8 @@ do {
return v // Ok
}
}
func test_cgfloat_operator_is_attempted_with_literal_arguments(v: CGFloat?) {
let ratio = v ?? (2.0 / 16.0)
let _: CGFloat = ratio // Ok
}