Skip to content

Commit 4af4a3d

Browse files
authored
Merge pull request #30656 from apple/revert-29845-trailing-closure-position
Revert "[ConstraintSystem] Accept trailing closure if multiple defaulted parameters after last function parameter"
2 parents 4accf59 + f759968 commit 4af4a3d

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -431,24 +431,24 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
431431

432432
// If we have a trailing closure, it maps to the last parameter.
433433
if (hasTrailingClosure && numParams > 0) {
434+
unsigned lastParamIdx = numParams - 1;
435+
bool lastAcceptsTrailingClosure =
436+
acceptsTrailingClosure(params[lastParamIdx]);
437+
434438
// If the last parameter is defaulted, this might be
435439
// an attempt to use a trailing closure with previous
436440
// parameter that accepts a function type e.g.
437441
//
438442
// func foo(_: () -> Int, _ x: Int = 0) {}
439443
// foo { 42 }
440-
bool lastAcceptsTrailingClosure = false;
441-
unsigned lastParamIdx = numParams - 1;
442-
for (unsigned i : indices(params)) {
443-
unsigned idx = numParams - 1 - i;
444-
if (acceptsTrailingClosure(params[idx])) {
444+
if (!lastAcceptsTrailingClosure && numParams > 1 &&
445+
paramInfo.hasDefaultArgument(lastParamIdx)) {
446+
auto paramType = params[lastParamIdx - 1].getPlainType();
447+
// If the parameter before defaulted last accepts.
448+
if (paramType->is<AnyFunctionType>()) {
445449
lastAcceptsTrailingClosure = true;
446-
lastParamIdx = idx;
447-
break;
450+
lastParamIdx -= 1;
448451
}
449-
if (paramInfo.hasDefaultArgument(idx))
450-
continue;
451-
break;
452452
}
453453

454454
bool isExtraClosure = false;

test/Constraints/closures.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -921,14 +921,6 @@ func test_trailing_closure_with_defaulted_last() {
921921
func foo<T>(fn: () -> T, value: Int = 0) {}
922922
foo { 42 } // Ok
923923
foo(fn: { 42 }) // Ok
924-
925-
func bar<T>(type: T.Type, fn: T, a: Int = 0) {}
926-
bar(type: (() -> Int).self) { 42 }
927-
bar(type: (() -> Int).self, fn: { 42 })
928-
929-
func baz(fn: () -> Int, a: Int = 0, b: Int = 0, c: Int = 0) {}
930-
baz { 42 }
931-
baz(fn: { 42 })
932924
}
933925

934926
// Test that even in multi-statement closure case we still pick up `(Action) -> Void` over `Optional<(Action) -> Void>`.

0 commit comments

Comments
 (0)