Skip to content

Commit c88654e

Browse files
authored
Merge pull request #21361 from slavapestov/fix-tuple-splat-varargs-5.0
SE-0110 tuple splatting should not implode argument list with varargs [5.0]
2 parents 0353bc1 + dd84310 commit c88654e

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,17 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
11731173
!params[0].isVariadic());
11741174
};
11751175

1176+
auto canImplodeParams = [&](ArrayRef<AnyFunctionType::Param> params) {
1177+
if (params.size() == 1)
1178+
return false;
1179+
1180+
for (auto param : params)
1181+
if (param.isVariadic() || param.isInOut())
1182+
return false;
1183+
1184+
return true;
1185+
};
1186+
11761187
auto implodeParams = [&](SmallVectorImpl<AnyFunctionType::Param> &params) {
11771188
auto input = AnyFunctionType::composeInput(getASTContext(), params,
11781189
/*canonicalVararg=*/false);
@@ -1193,21 +1204,18 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
11931204

11941205
if (last != path.rend()) {
11951206
if (last->getKind() == ConstraintLocator::ApplyArgToParam) {
1196-
if (isSingleParam(func2Params)) {
1197-
if (!isSingleParam(func1Params)) {
1198-
implodeParams(func1Params);
1199-
}
1200-
} else if (getASTContext().isSwiftVersionAtLeast(4)
1201-
&& !getASTContext().isSwiftVersionAtLeast(5)
1202-
&& !isSingleParam(func2Params)) {
1207+
if (isSingleParam(func2Params) &&
1208+
canImplodeParams(func1Params)) {
1209+
implodeParams(func1Params);
1210+
} else if (!getASTContext().isSwiftVersionAtLeast(5) &&
1211+
isSingleParam(func1Params) &&
1212+
canImplodeParams(func2Params)) {
12031213
auto *simplified = locator.trySimplifyToExpr();
12041214
// We somehow let tuple unsplatting function conversions
12051215
// through in some cases in Swift 4, so let's let that
12061216
// continue to work, but only for Swift 4.
12071217
if (simplified && isa<DeclRefExpr>(simplified)) {
1208-
if (isSingleParam(func1Params)) {
1209-
implodeParams(func2Params);
1210-
}
1218+
implodeParams(func2Params);
12111219
}
12121220
}
12131221
}

test/Compatibility/tuple_arguments_4.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-typecheck-verify-swift -swift-version 4
22

3-
// See test/Compatibility/tuple_arguments_3.swift for the Swift 3 behavior.
4-
53
func concrete(_ x: Int) {}
64
func concreteLabeled(x: Int) {}
75
func concreteTwo(_ x: Int, _ y: Int) {} // expected-note 5 {{'concreteTwo' declared here}}
@@ -1671,7 +1669,6 @@ do {
16711669
h() // expected-error {{missing argument for parameter #1 in call}}
16721670
}
16731671

1674-
16751672
// https://bugs.swift.org/browse/SR-7191
16761673
class Mappable<T> {
16771674
init(_: T) { }
@@ -1680,3 +1677,9 @@ class Mappable<T> {
16801677

16811678
let x = Mappable(())
16821679
_ = x.map { (_: Void) in return () }
1680+
1681+
// https://bugs.swift.org/browse/SR-9470
1682+
do {
1683+
func f(_: Int...) {}
1684+
let _ = [(1, 2, 3)].map(f) // expected-error {{cannot invoke 'map' with an argument list of type '((Int...) -> ())'}}
1685+
}

test/Constraints/tuple_arguments.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,3 +1671,18 @@ do {
16711671
func h(_: ()) {} // expected-note {{'h' declared here}}
16721672
h() // expected-error {{missing argument for parameter #1 in call}}
16731673
}
1674+
1675+
// https://bugs.swift.org/browse/SR-7191
1676+
class Mappable<T> {
1677+
init(_: T) { }
1678+
func map<U>(_ body: (T) -> U) -> U { fatalError() }
1679+
}
1680+
1681+
let x = Mappable(())
1682+
_ = x.map { (_: Void) in return () }
1683+
1684+
// https://bugs.swift.org/browse/SR-9470
1685+
do {
1686+
func f(_: Int...) {}
1687+
let _ = [(1, 2, 3)].map(f) // expected-error {{cannot invoke 'map' with an argument list of type '((Int...) -> ())'}}
1688+
}

0 commit comments

Comments
 (0)