Skip to content

SE-0110 tuple splatting should not implode argument list with varargs [5.0] #21361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,17 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
!params[0].isVariadic());
};

auto canImplodeParams = [&](ArrayRef<AnyFunctionType::Param> params) {
if (params.size() == 1)
return false;

for (auto param : params)
if (param.isVariadic() || param.isInOut())
return false;

return true;
};

auto implodeParams = [&](SmallVectorImpl<AnyFunctionType::Param> &params) {
auto input = AnyFunctionType::composeInput(getASTContext(), params,
/*canonicalVararg=*/false);
Expand All @@ -1193,21 +1204,18 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,

if (last != path.rend()) {
if (last->getKind() == ConstraintLocator::ApplyArgToParam) {
if (isSingleParam(func2Params)) {
if (!isSingleParam(func1Params)) {
implodeParams(func1Params);
}
} else if (getASTContext().isSwiftVersionAtLeast(4)
&& !getASTContext().isSwiftVersionAtLeast(5)
&& !isSingleParam(func2Params)) {
if (isSingleParam(func2Params) &&
canImplodeParams(func1Params)) {
implodeParams(func1Params);
} else if (!getASTContext().isSwiftVersionAtLeast(5) &&
isSingleParam(func1Params) &&
canImplodeParams(func2Params)) {
auto *simplified = locator.trySimplifyToExpr();
// We somehow let tuple unsplatting function conversions
// through in some cases in Swift 4, so let's let that
// continue to work, but only for Swift 4.
if (simplified && isa<DeclRefExpr>(simplified)) {
if (isSingleParam(func1Params)) {
implodeParams(func2Params);
}
implodeParams(func2Params);
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions test/Compatibility/tuple_arguments_4.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// RUN: %target-typecheck-verify-swift -swift-version 4

// See test/Compatibility/tuple_arguments_3.swift for the Swift 3 behavior.

func concrete(_ x: Int) {}
func concreteLabeled(x: Int) {}
func concreteTwo(_ x: Int, _ y: Int) {} // expected-note 5 {{'concreteTwo' declared here}}
Expand Down Expand Up @@ -1671,7 +1669,6 @@ do {
h() // expected-error {{missing argument for parameter #1 in call}}
}


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

let x = Mappable(())
_ = x.map { (_: Void) in return () }

// https://bugs.swift.org/browse/SR-9470
do {
func f(_: Int...) {}
let _ = [(1, 2, 3)].map(f) // expected-error {{cannot invoke 'map' with an argument list of type '((Int...) -> ())'}}
}
15 changes: 15 additions & 0 deletions test/Constraints/tuple_arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1671,3 +1671,18 @@ do {
func h(_: ()) {} // expected-note {{'h' declared here}}
h() // expected-error {{missing argument for parameter #1 in call}}
}

// https://bugs.swift.org/browse/SR-7191
class Mappable<T> {
init(_: T) { }
func map<U>(_ body: (T) -> U) -> U { fatalError() }
}

let x = Mappable(())
_ = x.map { (_: Void) in return () }

// https://bugs.swift.org/browse/SR-9470
do {
func f(_: Int...) {}
let _ = [(1, 2, 3)].map(f) // expected-error {{cannot invoke 'map' with an argument list of type '((Int...) -> ())'}}
}