@@ -595,107 +595,74 @@ 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.
607
608
auto tyvarType = CS.getType (expr->getFn ())->getAs <TypeVariableType>();
608
- if (!tyvarType)
609
+ if (!tyvarType || CS. getFixedType (tyvarType) )
609
610
return ;
610
611
611
612
// This type variable is only currently associated with the function
612
613
// being applied, and the only constraint attached to it should
613
614
// be the disjunction constraint for the overload group.
614
- auto &CG = CS.getConstraintGraph ();
615
- llvm::SetVector<Constraint *> disjunctions;
616
- CG.gatherConstraints (tyvarType, disjunctions,
617
- ConstraintGraph::GatheringKind::EquivalenceClass,
618
- [](Constraint *constraint) -> bool {
619
- return constraint->getKind () ==
620
- ConstraintKind::Disjunction;
621
- });
622
- if (disjunctions.empty ())
615
+ auto disjunction = CS.getUnboundBindOverloadDisjunction (tyvarType);
616
+ if (!disjunction)
623
617
return ;
624
618
625
- // Look for the disjunction that binds the overload set.
626
- for (auto *disjunction : disjunctions) {
627
- auto oldConstraints = disjunction->getNestedConstraints ();
628
- auto csLoc = CS.getConstraintLocator (expr->getFn ());
629
-
630
- // Only replace the disjunctive overload constraint.
631
- if (oldConstraints[0 ]->getKind () != ConstraintKind::BindOverload) {
619
+ // Find the favored constraints and mark them.
620
+ SmallVector<Constraint *, 4 > newlyFavoredConstraints;
621
+ unsigned numFavoredConstraints = 0 ;
622
+ Constraint *firstFavored = nullptr ;
623
+ for (auto constraint : disjunction->getNestedConstraints ()) {
624
+ if (!constraint->getOverloadChoice ().isDecl ())
632
625
continue ;
633
- }
626
+ auto decl = constraint-> getOverloadChoice (). getDecl ();
634
627
635
- if (mustConsider) {
636
- bool hasMustConsider = false ;
637
- for (auto oldConstraint : oldConstraints) {
638
- auto overloadChoice = oldConstraint->getOverloadChoice ();
639
- if (overloadChoice.isDecl () &&
640
- mustConsider (overloadChoice.getDecl ()))
641
- hasMustConsider = true ;
642
- }
643
- if (hasMustConsider) {
644
- continue ;
645
- }
646
- }
628
+ if (mustConsider && mustConsider (decl)) {
629
+ // Roll back any constraints we favored.
630
+ for (auto favored : newlyFavoredConstraints)
631
+ favored->setFavored (false );
647
632
648
- // Copy over the existing bindings, dividing the constraints up
649
- // into "favored" and non-favored lists.
650
- SmallVector<Constraint *, 4 > favoredConstraints;
651
- SmallVector<Constraint *, 4 > fallbackConstraints;
652
- for (auto oldConstraint : oldConstraints) {
653
- if (!oldConstraint->getOverloadChoice ().isDecl ())
654
- continue ;
655
- auto decl = oldConstraint->getOverloadChoice ().getDecl ();
656
- if (!decl->getAttrs ().isUnavailable (CS.getASTContext ()) &&
657
- isFavored (decl))
658
- favoredConstraints.push_back (oldConstraint);
659
- else
660
- fallbackConstraints.push_back (oldConstraint);
633
+ return ;
661
634
}
662
635
663
- // If we did not find any favored constraints, we're done.
664
- if (favoredConstraints.empty ()) break ;
636
+ Type overloadType =
637
+ CS.getEffectiveOverloadType (constraint->getOverloadChoice (),
638
+ /* allowMembers=*/ true , CS.DC );
639
+ if (!overloadType)
640
+ continue ;
665
641
666
- if (favoredConstraints.size () == 1 ) {
667
- auto overloadChoice = favoredConstraints[0 ]->getOverloadChoice ();
668
- auto overloadType = overloadChoice.getDecl ()->getInterfaceType ();
669
- auto resultType = overloadType->getAs <AnyFunctionType>()->getResult ();
642
+ if (!decl->getAttrs ().isUnavailable (CS.getASTContext ()) &&
643
+ isFavored (decl, overloadType)) {
644
+ // If we might need to roll back the favored constraints, keep
645
+ // track of those we are favoring.
646
+ if (mustConsider && !constraint->isFavored ())
647
+ newlyFavoredConstraints.push_back (constraint);
648
+
649
+ constraint->setFavored ();
650
+ ++numFavoredConstraints;
651
+ if (!firstFavored)
652
+ firstFavored = constraint;
653
+ }
654
+ }
655
+
656
+ // If there was one favored constraint, set the favored type based on its
657
+ // result type.
658
+ if (numFavoredConstraints == 1 ) {
659
+ auto overloadChoice = firstFavored->getOverloadChoice ();
660
+ auto overloadType =
661
+ CS.getEffectiveOverloadType (overloadChoice, /* allowMembers=*/ true ,
662
+ CS.DC );
663
+ auto resultType = overloadType->castTo <AnyFunctionType>()->getResult ();
664
+ if (!resultType->hasTypeParameter ())
670
665
CS.setFavoredType (expr, resultType.getPointer ());
671
- }
672
-
673
- // Remove the original constraint from the inactive constraint
674
- // list and add the new one.
675
- CS.removeInactiveConstraint (disjunction);
676
-
677
- // Create the disjunction of favored constraints.
678
- auto favoredConstraintsDisjunction =
679
- Constraint::createDisjunction (CS,
680
- favoredConstraints,
681
- csLoc);
682
-
683
- favoredConstraintsDisjunction->setFavored ();
684
-
685
- llvm::SmallVector<Constraint *, 2 > aggregateConstraints;
686
- aggregateConstraints.push_back (favoredConstraintsDisjunction);
687
-
688
- if (!fallbackConstraints.empty ()) {
689
- // Find the disjunction of fallback constraints. If any
690
- // constraints were added here, create a new disjunction.
691
- Constraint *fallbackConstraintsDisjunction =
692
- Constraint::createDisjunction (CS, fallbackConstraints, csLoc);
693
-
694
- aggregateConstraints.push_back (fallbackConstraintsDisjunction);
695
- }
696
-
697
- CS.addDisjunctionConstraint (aggregateConstraints, csLoc);
698
- break ;
699
666
}
700
667
}
701
668
@@ -738,18 +705,11 @@ namespace {
738
705
void favorMatchingUnaryOperators (ApplyExpr *expr,
739
706
ConstraintSystem &CS) {
740
707
// Determine whether the given declaration is favored.
741
- auto isFavoredDecl = [&](ValueDecl *value) -> bool {
742
- auto valueTy = value->getInterfaceType ();
743
-
744
- auto fnTy = valueTy->getAs <AnyFunctionType>();
708
+ auto isFavoredDecl = [&](ValueDecl *value, Type type) -> bool {
709
+ auto fnTy = type->getAs <AnyFunctionType>();
745
710
if (!fnTy)
746
711
return false ;
747
712
748
- // Figure out the parameter type.
749
- if (value->getDeclContext ()->isTypeContext ()) {
750
- fnTy = fnTy->getResult ()->castTo <AnyFunctionType>();
751
- }
752
-
753
713
Type paramTy = FunctionType::composeInput (CS.getASTContext (),
754
714
fnTy->getParams (), false );
755
715
auto resultTy = fnTy->getResult ();
@@ -791,10 +751,8 @@ namespace {
791
751
}
792
752
793
753
// Determine whether the given declaration is favored.
794
- auto isFavoredDecl = [&](ValueDecl *value) -> bool {
795
- auto valueTy = value->getInterfaceType ();
796
-
797
- if (!valueTy->is <AnyFunctionType>())
754
+ auto isFavoredDecl = [&](ValueDecl *value, Type type) -> bool {
755
+ if (!type->is <AnyFunctionType>())
798
756
return false ;
799
757
800
758
auto paramCount = getParamCount (value);
@@ -809,23 +767,11 @@ namespace {
809
767
810
768
if (auto favoredTy = CS.getFavoredType (expr->getArg ())) {
811
769
// Determine whether the given declaration is favored.
812
- auto isFavoredDecl = [&](ValueDecl *value) -> bool {
813
- auto valueTy = value->getInterfaceType ();
814
-
815
- auto fnTy = valueTy->getAs <AnyFunctionType>();
770
+ auto isFavoredDecl = [&](ValueDecl *value, Type type) -> bool {
771
+ auto fnTy = type->getAs <AnyFunctionType>();
816
772
if (!fnTy)
817
773
return false ;
818
774
819
- // Figure out the parameter type, accounting for the implicit 'self' if
820
- // necessary.
821
- if (auto *FD = dyn_cast<AbstractFunctionDecl>(value)) {
822
- if (FD->hasImplicitSelfDecl ()) {
823
- if (auto resFnTy = fnTy->getResult ()->getAs <AnyFunctionType>()) {
824
- fnTy = resFnTy;
825
- }
826
- }
827
- }
828
-
829
775
auto paramTy =
830
776
AnyFunctionType::composeInput (CS.getASTContext (), fnTy->getParams (),
831
777
/* canonicalVararg*/ false );
@@ -884,10 +830,8 @@ namespace {
884
830
};
885
831
886
832
// Determine whether the given declaration is favored.
887
- auto isFavoredDecl = [&](ValueDecl *value) -> bool {
888
- auto valueTy = value->getInterfaceType ();
889
-
890
- auto fnTy = valueTy->getAs <AnyFunctionType>();
833
+ auto isFavoredDecl = [&](ValueDecl *value, Type type) -> bool {
834
+ auto fnTy = type->getAs <AnyFunctionType>();
891
835
if (!fnTy)
892
836
return false ;
893
837
@@ -913,11 +857,6 @@ namespace {
913
857
}
914
858
}
915
859
916
- // Figure out the parameter type.
917
- if (value->getDeclContext ()->isTypeContext ()) {
918
- fnTy = fnTy->getResult ()->castTo <AnyFunctionType>();
919
- }
920
-
921
860
auto params = fnTy->getParams ();
922
861
if (params.size () != 2 )
923
862
return false ;
0 commit comments