Skip to content

Commit c220272

Browse files
committed
[CSBindings] Infer any type variable connected to a code completion token as a potential hole
If type variable is associated with a code completion token it's possible that it doesn't have enough contextual information to be resolved to anything, so let's add a hole type which originates from type variable associated with code completion expression to make this relationship explicit and avoid "fixing" problems rooted in fact that type variable is underconstrained due to code completion.
1 parent 427c647 commit c220272

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,15 @@ void ConstraintSystem::PotentialBindings::finalize(
377377
PotentiallyIncomplete = true;
378378
}
379379

380+
// If this type variable is associated with a code completion token
381+
// and it failed to infer any bindings let's adjust hole's locator
382+
// to point to a code completion token to avoid attempting to "fix"
383+
// this problem since its rooted in the fact that constraint system
384+
// is under-constrained.
385+
if (AssociatedCodeCompletionToken) {
386+
locator = cs.getConstraintLocator(AssociatedCodeCompletionToken);
387+
}
388+
380389
addPotentialBinding(PotentialBinding::forHole(TypeVar, locator));
381390
}
382391

@@ -768,6 +777,18 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
768777

769778
result.InvolvesTypeVariables = true;
770779

780+
// If current type variable is associated with a code completion token
781+
// it's possible that it doesn't have enough contextual information
782+
// to be resolved to anything, so let's note that fact in the potential
783+
// bindings and use it when forming a hole if there are no other bindings
784+
// available.
785+
if (auto *locator = bindingTypeVar->getImpl().getLocator()) {
786+
if (locator->directlyAt<CodeCompletionExpr>()) {
787+
result.AssociatedCodeCompletionToken = locator->getAnchor();
788+
result.PotentiallyIncomplete = true;
789+
}
790+
}
791+
771792
if (constraint->getKind() == ConstraintKind::Subtype &&
772793
kind == AllowedBindingKind::Subtypes) {
773794
result.SubtypeOf.insert(bindingTypeVar);

lib/Sema/ConstraintSystem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4644,6 +4644,8 @@ class ConstraintSystem {
46444644
/// `bind param` are present in the system.
46454645
bool PotentiallyIncomplete = false;
46464646

4647+
ASTNode AssociatedCodeCompletionToken = ASTNode();
4648+
46474649
/// Whether this type variable has literal bindings.
46484650
LiteralBindingKind LiteralBinding = LiteralBindingKind::None;
46494651

0 commit comments

Comments
 (0)