Skip to content

Commit e96436a

Browse files
authored
Merge pull request #62392 from xedin/issue-62390
[CSSimplify] Account for tuple splat when resolving closure parameters
2 parents 0368538 + 7588434 commit e96436a

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10595,6 +10595,27 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1059510595
return None;
1059610596

1059710597
auto numContextualParams = fnType->getNumParams();
10598+
10599+
if (numContextualParams == 1) {
10600+
const auto &param = fnType->getParams()[0];
10601+
if (auto *tuple = param.getPlainType()->getAs<TupleType>()) {
10602+
// If arity is the same it's a tuple splat which is allowed
10603+
// for closures (see SE-0110 for more details):
10604+
//
10605+
// func test(_: ((Int, Int)) -> Void) {}
10606+
// test { (arg, _) in
10607+
// ...
10608+
// }
10609+
if (tuple->getNumElements() == inferredClosureType->getNumParams() &&
10610+
param.getParameterFlags().isNone()) {
10611+
const auto &elt = tuple->getElement(index);
10612+
return AnyFunctionType::Param(elt.getType(), elt.getName());
10613+
}
10614+
10615+
return None;
10616+
}
10617+
}
10618+
1059810619
if (numContextualParams != inferredClosureType->getNumParams() ||
1059910620
numContextualParams <= index)
1060010621
return None;

test/Constraints/closures.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ func rdar21078316() {
386386

387387
// <rdar://problem/20978044> QoI: Poor diagnostic when using an incorrect tuple element in a closure
388388
var numbers = [1, 2, 3]
389-
zip(numbers, numbers).filter { $0.2 > 1 } // expected-error {{value of tuple type '(Array<Int>.Element, Array<Int>.Element)' has no member '2'}}
389+
zip(numbers, numbers).filter { $0.2 > 1 } // expected-error {{value of tuple type '(Int, Int)' has no member '2'}}
390390

391391

392392

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-typecheck-verify-swift -solver-expression-time-threshold=1
2+
// REQUIRES: tools-release,no_asan
3+
4+
// https://github.com/apple/swift/issues/62390
5+
// Compiler is extremely slow to compile simple closure
6+
7+
func testFn<U>(_: ((Int, Int)) -> U) {}
8+
9+
testFn { (a, _) in
10+
return ((a <= a && a >= a) || (a <= a && a >= a)) // Ok
11+
}

0 commit comments

Comments
 (0)