Skip to content

Commit 2df9bb5

Browse files
authored
Merge pull request swiftlang#30440 from LucianoPAlmeida/SR-11540-ambiguos-closure-inference
[SR-11540] Fix ambiguos overload apply to argument for contextual closure
2 parents ba78dbb + a25f996 commit 2df9bb5

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
16851685
if (isSingleTupleParam(ctx, func2Params) &&
16861686
canImplodeParams(func1Params)) {
16871687
implodeParams(func1Params);
1688+
increaseScore(SK_FunctionConversion);
16881689
} else if (!ctx.isSwiftVersionAtLeast(5) &&
16891690
isSingleTupleParam(ctx, func1Params) &&
16901691
canImplodeParams(func2Params)) {
@@ -1697,6 +1698,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
16971698
isa<OverloadedDeclRefExpr>(simplified) ||
16981699
isa<UnresolvedDeclRefExpr>(simplified))) {
16991700
implodeParams(func2Params);
1701+
increaseScore(SK_FunctionConversion);
17001702
}
17011703
}
17021704
} else if (last->getKind() == ConstraintLocator::PatternMatch &&
@@ -1724,9 +1726,11 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
17241726
if (isSingleTupleParam(ctx, func1Params) &&
17251727
canImplodeParams(func2Params)) {
17261728
implodeParams(func2Params);
1729+
increaseScore(SK_FunctionConversion);
17271730
} else if (isSingleTupleParam(ctx, func2Params) &&
17281731
canImplodeParams(func1Params)) {
17291732
implodeParams(func1Params);
1733+
increaseScore(SK_FunctionConversion);
17301734
}
17311735
}
17321736
}

test/Constraints/diag_ambiguities.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ func +++(d: Double, i: Int) {} // expected-note{{found this candidate}}
1818
1 +++ 2 // expected-error{{ambiguous use of operator '+++'}}
1919

2020
class C {
21-
init(_ action: (Int) -> ()) {} // expected-note{{found this candidate}}
22-
init(_ action: (Int, Int) -> ()) {} // expected-note{{found this candidate}}
21+
init(_ action: (Int) -> ()) {}
22+
init(_ action: (Int, Int) -> ()) {}
2323
}
2424

2525
func g(_ x: Int) -> () {} // expected-note{{found this candidate}}
2626
func g(_ x: Int, _ y: Int) -> () {} // expected-note{{found this candidate}}
2727
C(g) // expected-error{{ambiguous use of 'g'}}
2828

2929
func h<T>(_ x: T) -> () {}
30-
C(h) // expected-error{{ambiguous use of 'init(_:)'}}
30+
_ = C(h) // OK - init(_: (Int) -> ())
3131

3232
func rdar29691909_callee(_ o: AnyObject?) -> Any? { return o } // expected-note {{found this candidate}}
3333
func rdar29691909_callee(_ o: AnyObject) -> Any { return o } // expected-note {{found this candidate}}

test/expr/closure/inference.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,16 @@ var nestedClosuresWithBrokenInference = { f: Int in {} }
3838
// expected-error@-2 {{consecutive statements on a line must be separated by ';'}} {{44-44=;}}
3939
// expected-error@-3 {{expected expression}}
4040
// expected-error@-4 {{use of unresolved identifier 'f'}}
41+
42+
// SR-11540
43+
44+
func SR11540<R>(action: () -> R) -> Void {}
45+
46+
func SR11540<T, R>(action: (T) -> R) -> Void {}
47+
48+
func SR11540_1<T, R>(action: (T) -> R) -> Void {}
49+
50+
SR11540(action: { return }) // Ok SR11540<R>(action: () -> R) was the selected overload.
51+
52+
// In case that's the only possible overload, it's acceptable
53+
SR11540_1(action: { return }) // OK

0 commit comments

Comments
 (0)