Skip to content

Commit 29e69e2

Browse files
authored
Merge pull request #11966 from xedin/prefer-literal-bindings-over-typevars
2 parents bdc5afd + c8fad86 commit 29e69e2

File tree

4 files changed

+22
-27
lines changed

4 files changed

+22
-27
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: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,21 +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
1791-
// not fully bound, and is either not a literal or is a collection
1792-
// literal, or we have no disjunction to attempt instead, go ahead
1793-
// and try the bindings for this type variable.
1794-
if (bestBindings &&
1795-
(!disjunction ||
1796-
(!bestBindings.InvolvesTypeVariables && !bestBindings.FullyBound &&
1797-
(bestBindings.LiteralBinding == LiteralBindingKind::None ||
1798-
bestBindings.LiteralBinding == LiteralBindingKind::Collection)))) {
1799-
return tryTypeVariableBindings(solverState->depth, bestTypeVar,
1800-
bestBindings.Bindings, solutions,
1789+
// not fully bound, or we have no disjunction to attempt instead,
1790+
// go ahead and try the bindings for this type variable.
1791+
if (bestBindings && (!disjunction || (!bestBindings->InvolvesTypeVariables &&
1792+
!bestBindings->FullyBound))) {
1793+
return tryTypeVariableBindings(solverState->depth, bestBindings->TypeVar,
1794+
bestBindings->Bindings, solutions,
18011795
allowFreeTypeVariables);
18021796
}
18031797

lib/Sema/ConstraintSystem.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,8 +2551,10 @@ class ConstraintSystem {
25512551
};
25522552

25532553
struct PotentialBindings {
2554-
typedef std::tuple<bool, bool, bool, bool, unsigned char,
2555-
bool, unsigned int> BindingScore;
2554+
typedef std::tuple<bool, bool, bool, bool, bool,
2555+
unsigned char, unsigned int> BindingScore;
2556+
2557+
TypeVariableType *TypeVar;
25562558

25572559
/// The set of potential bindings.
25582560
SmallVector<PotentialBinding, 4> Bindings;
@@ -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

@@ -2591,8 +2595,8 @@ class ConstraintSystem {
25912595
b.FullyBound,
25922596
b.IsRHSOfBindParam,
25932597
b.SubtypeOfExistentialType,
2594-
static_cast<unsigned char>(b.LiteralBinding),
25952598
b.InvolvesTypeVariables,
2599+
static_cast<unsigned char>(b.LiteralBinding),
25962600
-(b.Bindings.size() - b.NumDefaultableBindings));
25972601
}
25982602

@@ -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

validation-test/Sema/type_checker_perf/slow/rdar21720888.swift.gyb renamed to validation-test/Sema/type_checker_perf/fast/rdar21720888.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %scale-test --invert-result --begin 2 --end 7 --step 1 --select incrementScopeCounter %s
1+
// RUN: %scale-test --begin 1 --end 10 --step 1 --select incrementScopeCounter %s
22
// REQUIRES: OS=macosx
33
// REQUIRES: asserts
44

0 commit comments

Comments
 (0)