Skip to content

Commit a455db6

Browse files
authored
Merge pull request swiftlang#14477 from rudkx/fix-sr6796
Restore a very narrow function argument conversion for -swift-version 4.
2 parents b140b8c + 5a02541 commit a455db6

File tree

5 files changed

+1745
-6
lines changed

5 files changed

+1745
-6
lines changed

lib/Sema/CSApply.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6156,7 +6156,10 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
61566156
// func bar() -> ((()) -> Void)? { return nil }
61576157
// foo(bar) // This expression should compile in Swift 3 mode
61586158
// ```
6159-
if (tc.getLangOpts().isSwiftVersion3()) {
6159+
//
6160+
// See also: https://bugs.swift.org/browse/SR-6796
6161+
if (cs.getASTContext().isSwiftVersionAtLeast(3) &&
6162+
!cs.getASTContext().isSwiftVersionAtLeast(5)) {
61606163
auto obj1 = fromType->getOptionalObjectType();
61616164
auto obj2 = toType->getOptionalObjectType();
61626165

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 renamed to test/Compatibility/tuple_arguments_3.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// RUN: %target-typecheck-verify-swift -swift-version 3
22

33
// Tests for tuple argument behavior in Swift 3, which was broken.
4-
// The Swift 4 test is in test/Constraints/tuple_arguments.swift.
4+
// The Swift 4 test is in test/Compatibility/tuple_arguments_4.swift.
5+
// The test for the most recent version is in test/Constraints/tuple_arguments.swift.
56

67
// Key:
78
// - "Crashes in actual Swift 3" -- snippets which crashed in Swift 3.0.1.
@@ -1516,3 +1517,20 @@ do {
15161517
takeFn(fn: { (pair: (Int, Int?)) in } )
15171518
takeFn { (pair: (Int, Int?)) in }
15181519
}
1520+
1521+
// https://bugs.swift.org/browse/SR-6796
1522+
do {
1523+
func f(a: (() -> Void)? = nil) {}
1524+
func log<T>() -> ((T) -> Void)? { return nil }
1525+
1526+
f(a: log() as ((()) -> Void)?) // Allow ((()) -> Void)? to be passed in place of (() -> Void)?
1527+
1528+
func logNoOptional<T>() -> (T) -> Void { }
1529+
f(a: logNoOptional() as ((()) -> Void)) // Also allow the optional-injected form.
1530+
1531+
func g() {}
1532+
g(())
1533+
1534+
func h(_: ()) {}
1535+
h()
1536+
}

0 commit comments

Comments
 (0)