@@ -891,22 +891,47 @@ void PotentialBindings::addLiteral(Constraint *constraint) {
891
891
Literals.insert (constraint);
892
892
}
893
893
894
- bool BindingSet::isViable (PotentialBinding &binding) const {
894
+ bool BindingSet::isViable (PotentialBinding &binding) {
895
895
// Prevent against checking against the same opened nominal type
896
896
// over and over again. Doing so means redundant work in the best
897
897
// case. In the worst case, we'll produce lots of duplicate solutions
898
898
// for this constraint system, which is problematic for overload
899
899
// resolution.
900
900
auto type = binding.BindingType ;
901
- if (type->hasTypeVariable ()) {
902
- auto *NTD = type->getAnyNominal ();
903
- if (!NTD)
904
- return true ;
905
901
906
- for (auto &existing : Bindings) {
907
- auto *existingNTD = existing.BindingType ->getAnyNominal ();
908
- if (existingNTD && NTD == existingNTD)
909
- return false ;
902
+ auto *NTD = type->getAnyNominal ();
903
+ if (!NTD)
904
+ return true ;
905
+
906
+ for (auto existing = Bindings.begin (); existing != Bindings.end ();
907
+ ++existing) {
908
+ auto existingType = existing->BindingType ;
909
+
910
+ auto *existingNTD = existingType->getAnyNominal ();
911
+ if (!existingNTD || NTD != existingNTD)
912
+ continue ;
913
+
914
+ // If new type has a type variable it shouldn't
915
+ // be considered viable.
916
+ if (type->hasTypeVariable ())
917
+ return false ;
918
+
919
+ // If new type doesn't have any type variables,
920
+ // but existing binding does, let's replace existing
921
+ // binding with new one.
922
+ if (existingType->hasTypeVariable ()) {
923
+ // First, let's remove all of the adjacent type
924
+ // variables associated with this binding.
925
+ {
926
+ SmallPtrSet<TypeVariableType *, 4 > referencedVars;
927
+ existingType->getTypeVariables (referencedVars);
928
+ for (auto *var : referencedVars)
929
+ AdjacentVars.erase (var);
930
+ }
931
+
932
+ // And now let's remove the binding itself.
933
+ Bindings.erase (existing);
934
+ break ;
910
935
}
911
936
}
912
937
0 commit comments