@@ -3425,18 +3425,36 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
3425
3425
if (!anchor)
3426
3426
return getTypeMatchFailure(argumentLocator);
3427
3427
3428
- // If there are missing arguments, let's add them
3429
- // using parameter as a template.
3430
- if (diff < 0) {
3431
- if (fixMissingArguments(*this, anchor, func1Params, func2Params,
3432
- abs(diff), loc))
3428
+ // The param diff is in a function type coercion context
3429
+ //
3430
+ // func fn(_: Int) {}
3431
+ // let i: Int = 0
3432
+ // (fn as (Int, Int) -> Void)(i, i)
3433
+ //
3434
+ // Since we are not in a function argument application, so simply record
3435
+ // a function type mismatch instead of an argument fix.
3436
+ // Except for when a closure is a subexpr because missing and extraneous
3437
+ // arg fix can properly handle closure diagnostic.
3438
+ if (loc->isForCoercion() && !isExpr<ClosureExpr>(anchor)) {
3439
+ auto *fix = ContextualMismatch::create(*this, func1, func2, loc);
3440
+ if (recordFix(fix))
3433
3441
return getTypeMatchFailure(argumentLocator);
3434
3442
} else {
3435
- // If there are extraneous arguments, let's remove
3436
- // them from the list.
3437
- if (fixExtraneousArguments(*this, func2, func1Params, diff, loc))
3438
- return getTypeMatchFailure(argumentLocator);
3443
+ // If there are missing arguments, let's add them
3444
+ // using parameter as a template.
3445
+ if (diff < 0) {
3446
+ if (fixMissingArguments(*this, anchor, func1Params, func2Params,
3447
+ abs(diff), loc))
3448
+ return getTypeMatchFailure(argumentLocator);
3449
+ } else {
3450
+ // If there are extraneous arguments, let's remove
3451
+ // them from the list.
3452
+ if (fixExtraneousArguments(*this, func2, func1Params, diff, loc))
3453
+ return getTypeMatchFailure(argumentLocator);
3454
+ }
3455
+ }
3439
3456
3457
+ if (diff > 0) {
3440
3458
// Drop all of the extraneous arguments.
3441
3459
auto numParams = func2Params.size();
3442
3460
func1Params.erase(func1Params.begin() + numParams, func1Params.end());
0 commit comments