@@ -430,19 +430,38 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
430
430
431
431
// If we have a trailing closure, it maps to the last parameter.
432
432
if (hasTrailingClosure && numParams > 0 ) {
433
- const auto ¶m = params[numParams - 1 ];
433
+ unsigned lastParamIdx = numParams - 1 ;
434
+ bool lastAcceptsTrailingClosure =
435
+ acceptsTrailingClosure (params[lastParamIdx]);
436
+
437
+ // If the last parameter is defaulted, this might be
438
+ // an attempt to use a trailing closure with previous
439
+ // parameter that accepts a function type e.g.
434
440
//
441
+ // func foo(_: () -> Int, _ x: Int = 0) {}
442
+ // foo { 42 }
443
+ if (!lastAcceptsTrailingClosure && numParams > 1 &&
444
+ paramInfo.hasDefaultArgument (lastParamIdx)) {
445
+ auto paramType = params[lastParamIdx - 1 ].getPlainType ();
446
+ // If the parameter before defaulted last accepts.
447
+ if (paramType->is <AnyFunctionType>()) {
448
+ lastAcceptsTrailingClosure = true ;
449
+ lastParamIdx -= 1 ;
450
+ }
451
+ }
452
+
435
453
bool isExtraClosure = false ;
436
454
// If there is no suitable last parameter to accept the trailing closure,
437
455
// notify the listener and bail if we need to.
438
- if (!acceptsTrailingClosure (param) ) {
456
+ if (!lastAcceptsTrailingClosure ) {
439
457
if (numArgs > numParams) {
440
458
// Argument before the trailing closure.
441
459
unsigned prevArg = numArgs - 2 ;
442
460
auto &arg = args[prevArg];
443
461
// If the argument before trailing closure matches
444
462
// last parameter, this is just a special case of
445
463
// an extraneous argument.
464
+ const auto param = params[numParams - 1 ];
446
465
if (param.hasLabel () && param.getLabel () == arg.getLabel ()) {
447
466
isExtraClosure = true ;
448
467
if (listener.extraArgument (numArgs - 1 ))
@@ -451,7 +470,7 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
451
470
}
452
471
453
472
if (!isExtraClosure &&
454
- listener.trailingClosureMismatch (numParams - 1 , numArgs - 1 ))
473
+ listener.trailingClosureMismatch (lastParamIdx , numArgs - 1 ))
455
474
return true ;
456
475
}
457
476
@@ -460,7 +479,7 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
460
479
++numClaimedArgs;
461
480
// Let's claim the trailing closure unless it's an extra argument.
462
481
if (!isExtraClosure)
463
- parameterBindings[numParams - 1 ].push_back (numArgs - 1 );
482
+ parameterBindings[lastParamIdx ].push_back (numArgs - 1 );
464
483
}
465
484
466
485
// Mark through the parameters, binding them to their arguments.
0 commit comments