Skip to content

[ConstraintSolver] When ranking bindings prefer type vars with literal bindings #11966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
using namespace swift;
using namespace constraints;

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

// If these are the first bindings, or they are better than what
// we saw before, use them instead.
if (!bestTypeVar || bindings < bestBindings) {
if (!bestBindings || bindings < *bestBindings)
bestBindings = std::move(bindings);
bestTypeVar = typeVar;
}
}

return std::make_pair(bestBindings, bestTypeVar);
return bestBindings;
}

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

PotentialBindings result;
PotentialBindings result(typeVar);

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

// This is right-hand side, let's continue.
Expand Down
20 changes: 7 additions & 13 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1783,21 +1783,15 @@ bool ConstraintSystem::solveSimplified(
Constraint *disjunction, SmallVectorImpl<Solution> &solutions,
FreeTypeVariableBinding allowFreeTypeVariables) {

TypeVariableType *bestTypeVar = nullptr;
PotentialBindings bestBindings;
std::tie(bestBindings, bestTypeVar) = determineBestBindings();
auto bestBindings = determineBestBindings();

// If we have a binding that does not involve type variables, and is
// not fully bound, and is either not a literal or is a collection
// literal, or we have no disjunction to attempt instead, go ahead
// and try the bindings for this type variable.
if (bestBindings &&
(!disjunction ||
(!bestBindings.InvolvesTypeVariables && !bestBindings.FullyBound &&
(bestBindings.LiteralBinding == LiteralBindingKind::None ||
bestBindings.LiteralBinding == LiteralBindingKind::Collection)))) {
return tryTypeVariableBindings(solverState->depth, bestTypeVar,
bestBindings.Bindings, solutions,
// not fully bound, or we have no disjunction to attempt instead,
// go ahead and try the bindings for this type variable.
if (bestBindings && (!disjunction || (!bestBindings->InvolvesTypeVariables &&
!bestBindings->FullyBound))) {
return tryTypeVariableBindings(solverState->depth, bestBindings->TypeVar,
bestBindings->Bindings, solutions,
allowFreeTypeVariables);
}

Expand Down
12 changes: 8 additions & 4 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2551,8 +2551,10 @@ class ConstraintSystem {
};

struct PotentialBindings {
typedef std::tuple<bool, bool, bool, bool, unsigned char,
bool, unsigned int> BindingScore;
typedef std::tuple<bool, bool, bool, bool, bool,
unsigned char, unsigned int> BindingScore;

TypeVariableType *TypeVar;

/// The set of potential bindings.
SmallVector<PotentialBinding, 4> Bindings;
Expand All @@ -2578,6 +2580,8 @@ class ConstraintSystem {
/// Tracks the position of the last known supertype in the group.
Optional<unsigned> lastSupertypeIndex;

PotentialBindings(TypeVariableType *typeVar) : TypeVar(typeVar) {}

/// Determine whether the set of bindings is non-empty.
explicit operator bool() const { return !Bindings.empty(); }

Expand All @@ -2591,8 +2595,8 @@ class ConstraintSystem {
b.FullyBound,
b.IsRHSOfBindParam,
b.SubtypeOfExistentialType,
static_cast<unsigned char>(b.LiteralBinding),
b.InvolvesTypeVariables,
static_cast<unsigned char>(b.LiteralBinding),
-(b.Bindings.size() - b.NumDefaultableBindings));
}

Expand Down Expand Up @@ -2717,7 +2721,7 @@ class ConstraintSystem {

Optional<Type> checkTypeOfBinding(TypeVariableType *typeVar, Type type,
bool *isNilLiteral = nullptr);
std::pair<PotentialBindings, TypeVariableType *> determineBestBindings();
Optional<PotentialBindings> determineBestBindings();
PotentialBindings getPotentialBindings(TypeVariableType *typeVar);

bool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %scale-test --invert-result --begin 2 --end 7 --step 1 --select incrementScopeCounter %s
// RUN: %scale-test --begin 1 --end 10 --step 1 --select incrementScopeCounter %s
// REQUIRES: OS=macosx
// REQUIRES: asserts

Expand Down