Skip to content

Commit c8fad86

Browse files
committed
[ConstraintSolver] NFC: Refactor PontentialBindings to reference type variable they belong to
1 parent 3792ada commit c8fad86

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
using namespace swift;
2121
using namespace constraints;
2222

23-
std::pair<ConstraintSystem::PotentialBindings, TypeVariableType *>
23+
Optional<ConstraintSystem::PotentialBindings>
2424
ConstraintSystem::determineBestBindings() {
2525
// Look for potential type variable bindings.
26-
TypeVariableType *bestTypeVar = nullptr;
27-
PotentialBindings bestBindings;
26+
Optional<PotentialBindings> bestBindings;
2827
for (auto typeVar : getTypeVariables()) {
2928
// Skip any type variables that are bound.
3029
if (typeVar->getImpl().hasRepresentativeOrFixed())
@@ -42,13 +41,11 @@ ConstraintSystem::determineBestBindings() {
4241

4342
// If these are the first bindings, or they are better than what
4443
// we saw before, use them instead.
45-
if (!bestTypeVar || bindings < bestBindings) {
44+
if (!bestBindings || bindings < *bestBindings)
4645
bestBindings = std::move(bindings);
47-
bestTypeVar = typeVar;
48-
}
4946
}
5047

51-
return std::make_pair(bestBindings, bestTypeVar);
48+
return bestBindings;
5249
}
5350

5451
/// Find the set of type variables that are inferable from the given type.
@@ -141,7 +138,7 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
141138
getConstraintGraph().gatherConstraints(
142139
typeVar, constraints, ConstraintGraph::GatheringKind::EquivalenceClass);
143140

144-
PotentialBindings result;
141+
PotentialBindings result(typeVar);
145142

146143
// Consider each of the constraints related to this type variable.
147144
llvm::SmallPtrSet<CanType, 4> exactTypes;
@@ -196,7 +193,7 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
196193
auto dynamicType = constraint->getFirstType();
197194
if (auto *tv = dynamicType->getAs<TypeVariableType>()) {
198195
if (tv->getImpl().getRepresentative(nullptr) == typeVar)
199-
return {};
196+
return {typeVar};
200197
}
201198

202199
// This is right-hand side, let's continue.

lib/Sema/CSSolver.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,17 +1783,15 @@ bool ConstraintSystem::solveSimplified(
17831783
Constraint *disjunction, SmallVectorImpl<Solution> &solutions,
17841784
FreeTypeVariableBinding allowFreeTypeVariables) {
17851785

1786-
TypeVariableType *bestTypeVar = nullptr;
1787-
PotentialBindings bestBindings;
1788-
std::tie(bestBindings, bestTypeVar) = determineBestBindings();
1786+
auto bestBindings = determineBestBindings();
17891787

17901788
// If we have a binding that does not involve type variables, and is
17911789
// not fully bound, or we have no disjunction to attempt instead,
17921790
// go ahead and try the bindings for this type variable.
1793-
if (bestBindings && (!disjunction || (!bestBindings.InvolvesTypeVariables &&
1794-
!bestBindings.FullyBound))) {
1795-
return tryTypeVariableBindings(solverState->depth, bestTypeVar,
1796-
bestBindings.Bindings, solutions,
1791+
if (bestBindings && (!disjunction || (!bestBindings->InvolvesTypeVariables &&
1792+
!bestBindings->FullyBound))) {
1793+
return tryTypeVariableBindings(solverState->depth, bestBindings->TypeVar,
1794+
bestBindings->Bindings, solutions,
17971795
allowFreeTypeVariables);
17981796
}
17991797

lib/Sema/ConstraintSystem.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2554,6 +2554,8 @@ class ConstraintSystem {
25542554
typedef std::tuple<bool, bool, bool, bool, bool,
25552555
unsigned char, unsigned int> BindingScore;
25562556

2557+
TypeVariableType *TypeVar;
2558+
25572559
/// The set of potential bindings.
25582560
SmallVector<PotentialBinding, 4> Bindings;
25592561

@@ -2578,6 +2580,8 @@ class ConstraintSystem {
25782580
/// Tracks the position of the last known supertype in the group.
25792581
Optional<unsigned> lastSupertypeIndex;
25802582

2583+
PotentialBindings(TypeVariableType *typeVar) : TypeVar(typeVar) {}
2584+
25812585
/// Determine whether the set of bindings is non-empty.
25822586
explicit operator bool() const { return !Bindings.empty(); }
25832587

@@ -2717,7 +2721,7 @@ class ConstraintSystem {
27172721

27182722
Optional<Type> checkTypeOfBinding(TypeVariableType *typeVar, Type type,
27192723
bool *isNilLiteral = nullptr);
2720-
std::pair<PotentialBindings, TypeVariableType *> determineBestBindings();
2724+
Optional<PotentialBindings> determineBestBindings();
27212725
PotentialBindings getPotentialBindings(TypeVariableType *typeVar);
27222726

27232727
bool

0 commit comments

Comments
 (0)