Skip to content

Commit 7c92f7d

Browse files
[CSSimplify] Add autoclosure check for contravariant match and extra test case
1 parent ef86e9e commit 7c92f7d

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,9 +2230,15 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
22302230
//
22312231
// let _ = autoclosure as (() -> (Int)) -> () // non-autoclosure preferred
22322232
//
2233-
if (func1Param.isAutoClosure() &&
2234-
(!func2Param.isAutoClosure() &&
2235-
func2Param.getPlainType()->is<FunctionType>())) {
2233+
auto isAutoClosureFunctionMatch = [](AnyFunctionType::Param &param1,
2234+
AnyFunctionType::Param &param2) {
2235+
return param1.isAutoClosure() &&
2236+
(!param2.isAutoClosure() &&
2237+
param2.getPlainType()->is<FunctionType>());
2238+
};
2239+
2240+
if (isAutoClosureFunctionMatch(func1Param, func2Param) ||
2241+
isAutoClosureFunctionMatch(func2Param, func1Param)) {
22362242
increaseScore(SK_FunctionToAutoClosureConversion);
22372243
}
22382244

test/Constraints/sr2705.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ func autoclosure(f: Int) { }
2626

2727
autoclosure(f: { 0 }) // OK
2828
let _ = autoclosure as (() -> (Int)) -> () // OK
29+
30+
func test(_: (@autoclosure () -> Int) -> Void) {}
31+
func test(_: (() -> Int) -> Void) {}
32+
33+
func fn(_: () -> Int) {}
34+
35+
test(fn) // OK

0 commit comments

Comments
 (0)