@@ -58,6 +58,16 @@ bool ConstraintSystem::PotentialBindings::isDelayed() const {
58
58
return false ;
59
59
}
60
60
61
+ bool ConstraintSystem::PotentialBindings::involvesTypeVariables () const {
62
+ // This is effectively O(1) right now since bindings are re-computed
63
+ // on each step of the solver, but once bindings are computed
64
+ // incrementally it becomes more important to double-check that
65
+ // any adjacent type variables found previously are still unresolved.
66
+ return llvm::any_of (AdjacentVars, [](TypeVariableType *typeVar) {
67
+ return !typeVar->getImpl ().getFixedType (/* record=*/ nullptr );
68
+ });
69
+ }
70
+
61
71
bool ConstraintSystem::PotentialBindings::isPotentiallyIncomplete () const {
62
72
// Generic parameters are always potentially incomplete.
63
73
if (isGenericParameter ())
@@ -814,7 +824,7 @@ bool ConstraintSystem::PotentialBindings::favoredOverDisjunction(
814
824
return boundType->lookThroughAllOptionalTypes ()->is <TypeVariableType>();
815
825
}
816
826
817
- return !InvolvesTypeVariables ;
827
+ return !involvesTypeVariables () ;
818
828
}
819
829
820
830
ConstraintSystem::PotentialBindings
@@ -888,17 +898,16 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
888
898
}
889
899
}
890
900
891
- // Can't infer anything.
892
- if (result.InvolvesTypeVariables )
893
- return None;
894
-
895
901
// Check whether both this type and another type variable are
896
902
// inferable.
897
903
SmallPtrSet<TypeVariableType *, 4 > typeVars;
898
904
findInferableTypeVars (first, typeVars);
899
905
findInferableTypeVars (second, typeVars);
900
- if (typeVars.size () > 1 && typeVars.count (typeVar))
901
- result.InvolvesTypeVariables = true ;
906
+
907
+ if (typeVars.erase (typeVar)) {
908
+ result.AdjacentVars .insert (typeVars.begin (), typeVars.end ());
909
+ }
910
+
902
911
return None;
903
912
}
904
913
@@ -927,11 +936,27 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
927
936
// If the type we'd be binding to is a dependent member, don't try to
928
937
// resolve this type variable yet.
929
938
if (type->is <DependentMemberType>()) {
930
- if (!ConstraintSystem::typeVarOccursInType (typeVar, type,
931
- &result.InvolvesTypeVariables )) {
932
- result.DelayedBy .push_back (constraint);
939
+ SmallVector<TypeVariableType *, 4 > referencedVars;
940
+ type->getTypeVariables (referencedVars);
941
+
942
+ bool containsSelf = false ;
943
+ for (auto *var : referencedVars) {
944
+ // Add all type variables encountered in the type except
945
+ // to the current type variable.
946
+ if (var != typeVar) {
947
+ result.AdjacentVars .insert (var);
948
+ continue ;
949
+ }
950
+
951
+ containsSelf = true ;
933
952
}
934
953
954
+ // If inferred type doesn't contain the current type variable,
955
+ // let's mark bindings as delayed until dependent member type
956
+ // is resolved.
957
+ if (!containsSelf)
958
+ result.DelayedBy .push_back (constraint);
959
+
935
960
return None;
936
961
}
937
962
@@ -951,15 +976,18 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
951
976
// FIXME: this has a super-inefficient extraneous simplifyType() in it.
952
977
if (auto boundType = checkTypeOfBinding (typeVar, type)) {
953
978
type = *boundType;
954
- if (type->hasTypeVariable ())
955
- result.InvolvesTypeVariables = true ;
979
+ if (type->hasTypeVariable ()) {
980
+ SmallVector<TypeVariableType *, 4 > referencedVars;
981
+ type->getTypeVariables (referencedVars);
982
+ result.AdjacentVars .insert (referencedVars.begin (), referencedVars.end ());
983
+ }
956
984
} else {
957
985
auto *bindingTypeVar = type->getRValueType ()->getAs <TypeVariableType>();
958
986
959
987
if (!bindingTypeVar)
960
988
return None;
961
989
962
- result.InvolvesTypeVariables = true ;
990
+ result.AdjacentVars . insert (bindingTypeVar) ;
963
991
964
992
// If current type variable is associated with a code completion token
965
993
// it's possible that it doesn't have enough contextual information
0 commit comments