Skip to content

Commit 43c56b3

Browse files
authored
Merge pull request swiftlang#14457 from rudkx/fix-sr6837-5.0
[5.0] Fix SR-6837 - allow function conversion for -swift-version 4 *only*
2 parents 443bd22 + 4b13865 commit 43c56b3

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,16 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
11731173
if (auto *paren2 = dyn_cast<ParenType>(func2Input.getPointer())) {
11741174
if (!func1Input->hasParenSugar())
11751175
func2Input = paren2->getUnderlyingType();
1176+
} else if (getASTContext().isSwiftVersionAtLeast(4)
1177+
&& !getASTContext().isSwiftVersionAtLeast(5)
1178+
&& !func2Input->hasParenSugar()) {
1179+
auto *simplified = locator.trySimplifyToExpr();
1180+
// We somehow let tuple unsplatting function conversions
1181+
// through in some cases in Swift 4, so let's let that
1182+
// continue to work, but only for Swift 4.
1183+
if (simplified && isa<DeclRefExpr>(simplified))
1184+
if (auto *paren1 = dyn_cast<ParenType>(func1Input.getPointer()))
1185+
func1Input = paren1->getUnderlyingType();
11761186
}
11771187
}
11781188
}

test/Compatibility/tuple_arguments.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,3 +1507,12 @@ public extension Optional {
15071507
return value.apply(self)
15081508
}
15091509
}
1510+
1511+
// https://bugs.swift.org/browse/SR-6837
1512+
do {
1513+
func takeFn(fn: (_ i: Int, _ j: Int?) -> ()) {}
1514+
func takePair(_ pair: (Int, Int?)) {}
1515+
takeFn(fn: takePair)
1516+
takeFn(fn: { (pair: (Int, Int?)) in } )
1517+
takeFn { (pair: (Int, Int?)) in }
1518+
}

test/Constraints/tuple_arguments.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,3 +1642,15 @@ public extension Optional {
16421642
return value.apply(self)
16431643
}
16441644
}
1645+
1646+
1647+
// https://bugs.swift.org/browse/SR-6837
1648+
do {
1649+
func takeFn(fn: (_ i: Int, _ j: Int?) -> ()) {}
1650+
func takePair(_ pair: (Int, Int?)) {}
1651+
takeFn(fn: takePair) // Allow for -swift-version 4, but not later
1652+
takeFn(fn: { (pair: (Int, Int?)) in } ) // Disallow for -swift-version 4 and later
1653+
// expected-error@-1 {{contextual closure type '(Int, Int?) -> ()' expects 2 arguments, but 1 was used in closure body}}
1654+
takeFn { (pair: (Int, Int?)) in } // Disallow for -swift-version 4 and later
1655+
// expected-error@-1 {{contextual closure type '(Int, Int?) -> ()' expects 2 arguments, but 1 was used in closure body}}
1656+
}

0 commit comments

Comments
 (0)