@@ -4747,9 +4747,6 @@ class ConstraintSystem {
4747
4747
// / Whether this type variable is only bound above by existential types.
4748
4748
bool SubtypeOfExistentialType = false ;
4749
4749
4750
- // / The number of defaultable bindings.
4751
- unsigned NumDefaultableBindings = 0 ;
4752
-
4753
4750
// / Tracks the position of the last known supertype in the group.
4754
4751
Optional<unsigned > lastSupertypeIndex;
4755
4752
@@ -4767,35 +4764,45 @@ class ConstraintSystem {
4767
4764
// / Determine whether the set of bindings is non-empty.
4768
4765
explicit operator bool () const { return !Bindings.empty (); }
4769
4766
4770
- // / Whether there are any non-defaultable bindings.
4771
- bool hasNonDefaultableBindings () const {
4772
- return Bindings.size () > NumDefaultableBindings;
4767
+ unsigned getNumDefaultableBindings () const {
4768
+ return llvm::count_if (Bindings, [](const PotentialBinding &binding) {
4769
+ return binding.isDefaultableBinding ();
4770
+ });
4773
4771
}
4774
4772
4775
4773
static BindingScore formBindingScore (const PotentialBindings &b) {
4774
+ auto numDefaults = b.getNumDefaultableBindings ();
4775
+ auto hasNoDefaultableBindings = b.Bindings .size () > numDefaults;
4776
+
4776
4777
return std::make_tuple (b.IsHole ,
4777
- !b. hasNonDefaultableBindings () ,
4778
+ !hasNoDefaultableBindings ,
4778
4779
b.FullyBound ,
4779
4780
b.SubtypeOfExistentialType ,
4780
4781
b.InvolvesTypeVariables ,
4781
4782
static_cast <unsigned char >(b.LiteralBinding ),
4782
- -(b.Bindings .size () - b. NumDefaultableBindings ));
4783
+ -(b.Bindings .size () - numDefaults ));
4783
4784
}
4784
4785
4785
4786
// / Compare two sets of bindings, where \c x < y indicates that
4786
4787
// / \c x is a better set of bindings that \c y.
4787
4788
friend bool operator <(const PotentialBindings &x,
4788
4789
const PotentialBindings &y) {
4789
- if (formBindingScore (x) < formBindingScore (y))
4790
+ auto xScore = formBindingScore (x);
4791
+ auto yScore = formBindingScore (y);
4792
+
4793
+ if (xScore < yScore)
4790
4794
return true ;
4791
4795
4792
- if (formBindingScore (y) < formBindingScore (x) )
4796
+ if (yScore < xScore )
4793
4797
return false ;
4794
4798
4799
+ auto xDefaults = x.Bindings .size () + std::get<6 >(xScore);
4800
+ auto yDefaults = y.Bindings .size () + std::get<6 >(yScore);
4801
+
4795
4802
// If there is a difference in number of default types,
4796
4803
// prioritize bindings with fewer of them.
4797
- if (x. NumDefaultableBindings != y. NumDefaultableBindings )
4798
- return x. NumDefaultableBindings < y. NumDefaultableBindings ;
4804
+ if (xDefaults != yDefaults )
4805
+ return xDefaults < yDefaults ;
4799
4806
4800
4807
// If neither type variable is a "hole" let's check whether
4801
4808
// there is a subtype relationship between them and prefer
@@ -4924,8 +4931,10 @@ class ConstraintSystem {
4924
4931
out << " literal=" << static_cast <int >(LiteralBinding) << " " ;
4925
4932
if (InvolvesTypeVariables)
4926
4933
out << " involves_type_vars " ;
4927
- if (NumDefaultableBindings > 0 )
4928
- out << " #defaultable_bindings=" << NumDefaultableBindings << " " ;
4934
+
4935
+ auto numDefaultable = getNumDefaultableBindings ();
4936
+ if (numDefaultable > 0 )
4937
+ out << " #defaultable_bindings=" << numDefaultable << " " ;
4929
4938
4930
4939
PrintOptions PO;
4931
4940
PO.PrintTypesForDebugging = true ;
0 commit comments