Skip to content

Commit 6310aca

Browse files
committed
Restore a very narrow function argument conversion for -swift-version 4.
Allow functions with type `(()) -> T` to be passed in places where we expect `() -> T`, but only for -swift-version 4 (for -swift-version 3 this already works due to other horrible things in CSSimplify.cpp). We need to look at how we can help migrate these cases to -swift-version 5, but in the meantime, but that is something we can consider separately.
1 parent af67204 commit 6310aca

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

lib/Sema/CSApply.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6152,7 +6152,10 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
61526152
// func bar() -> ((()) -> Void)? { return nil }
61536153
// foo(bar) // This expression should compile in Swift 3 mode
61546154
// ```
6155-
if (tc.getLangOpts().isSwiftVersion3()) {
6155+
//
6156+
// See also: https://bugs.swift.org/browse/SR-6796
6157+
if (cs.getASTContext().isSwiftVersionAtLeast(3) &&
6158+
!cs.getASTContext().isSwiftVersionAtLeast(5)) {
61566159
auto obj1 = fromType->getOptionalObjectType();
61576160
auto obj2 = toType->getOptionalObjectType();
61586161

lib/Sema/CSSimplify.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,35 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
11681168
}
11691169
}
11701170

1171+
// https://bugs.swift.org/browse/SR-6796
1172+
// Add a super-narrow hack to allow:
1173+
// (()) -> T to be passed in place of () -> T
1174+
if (getASTContext().isSwiftVersionAtLeast(4) &&
1175+
!getASTContext().isSwiftVersionAtLeast(5)) {
1176+
SmallVector<LocatorPathElt, 4> path;
1177+
locator.getLocatorParts(path);
1178+
1179+
// Find the last path element, skipping GenericArgument elements
1180+
// so that we allow this exception in cases of optional types, and
1181+
// skipping OptionalPayload elements so that we allow this
1182+
// exception in cases of optional injection.
1183+
auto last = std::find_if(
1184+
path.rbegin(), path.rend(), [](LocatorPathElt &elt) -> bool {
1185+
return elt.getKind() != ConstraintLocator::GenericArgument &&
1186+
elt.getKind() != ConstraintLocator::OptionalPayload;
1187+
});
1188+
1189+
if (last != path.rend()) {
1190+
if (last->getKind() == ConstraintLocator::ApplyArgToParam) {
1191+
if (auto *paren1 = dyn_cast<ParenType>(func1Input.getPointer())) {
1192+
auto innerTy = paren1->getUnderlyingType();
1193+
if (func2Input->isVoid() && innerTy->isVoid())
1194+
func1Input = innerTy;
1195+
}
1196+
}
1197+
}
1198+
}
1199+
11711200
// Input types can be contravariant (or equal).
11721201
SmallVector<AnyFunctionType::Param, 4> func1Params;
11731202
SmallVector<AnyFunctionType::Param, 4> func2Params;

test/Compatibility/tuple_arguments.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,3 +1516,14 @@ do {
15161516
takeFn(fn: { (pair: (Int, Int?)) in } )
15171517
takeFn { (pair: (Int, Int?)) in }
15181518
}
1519+
1520+
// https://bugs.swift.org/browse/SR-6796
1521+
do {
1522+
func f(a: (() -> Void)? = nil) {}
1523+
func log<T>() -> ((T) -> Void)? { return nil }
1524+
1525+
f(a: log() as ((()) -> Void)?) // Allow ((()) -> Void)? to be passed in place of (() -> Void)?
1526+
1527+
func logNoOptional<T>() -> (T) -> Void { }
1528+
f(a: logNoOptional() as ((()) -> Void)) // Also allow the optional-injected form.
1529+
}

test/Constraints/tuple_arguments.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ func rdar33239714() {
16261626
do {
16271627
func foo(_: (() -> Void)?) {}
16281628
func bar() -> ((()) -> Void)? { return nil }
1629-
foo(bar()) // expected-error {{cannot convert value of type '((()) -> Void)?' to expected argument type '(() -> Void)?'}}
1629+
foo(bar()) // Allow ((()) -> Void)? to be passed in place of (() -> Void)? for -swift-version 4 but not later.
16301630
}
16311631

16321632
// https://bugs.swift.org/browse/SR-6509
@@ -1643,7 +1643,6 @@ public extension Optional {
16431643
}
16441644
}
16451645

1646-
16471646
// https://bugs.swift.org/browse/SR-6837
16481647
do {
16491648
func takeFn(fn: (_ i: Int, _ j: Int?) -> ()) {}
@@ -1654,3 +1653,14 @@ do {
16541653
takeFn { (pair: (Int, Int?)) in } // Disallow for -swift-version 4 and later
16551654
// expected-error@-1 {{contextual closure type '(Int, Int?) -> ()' expects 2 arguments, but 1 was used in closure body}}
16561655
}
1656+
1657+
// https://bugs.swift.org/browse/SR-6796
1658+
do {
1659+
func f(a: (() -> Void)? = nil) {}
1660+
func log<T>() -> ((T) -> Void)? { return nil }
1661+
1662+
f(a: log() as ((()) -> Void)?) // Allow ((()) -> Void)? to be passed in place of (() -> Void)? for -swift-version 4 but not later.
1663+
1664+
func logNoOptional<T>() -> (T) -> Void { }
1665+
f(a: logNoOptional() as ((()) -> Void)) // Also allow the optional-injected form.
1666+
}

0 commit comments

Comments
 (0)