@@ -5442,7 +5442,31 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
5442
5442
}
5443
5443
5444
5444
auto overloadName = calleeInfo.declName ;
5445
-
5445
+
5446
+ // Local function to check if the error with argument type is
5447
+ // related to contextual type information of the enclosing expression
5448
+ // rather than resolution of argument expression itself.
5449
+ auto isContextualConversionFailure = [&](Expr *argExpr) -> bool {
5450
+ // If we found an exact match, this must be a problem with a conversion from
5451
+ // the result of the call to the expected type. Diagnose this as a
5452
+ // conversion failure.
5453
+ if (calleeInfo.closeness == CC_ExactMatch)
5454
+ return true ;
5455
+
5456
+ if (!CS->getContextualType () || calleeInfo.closeness != CC_ArgumentMismatch)
5457
+ return false ;
5458
+
5459
+ CalleeCandidateInfo candidates (fnExpr, hasTrailingClosure, CS);
5460
+
5461
+ // Filter original list of choices based on the deduced type of
5462
+ // argument expression after force re-check.
5463
+ candidates.filterContextualMemberList (argExpr);
5464
+
5465
+ // One of the candidates matches exactly, which means that
5466
+ // this is a contextual type conversion failure, we can't diagnose here.
5467
+ return candidates.closeness == CC_ExactMatch;
5468
+ };
5469
+
5446
5470
// Otherwise, we have a generic failure. Diagnose it with a generic error
5447
5471
// message now.
5448
5472
if (isa<BinaryExpr>(callExpr) && isa<TupleExpr>(argExpr)) {
@@ -5482,28 +5506,9 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
5482
5506
return true ;
5483
5507
}
5484
5508
}
5485
-
5486
- // If we found an exact match, this must be a problem with a conversion from
5487
- // the result of the call to the expected type. Diagnose this as a
5488
- // conversion failure.
5489
- if (calleeInfo.closeness == CC_ExactMatch)
5490
- return false ;
5491
-
5492
- // If this is not a specific structural problem we can diagnose,
5493
- // let's check if this is contextual type conversion error.
5494
- if (CS->getContextualType () &&
5495
- calleeInfo.closeness == CC_ArgumentMismatch) {
5496
- CalleeCandidateInfo candidates (fnExpr, hasTrailingClosure, CS);
5497
5509
5498
- // Filter original list of choices based on the deduced type of
5499
- // argument expression after force re-check.
5500
- candidates.filterContextualMemberList (argTuple);
5501
-
5502
- // One of the candidates matches exactly, which means that
5503
- // this is a contextual type conversion failure, we can't diagnose here.
5504
- if (candidates.closeness == CC_ExactMatch)
5505
- return false ;
5506
- }
5510
+ if (isContextualConversionFailure (argTuple))
5511
+ return false ;
5507
5512
5508
5513
if (!lhsType->isEqual (rhsType)) {
5509
5514
diagnose (callExpr->getLoc (), diag::cannot_apply_binop_to_args,
@@ -5531,11 +5536,8 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
5531
5536
5532
5537
return true ;
5533
5538
}
5534
-
5535
- // If we found an exact match, this must be a problem with a conversion from
5536
- // the result of the call to the expected type. Diagnose this as a
5537
- // conversion failure.
5538
- if (calleeInfo.closeness == CC_ExactMatch)
5539
+
5540
+ if (isContextualConversionFailure (argExpr))
5539
5541
return false ;
5540
5542
5541
5543
// Generate specific error messages for unary operators.
0 commit comments