Skip to content

Commit cb4e597

Browse files
committed
[ConstraintSystem] NFC: Convert getPotentialBindingForRelationalConstraint into a method on PotentialBindings
1 parent 72888ca commit cb4e597

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,11 @@ struct PotentialBindings {
458458
llvm::SmallDenseMap<TypeVariableType *, PotentialBindings>
459459
&inferredBindings);
460460

461+
/// Attempt to infer a new binding and other useful information
462+
/// (i.e. whether bindings should be delayed) from the given
463+
/// relational constraint.
464+
Optional<PotentialBinding> inferFromRelational(Constraint *constraint);
465+
461466
public:
462467
bool infer(Constraint *constraint);
463468

include/swift/Sema/ConstraintSystem.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,7 +2036,6 @@ class ConstraintSystem {
20362036
friend class TypeVariableStep;
20372037
friend class RequirementFailure;
20382038
friend class MissingMemberFailure;
2039-
friend struct PotentialBindings;
20402039

20412040
class SolverScope;
20422041

@@ -4611,10 +4610,6 @@ class ConstraintSystem {
46114610
bool finalize = true);
46124611

46134612
private:
4614-
Optional<PotentialBinding>
4615-
getPotentialBindingForRelationalConstraint(PotentialBindings &result,
4616-
Constraint *constraint) const;
4617-
46184613
/// Add a constraint to the constraint system.
46194614
SolutionKind addConstraintImpl(ConstraintKind kind, Type first, Type second,
46204615
ConstraintLocatorBuilder locator,

lib/Sema/CSBindings.cpp

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -830,27 +830,24 @@ PotentialBindings ConstraintSystem::inferBindingsFor(TypeVariableType *typeVar,
830830
}
831831

832832
Optional<PotentialBinding>
833-
ConstraintSystem::getPotentialBindingForRelationalConstraint(
834-
PotentialBindings &result, Constraint *constraint) const {
833+
PotentialBindings::inferFromRelational(Constraint *constraint) {
835834
assert(constraint->getClassification() ==
836835
ConstraintClassification::Relational &&
837836
"only relational constraints handled here");
838837

839-
auto *typeVar = result.TypeVar;
840-
841-
auto first = simplifyType(constraint->getFirstType());
842-
auto second = simplifyType(constraint->getSecondType());
838+
auto first = CS.simplifyType(constraint->getFirstType());
839+
auto second = CS.simplifyType(constraint->getSecondType());
843840

844841
if (first->is<TypeVariableType>() && first->isEqual(second))
845842
return None;
846843

847844
Type type;
848845
AllowedBindingKind kind;
849-
if (first->getAs<TypeVariableType>() == typeVar) {
846+
if (first->getAs<TypeVariableType>() == TypeVar) {
850847
// Upper bound for this type variable.
851848
type = second;
852849
kind = AllowedBindingKind::Subtypes;
853-
} else if (second->getAs<TypeVariableType>() == typeVar) {
850+
} else if (second->getAs<TypeVariableType>() == TypeVar) {
854851
// Lower bound for this type variable.
855852
type = first;
856853
kind = AllowedBindingKind::Supertypes;
@@ -863,7 +860,7 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
863860
// of bindings for them until closure's body is opened.
864861
if (auto *typeVar = first->getAs<TypeVariableType>()) {
865862
if (typeVar->getImpl().isClosureType()) {
866-
result.DelayedBy.push_back(constraint);
863+
DelayedBy.push_back(constraint);
867864
return None;
868865
}
869866
}
@@ -874,8 +871,8 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
874871
findInferableTypeVars(first, typeVars);
875872
findInferableTypeVars(second, typeVars);
876873

877-
if (typeVars.erase(typeVar)) {
878-
result.AdjacentVars.insert(typeVars.begin(), typeVars.end());
874+
if (typeVars.erase(TypeVar)) {
875+
AdjacentVars.insert(typeVars.begin(), typeVars.end());
879876
}
880877

881878
return None;
@@ -885,11 +882,11 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
885882
if (type->hasError())
886883
return None;
887884

888-
if (auto *locator = typeVar->getImpl().getLocator()) {
885+
if (auto *locator = TypeVar->getImpl().getLocator()) {
889886
if (locator->isKeyPathType()) {
890887
auto *BGT =
891888
type->lookThroughAllOptionalTypes()->getAs<BoundGenericType>();
892-
if (!BGT || !isKnownKeyPathDecl(getASTContext(), BGT->getDecl()))
889+
if (!BGT || !isKnownKeyPathDecl(CS.getASTContext(), BGT->getDecl()))
893890
return None;
894891
}
895892
}
@@ -915,8 +912,8 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
915912
for (auto *var : referencedVars) {
916913
// Add all type variables encountered in the type except
917914
// to the current type variable.
918-
if (var != typeVar) {
919-
result.AdjacentVars.insert(var);
915+
if (var != TypeVar) {
916+
AdjacentVars.insert(var);
920917
continue;
921918
}
922919

@@ -927,7 +924,7 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
927924
// let's mark bindings as delayed until dependent member type
928925
// is resolved.
929926
if (!containsSelf)
930-
result.DelayedBy.push_back(constraint);
927+
DelayedBy.push_back(constraint);
931928

932929
return None;
933930
}
@@ -941,25 +938,25 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
941938
// should be allowed to escape. As a result we allow anything
942939
// passed in to escape.
943940
if (auto *fnTy = type->getAs<AnyFunctionType>())
944-
if (typeVar->getImpl().getGenericParameter() && !shouldAttemptFixes())
941+
if (isGenericParameter() && !CS.shouldAttemptFixes())
945942
type = fnTy->withExtInfo(fnTy->getExtInfo().withNoEscape(false));
946943

947944
// Check whether we can perform this binding.
948945
// FIXME: this has a super-inefficient extraneous simplifyType() in it.
949-
if (auto boundType = checkTypeOfBinding(typeVar, type)) {
946+
if (auto boundType = CS.checkTypeOfBinding(TypeVar, type)) {
950947
type = *boundType;
951948
if (type->hasTypeVariable()) {
952949
SmallVector<TypeVariableType *, 4> referencedVars;
953950
type->getTypeVariables(referencedVars);
954-
result.AdjacentVars.insert(referencedVars.begin(), referencedVars.end());
951+
AdjacentVars.insert(referencedVars.begin(), referencedVars.end());
955952
}
956953
} else {
957954
auto *bindingTypeVar = type->getRValueType()->getAs<TypeVariableType>();
958955

959956
if (!bindingTypeVar)
960957
return None;
961958

962-
result.AdjacentVars.insert(bindingTypeVar);
959+
AdjacentVars.insert(bindingTypeVar);
963960

964961
// If current type variable is associated with a code completion token
965962
// it's possible that it doesn't have enough contextual information
@@ -968,7 +965,7 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
968965
// available.
969966
if (auto *locator = bindingTypeVar->getImpl().getLocator()) {
970967
if (locator->directlyAt<CodeCompletionExpr>())
971-
result.AssociatedCodeCompletionToken = locator->getAnchor();
968+
AssociatedCodeCompletionToken = locator->getAnchor();
972969
}
973970

974971
switch (constraint->getKind()) {
@@ -977,18 +974,18 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
977974
case ConstraintKind::ArgumentConversion:
978975
case ConstraintKind::OperatorArgumentConversion: {
979976
if (kind == AllowedBindingKind::Subtypes) {
980-
result.SubtypeOf.insert({bindingTypeVar, constraint});
977+
SubtypeOf.insert({bindingTypeVar, constraint});
981978
} else {
982979
assert(kind == AllowedBindingKind::Supertypes);
983-
result.SupertypeOf.insert({bindingTypeVar, constraint});
980+
SupertypeOf.insert({bindingTypeVar, constraint});
984981
}
985982
break;
986983
}
987984

988985
case ConstraintKind::Bind:
989986
case ConstraintKind::BindParam:
990987
case ConstraintKind::Equal: {
991-
result.EquivalentTo.insert({bindingTypeVar, constraint});
988+
EquivalentTo.insert({bindingTypeVar, constraint});
992989
break;
993990
}
994991

@@ -1002,14 +999,14 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
1002999
// Make sure we aren't trying to equate type variables with different
10031000
// lvalue-binding rules.
10041001
if (auto otherTypeVar = type->getAs<TypeVariableType>()) {
1005-
if (typeVar->getImpl().canBindToLValue() !=
1002+
if (TypeVar->getImpl().canBindToLValue() !=
10061003
otherTypeVar->getImpl().canBindToLValue())
10071004
return None;
10081005
}
10091006

1010-
if (type->is<InOutType>() && !typeVar->getImpl().canBindToInOut())
1007+
if (type->is<InOutType>() && !TypeVar->getImpl().canBindToInOut())
10111008
type = LValueType::get(type->getInOutObjectType());
1012-
if (type->is<LValueType>() && !typeVar->getImpl().canBindToLValue())
1009+
if (type->is<LValueType>() && !TypeVar->getImpl().canBindToLValue())
10131010
type = type->getRValueType();
10141011

10151012
// BindParam constraints are not reflexive and must be treated specially.
@@ -1043,8 +1040,7 @@ bool PotentialBindings::infer(Constraint *constraint) {
10431040
case ConstraintKind::ArgumentConversion:
10441041
case ConstraintKind::OperatorArgumentConversion:
10451042
case ConstraintKind::OptionalObject: {
1046-
auto binding =
1047-
CS.getPotentialBindingForRelationalConstraint(*this, constraint);
1043+
auto binding = inferFromRelational(constraint);
10481044
if (!binding)
10491045
break;
10501046

0 commit comments

Comments
 (0)