Skip to content

Commit 14dab87

Browse files
committed
[ConstraintSystem] Discount effects while filtering call overload sets
Before these changes filtering would not favor some members because their function types have `escaping` bit set when argument function type never has it. Double/CGFloat implicit conversion is one of such cases where `CGFloat.init` overloads have `escaping` bit, which leads to solver checking more overloads then it should. Note that the difference in effects such as `async` is going to be handled by scoring and ranking after partial solution is produced, so overloads that differ only in `async` or `throws` are still going to be solved for as part of "favored" set.
1 parent 5c05da0 commit 14dab87

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11029,9 +11029,19 @@ bool ConstraintSystem::simplifyAppliedOverloadsImpl(
1102911029
return true;
1103011030
}
1103111031

11032-
// If types lined up exactly, let's favor this overload choice.
11033-
if (Type(argFnType)->isEqual(choiceType))
11034-
constraint->setFavored();
11032+
// If types of arguments/parameters and result lined up exactly,
11033+
// let's favor this overload choice.
11034+
//
11035+
// Note this check ignores `ExtInfo` on purpose and only compares
11036+
// types, if there are overloads that differ only in effects then
11037+
// all of them are going to be considered and filtered as part of
11038+
// "favored" group after forming a valid partial solution.
11039+
if (auto *choiceFnType = choiceType->getAs<FunctionType>()) {
11040+
if (FunctionType::equalParams(argFnType->getParams(),
11041+
choiceFnType->getParams()) &&
11042+
argFnType->getResult()->isEqual(choiceFnType->getResult()))
11043+
constraint->setFavored();
11044+
}
1103511045

1103611046
// Account for any optional unwrapping/binding
1103711047
for (unsigned i : range(numOptionalUnwraps)) {

test/Constraints/overload_filtering_objc.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,24 @@ func testOptional(obj: P) {
2222
// CHECK: [disabled] $T2 bound to decl overload_filtering_objc.(file).P.opt(double:)
2323
_ = obj.opt?(1)
2424
}
25+
26+
27+
func test_double_cgfloat_conversion_filtering(d: Double, cgf: CGFloat) {
28+
// CHECK: [favored] $T{{.*}} bound to decl CoreGraphics.(file).CGFloat.init(_:)@{{.*}} : (CGFloat.Type) -> (Double) -> CGFloat {{.*}} -> implicit conversion [Double-to-CGFloat] -> apply function -> constructor member
29+
let _: CGFloat = d
30+
31+
// CHECK: [favored] $T{{.*}} bound to decl CoreGraphics.(file).Double extension.init(_:)@{{.*}} : (Double.Type) -> (CGFloat) -> Double {{.*}} -> implicit conversion [CGFloat-to-Double] -> apply function -> constructor member
32+
let _: Double = cgf
33+
34+
func test_optional_cgf(_: CGFloat??) {
35+
}
36+
37+
func test_optional_double(_: Double??) {
38+
}
39+
40+
// CHECK: [favored] $T{{.*}} bound to decl CoreGraphics.(file).CGFloat.init(_:)@{{.*}} : (CGFloat.Type) -> (Double) -> CGFloat {{.*}} -> implicit conversion [Double-to-CGFloat] -> apply function -> constructor member
41+
test_optional_cgf(d)
42+
43+
// CHECK: [favored] $T{{.*}} bound to decl CoreGraphics.(file).Double extension.init(_:)@{{.*}} : (Double.Type) -> (CGFloat) -> Double {{.*}} -> implicit conversion [CGFloat-to-Double] -> apply function -> constructor member
44+
test_optional_double(cgf)
45+
}

0 commit comments

Comments
 (0)