Skip to content

Commit b03e989

Browse files
committed
[CS] NFC: Factor out shouldIgnoreHoleForCodeCompletion
1 parent c35d65e commit b03e989

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2879,6 +2879,40 @@ TypeVariableBinding::fixForHole(ConstraintSystem &cs) const {
28792879
return std::nullopt;
28802880
}
28812881

2882+
static bool shouldIgnoreHoleForCodeCompletion(ConstraintSystem &cs,
2883+
TypeVariableType *typeVar,
2884+
ConstraintLocator *srcLocator) {
2885+
if (!cs.isForCodeCompletion())
2886+
return false;
2887+
2888+
// Don't penalize solutions with unresolved generics.
2889+
if (typeVar->getImpl().getGenericParameter())
2890+
return true;
2891+
2892+
// Don't penalize solutions if we couldn't determine the type of the code
2893+
// completion token. We still want to examine the surrounding types in
2894+
// that case.
2895+
if (typeVar->getImpl().isCodeCompletionToken())
2896+
return true;
2897+
2898+
// Don't penalize solutions with holes due to missing arguments after the
2899+
// code completion position.
2900+
auto argLoc = srcLocator->findLast<LocatorPathElt::SynthesizedArgument>();
2901+
if (argLoc && argLoc->isAfterCodeCompletionLoc())
2902+
return true;
2903+
2904+
// Don't penalize solutions that have holes for ignored arguments.
2905+
if (cs.hasArgumentsIgnoredForCodeCompletion()) {
2906+
// Avoid simplifying the locator if the constraint system didn't ignore
2907+
// any arguments.
2908+
auto argExpr = simplifyLocatorToAnchor(typeVar->getImpl().getLocator());
2909+
if (cs.isArgumentIgnoredForCodeCompletion(argExpr.dyn_cast<Expr *>())) {
2910+
return true;
2911+
}
2912+
}
2913+
return false;
2914+
}
2915+
28822916
bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
28832917
auto type = Binding.BindingType;
28842918
auto *srcLocator = Binding.getLocator();
@@ -2934,33 +2968,9 @@ bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
29342968
}
29352969

29362970
auto reportHole = [&]() {
2937-
if (cs.isForCodeCompletion()) {
2938-
// Don't penalize solutions with unresolved generics.
2939-
if (TypeVar->getImpl().getGenericParameter())
2940-
return false;
2941-
2942-
// Don't penalize solutions if we couldn't determine the type of the code
2943-
// completion token. We still want to examine the surrounding types in
2944-
// that case.
2945-
if (TypeVar->getImpl().isCodeCompletionToken())
2946-
return false;
2947-
2948-
// Don't penalize solutions with holes due to missing arguments after the
2949-
// code completion position.
2950-
auto argLoc = srcLocator->findLast<LocatorPathElt::SynthesizedArgument>();
2951-
if (argLoc && argLoc->isAfterCodeCompletionLoc())
2952-
return false;
2953-
2954-
// Don't penalize solutions that have holes for ignored arguments.
2955-
if (cs.hasArgumentsIgnoredForCodeCompletion()) {
2956-
// Avoid simplifying the locator if the constraint system didn't ignore
2957-
// any arguments.
2958-
auto argExpr = simplifyLocatorToAnchor(TypeVar->getImpl().getLocator());
2959-
if (cs.isArgumentIgnoredForCodeCompletion(argExpr.dyn_cast<Expr *>())) {
2960-
return false;
2961-
}
2962-
}
2963-
}
2971+
if (shouldIgnoreHoleForCodeCompletion(cs, TypeVar, srcLocator))
2972+
return false;
2973+
29642974
// Reflect in the score that this type variable couldn't be
29652975
// resolved and had to be bound to a placeholder "hole" type.
29662976
cs.increaseScore(SK_Hole, srcLocator);

0 commit comments

Comments
 (0)