@@ -381,6 +381,11 @@ namespace {
381
381
if (paramTy->isEqual (argTy))
382
382
return true ;
383
383
384
+ // Double and CGFloat types could be used interchangeably
385
+ if ((argTy->isCGFloatType () || argTy->isDoubleType ()) &&
386
+ (paramTy->isDoubleType () || paramTy->isCGFloatType ()))
387
+ return true ;
388
+
384
389
llvm::SmallSetVector<ProtocolDecl *, 2 > literalProtos;
385
390
if (auto argTypeVar = argTy->getAs <TypeVariableType>()) {
386
391
auto constraints = CS.getConstraintGraph ().gatherConstraints (
@@ -532,7 +537,23 @@ namespace {
532
537
533
538
return { nOperands, nNoDefault };
534
539
}
535
-
540
+
541
+ bool hasContextuallyFavorableResultType (AnyFunctionType *choice,
542
+ Type contextualTy) {
543
+ // No restrictions of what result could be.
544
+ if (!contextualTy)
545
+ return true ;
546
+
547
+ auto resultTy = choice->getResult ();
548
+ // Result type of the call matches expected contextual type.
549
+ if (contextualTy->isEqual (resultTy))
550
+ return true ;
551
+
552
+ // Double and CGFloat could be used interchangeably.
553
+ return (resultTy->isDoubleType () || resultTy->isCGFloatType ()) &&
554
+ (contextualTy->isDoubleType () || contextualTy->isCGFloatType ());
555
+ }
556
+
536
557
// / Favor unary operator constraints where we have exact matches
537
558
// / for the operand and contextual type.
538
559
void favorMatchingUnaryOperators (ApplyExpr *expr,
@@ -545,15 +566,12 @@ namespace {
545
566
546
567
Type paramTy = FunctionType::composeInput (CS.getASTContext (),
547
568
fnTy->getParams (), false );
548
- auto resultTy = fnTy->getResult ();
549
- auto contextualTy = CS.getContextualType (expr);
550
-
551
569
return isFavoredParamAndArg (
552
- CS, paramTy,
553
- CS. getType (expr-> getArg ())-> getWithoutParens ()) &&
554
- (!contextualTy || contextualTy-> isEqual (resultTy ));
570
+ CS, paramTy, CS. getType (expr-> getArg ())-> getWithoutParens ()) &&
571
+ hasContextuallyFavorableResultType (fnTy,
572
+ CS. getContextualType (expr ));
555
573
};
556
-
574
+
557
575
favorCallOverloads (expr, CS, isFavoredDecl);
558
576
}
559
577
@@ -704,15 +722,14 @@ namespace {
704
722
auto firstParamTy = params[0 ].getOldType ();
705
723
auto secondParamTy = params[1 ].getOldType ();
706
724
707
- auto resultTy = fnTy->getResult ();
708
725
auto contextualTy = CS.getContextualType (expr);
709
726
710
727
return (isFavoredParamAndArg (CS, firstParamTy, firstArgTy, secondArgTy) ||
711
728
isFavoredParamAndArg (CS, secondParamTy, secondArgTy,
712
729
firstArgTy)) &&
713
730
firstParamTy->isEqual (secondParamTy) &&
714
731
!isPotentialForcingOpportunity (firstArgTy, secondArgTy) &&
715
- (!contextualTy || contextualTy-> isEqual (resultTy) );
732
+ hasContextuallyFavorableResultType (fnTy, contextualTy);
716
733
};
717
734
718
735
favorCallOverloads (expr, CS, isFavoredDecl);
0 commit comments