@@ -595,12 +595,13 @@ namespace {
595
595
// / of the overload set and call arguments.
596
596
// /
597
597
// / \param expr The application.
598
- // / \param isFavored Determine whether the given overload is favored.
598
+ // / \param isFavored Determine whether the given overload is favored, passing
599
+ // / it the "effective" overload type when it's being called.
599
600
// / \param mustConsider If provided, a function to detect the presence of
600
601
// / overloads which inhibit any overload from being favored.
601
602
void favorCallOverloads (ApplyExpr *expr,
602
603
ConstraintSystem &CS,
603
- llvm::function_ref<bool (ValueDecl *)> isFavored,
604
+ llvm::function_ref<bool (ValueDecl *, Type )> isFavored,
604
605
std::function<bool(ValueDecl *)>
605
606
mustConsider = nullptr) {
606
607
// Find the type variable associated with the function, if any.
@@ -621,7 +622,7 @@ namespace {
621
622
Constraint *firstFavored = nullptr ;
622
623
for (auto constraint : disjunction->getNestedConstraints ()) {
623
624
if (!constraint->getOverloadChoice ().isDecl ())
624
- return ;
625
+ continue ;
625
626
auto decl = constraint->getOverloadChoice ().getDecl ();
626
627
627
628
if (mustConsider && mustConsider (decl)) {
@@ -632,8 +633,14 @@ namespace {
632
633
return ;
633
634
}
634
635
636
+ Type overloadType =
637
+ CS.getEffectiveOverloadType (constraint->getOverloadChoice (),
638
+ /* allowMembers=*/ true , CS.DC );
639
+ if (!overloadType)
640
+ continue ;
641
+
635
642
if (!decl->getAttrs ().isUnavailable (CS.getASTContext ()) &&
636
- isFavored (decl)) {
643
+ isFavored (decl, overloadType )) {
637
644
// If we might need to roll back the favored constraints, keep
638
645
// track of those we are favoring.
639
646
if (mustConsider && !constraint->isFavored ())
@@ -650,9 +657,12 @@ namespace {
650
657
// result type.
651
658
if (numFavoredConstraints == 1 ) {
652
659
auto overloadChoice = firstFavored->getOverloadChoice ();
653
- auto overloadType = overloadChoice.getDecl ()->getInterfaceType ();
654
- auto resultType = overloadType->getAs <AnyFunctionType>()->getResult ();
655
- CS.setFavoredType (expr, resultType.getPointer ());
660
+ auto overloadType =
661
+ CS.getEffectiveOverloadType (overloadChoice, /* allowMembers=*/ true ,
662
+ CS.DC );
663
+ auto resultType = overloadType->castTo <AnyFunctionType>()->getResult ();
664
+ if (!resultType->hasTypeParameter ())
665
+ CS.setFavoredType (expr, resultType.getPointer ());
656
666
}
657
667
}
658
668
@@ -695,18 +705,11 @@ namespace {
695
705
void favorMatchingUnaryOperators (ApplyExpr *expr,
696
706
ConstraintSystem &CS) {
697
707
// Determine whether the given declaration is favored.
698
- auto isFavoredDecl = [&](ValueDecl *value) -> bool {
699
- auto valueTy = value->getInterfaceType ();
700
-
701
- auto fnTy = valueTy->getAs <AnyFunctionType>();
708
+ auto isFavoredDecl = [&](ValueDecl *value, Type type) -> bool {
709
+ auto fnTy = type->getAs <AnyFunctionType>();
702
710
if (!fnTy)
703
711
return false ;
704
712
705
- // Figure out the parameter type.
706
- if (value->getDeclContext ()->isTypeContext ()) {
707
- fnTy = fnTy->getResult ()->castTo <AnyFunctionType>();
708
- }
709
-
710
713
Type paramTy = FunctionType::composeInput (CS.getASTContext (),
711
714
fnTy->getParams (), false );
712
715
auto resultTy = fnTy->getResult ();
@@ -748,10 +751,8 @@ namespace {
748
751
}
749
752
750
753
// Determine whether the given declaration is favored.
751
- auto isFavoredDecl = [&](ValueDecl *value) -> bool {
752
- auto valueTy = value->getInterfaceType ();
753
-
754
- if (!valueTy->is <AnyFunctionType>())
754
+ auto isFavoredDecl = [&](ValueDecl *value, Type type) -> bool {
755
+ if (!type->is <AnyFunctionType>())
755
756
return false ;
756
757
757
758
auto paramCount = getParamCount (value);
@@ -766,23 +767,11 @@ namespace {
766
767
767
768
if (auto favoredTy = CS.getFavoredType (expr->getArg ())) {
768
769
// Determine whether the given declaration is favored.
769
- auto isFavoredDecl = [&](ValueDecl *value) -> bool {
770
- auto valueTy = value->getInterfaceType ();
771
-
772
- auto fnTy = valueTy->getAs <AnyFunctionType>();
770
+ auto isFavoredDecl = [&](ValueDecl *value, Type type) -> bool {
771
+ auto fnTy = type->getAs <AnyFunctionType>();
773
772
if (!fnTy)
774
773
return false ;
775
774
776
- // Figure out the parameter type, accounting for the implicit 'self' if
777
- // necessary.
778
- if (auto *FD = dyn_cast<AbstractFunctionDecl>(value)) {
779
- if (FD->hasImplicitSelfDecl ()) {
780
- if (auto resFnTy = fnTy->getResult ()->getAs <AnyFunctionType>()) {
781
- fnTy = resFnTy;
782
- }
783
- }
784
- }
785
-
786
775
auto paramTy =
787
776
AnyFunctionType::composeInput (CS.getASTContext (), fnTy->getParams (),
788
777
/* canonicalVararg*/ false );
@@ -841,10 +830,8 @@ namespace {
841
830
};
842
831
843
832
// Determine whether the given declaration is favored.
844
- auto isFavoredDecl = [&](ValueDecl *value) -> bool {
845
- auto valueTy = value->getInterfaceType ();
846
-
847
- auto fnTy = valueTy->getAs <AnyFunctionType>();
833
+ auto isFavoredDecl = [&](ValueDecl *value, Type type) -> bool {
834
+ auto fnTy = type->getAs <AnyFunctionType>();
848
835
if (!fnTy)
849
836
return false ;
850
837
@@ -870,11 +857,6 @@ namespace {
870
857
}
871
858
}
872
859
873
- // Figure out the parameter type.
874
- if (value->getDeclContext ()->isTypeContext ()) {
875
- fnTy = fnTy->getResult ()->castTo <AnyFunctionType>();
876
- }
877
-
878
860
auto params = fnTy->getParams ();
879
861
if (params.size () != 2 )
880
862
return false ;
0 commit comments