Skip to content

Commit 7f228b0

Browse files
committed
[ConstraintSystem] Compute # of defaultable bindings on demand
1 parent 8474332 commit 7f228b0

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4747,9 +4747,6 @@ class ConstraintSystem {
47474747
/// Whether this type variable is only bound above by existential types.
47484748
bool SubtypeOfExistentialType = false;
47494749

4750-
/// The number of defaultable bindings.
4751-
unsigned NumDefaultableBindings = 0;
4752-
47534750
/// Tracks the position of the last known supertype in the group.
47544751
Optional<unsigned> lastSupertypeIndex;
47554752

@@ -4767,35 +4764,45 @@ class ConstraintSystem {
47674764
/// Determine whether the set of bindings is non-empty.
47684765
explicit operator bool() const { return !Bindings.empty(); }
47694766

4770-
/// Whether there are any non-defaultable bindings.
4771-
bool hasNonDefaultableBindings() const {
4772-
return Bindings.size() > NumDefaultableBindings;
4767+
unsigned getNumDefaultableBindings() const {
4768+
return llvm::count_if(Bindings, [](const PotentialBinding &binding) {
4769+
return binding.isDefaultableBinding();
4770+
});
47734771
}
47744772

47754773
static BindingScore formBindingScore(const PotentialBindings &b) {
4774+
auto numDefaults = b.getNumDefaultableBindings();
4775+
auto hasNoDefaultableBindings = b.Bindings.size() > numDefaults;
4776+
47764777
return std::make_tuple(b.IsHole,
4777-
!b.hasNonDefaultableBindings(),
4778+
!hasNoDefaultableBindings,
47784779
b.FullyBound,
47794780
b.SubtypeOfExistentialType,
47804781
b.InvolvesTypeVariables,
47814782
static_cast<unsigned char>(b.LiteralBinding),
4782-
-(b.Bindings.size() - b.NumDefaultableBindings));
4783+
-(b.Bindings.size() - numDefaults));
47834784
}
47844785

47854786
/// Compare two sets of bindings, where \c x < y indicates that
47864787
/// \c x is a better set of bindings that \c y.
47874788
friend bool operator<(const PotentialBindings &x,
47884789
const PotentialBindings &y) {
4789-
if (formBindingScore(x) < formBindingScore(y))
4790+
auto xScore = formBindingScore(x);
4791+
auto yScore = formBindingScore(y);
4792+
4793+
if (xScore < yScore)
47904794
return true;
47914795

4792-
if (formBindingScore(y) < formBindingScore(x))
4796+
if (yScore < xScore)
47934797
return false;
47944798

4799+
auto xDefaults = x.Bindings.size() + std::get<6>(xScore);
4800+
auto yDefaults = y.Bindings.size() + std::get<6>(yScore);
4801+
47954802
// If there is a difference in number of default types,
47964803
// prioritize bindings with fewer of them.
4797-
if (x.NumDefaultableBindings != y.NumDefaultableBindings)
4798-
return x.NumDefaultableBindings < y.NumDefaultableBindings;
4804+
if (xDefaults != yDefaults)
4805+
return xDefaults < yDefaults;
47994806

48004807
// If neither type variable is a "hole" let's check whether
48014808
// there is a subtype relationship between them and prefer
@@ -4924,8 +4931,10 @@ class ConstraintSystem {
49244931
out << "literal=" << static_cast<int>(LiteralBinding) << " ";
49254932
if (InvolvesTypeVariables)
49264933
out << "involves_type_vars ";
4927-
if (NumDefaultableBindings > 0)
4928-
out << "#defaultable_bindings=" << NumDefaultableBindings << " ";
4934+
4935+
auto numDefaultable = getNumDefaultableBindings();
4936+
if (numDefaultable > 0)
4937+
out << "#defaultable_bindings=" << numDefaultable << " ";
49294938

49304939
PrintOptions PO;
49314940
PO.PrintTypesForDebugging = true;

lib/Sema/CSBindings.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,9 +678,6 @@ void ConstraintSystem::PotentialBindings::addPotentialBinding(
678678
if (!isViable(binding))
679679
return;
680680

681-
if (binding.isDefaultableBinding())
682-
++NumDefaultableBindings;
683-
684681
Bindings.push_back(std::move(binding));
685682
}
686683

0 commit comments

Comments
 (0)