Skip to content

Commit 6a7cc79

Browse files
committed
[ConstraintSystem] Determine whether type variable is a hole on demand
1 parent 4ed9794 commit 6a7cc79

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4730,9 +4730,6 @@ class ConstraintSystem {
47304730
/// Whether the bindings of this type involve other type variables.
47314731
bool InvolvesTypeVariables = false;
47324732

4733-
/// Whether this type variable is considered a hole in the constraint system.
4734-
bool IsHole = false;
4735-
47364733
/// Whether the bindings represent (potentially) incomplete set,
47374734
/// there is no way to say with absolute certainty if that's the
47384735
/// case, but that could happen when certain constraints like
@@ -4761,6 +4758,17 @@ class ConstraintSystem {
47614758
/// Determine whether the set of bindings is non-empty.
47624759
explicit operator bool() const { return !Bindings.empty(); }
47634760

4761+
/// If there is only one binding and it's to a hole type, consider
4762+
/// this type variable to be a hole in a constraint system regardless
4763+
/// of where hole type originated.
4764+
bool isHole() const {
4765+
if (Bindings.size() != 1)
4766+
return false;
4767+
4768+
auto &binding = Bindings.front();
4769+
return binding.BindingType->is<HoleType>();
4770+
}
4771+
47644772
/// Determine if the bindings only constrain the type variable from above
47654773
/// with an existential type; such a binding is not very helpful because
47664774
/// it's impossible to enumerate the existential type's subtypes.
@@ -4784,7 +4792,7 @@ class ConstraintSystem {
47844792
auto numDefaults = b.getNumDefaultableBindings();
47854793
auto hasNoDefaultableBindings = b.Bindings.size() > numDefaults;
47864794

4787-
return std::make_tuple(b.IsHole,
4795+
return std::make_tuple(b.isHole(),
47884796
!hasNoDefaultableBindings,
47894797
b.FullyBound,
47904798
b.isSubtypeOfExistentialType(),
@@ -4820,7 +4828,7 @@ class ConstraintSystem {
48204828
// for "subtype" type variable to attempt more bindings later.
48214829
// This is required because algorithm can't currently infer
48224830
// bindings for subtype transitively through superclass ones.
4823-
if (!(x.IsHole && y.IsHole)) {
4831+
if (!(std::get<0>(xScore) && std::get<0>(yScore))) {
48244832
if (x.isSubtypeOf(y.TypeVar))
48254833
return false;
48264834

lib/Sema/CSBindings.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ void ConstraintSystem::PotentialBindings::finalize(
462462
// If there are no bindings, typeVar may be a hole.
463463
if (cs.shouldAttemptFixes() && Bindings.empty() &&
464464
TypeVar->getImpl().canBindToHole()) {
465-
IsHole = true;
466465
// If the base of the unresolved member reference like `.foo`
467466
// couldn't be resolved we'd want to bind it to a hole at the
468467
// very last moment possible, just like generic parameters.
@@ -695,7 +694,7 @@ bool ConstraintSystem::PotentialBindings::isViable(
695694

696695
bool ConstraintSystem::PotentialBindings::favoredOverDisjunction(
697696
Constraint *disjunction) const {
698-
if (IsHole || FullyBound)
697+
if (isHole() || FullyBound)
699698
return false;
700699

701700
// If this bindings are for a closure and there are no holes,

lib/Sema/ConstraintGraph.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ bool ConstraintGraph::contractEdges() {
11131113
bool isNotContractable = true;
11141114
if (auto bindings = CS.inferBindingsFor(tyvar1)) {
11151115
// Holes can't be contracted.
1116-
if (bindings.IsHole)
1116+
if (bindings.isHole())
11171117
continue;
11181118

11191119
for (auto &binding : bindings.Bindings) {

0 commit comments

Comments
 (0)