Skip to content

Commit 6caf1cc

Browse files
committed
[CSOptimizer] Fix Double<->CGFloat implicit conversion support when arguments are literals
Passing Double to CGFloat as well as vice versa constitutes an exact match.
1 parent e30587b commit 6caf1cc

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/Sema/CSOptimizer.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -454,19 +454,29 @@ static Constraint *determineBestChoicesInContext(
454454
scoreCandidateMatch = [&](GenericSignature genericSig,
455455
Type candidateType, Type paramType,
456456
MatchOptions options) -> double {
457-
auto areEqual = [](Type a, Type b) {
457+
auto areEqual = [&options](Type a, Type b) {
458+
// Double<->CGFloat implicit conversion support for literals
459+
// only since in this case the conversion might not result in
460+
// score penalty.
461+
if (options.contains(MatchFlag::Literal) &&
462+
((a->isDouble() && b->isCGFloat()) ||
463+
(a->isCGFloat() && b->isDouble())))
464+
return true;
465+
458466
return a->getDesugaredType()->isEqual(b->getDesugaredType());
459467
};
460468

461469
if (options.contains(MatchFlag::ExactOnly))
462470
return areEqual(candidateType, paramType) ? 1 : 0;
463471

464472
// Exact match between candidate and parameter types.
465-
if (areEqual(candidateType, paramType))
473+
if (areEqual(candidateType, paramType)) {
466474
return options.contains(MatchFlag::Literal) ? 0.3 : 1;
475+
}
467476

468-
if (options.contains(MatchFlag::Literal))
477+
if (options.contains(MatchFlag::Literal)) {
469478
return 0;
479+
}
470480

471481
// Check whether match would require optional injection.
472482
{

test/Constraints/implicit_double_cgfloat_conversion.swift

+5
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,8 @@ do {
361361
return v // Ok
362362
}
363363
}
364+
365+
func test_cgfloat_operator_is_attempted_with_literal_arguments(v: CGFloat?) {
366+
let ratio = v ?? (2.0 / 16.0)
367+
let _: CGFloat = ratio // Ok
368+
}

0 commit comments

Comments
 (0)