Skip to content

Commit 86ddd52

Browse files
author
Nathan Hawes
committed
[Sema] Extract out expression-contains-completion-loc check into a method on ConstraintSystem (NFC)
1 parent c777b25 commit 86ddd52

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2845,6 +2845,10 @@ class ConstraintSystem {
28452845
return TypeVariables.count(typeVar) > 0;
28462846
}
28472847

2848+
/// Whether the given expression's source range contains the code
2849+
/// completion location.
2850+
bool containsCodeCompletionLoc(Expr *expr) const;
2851+
28482852
void setClosureType(const ClosureExpr *closure, FunctionType *type) {
28492853
assert(closure);
28502854
assert(type && "Expected non-null type");

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,9 +1019,7 @@ namespace {
10191019
// ErrorExpr's OriginalExpr (valid sub-expression) if it had one,
10201020
// independent of the wider expression containing the ErrorExpr, so
10211021
// there's no point attempting to produce a solution for it.
1022-
SourceRange range = E->getSourceRange();
1023-
if (range.isValid() &&
1024-
CS.getASTContext().SourceMgr.rangeContainsCodeCompletionLoc(range))
1022+
if (CS.containsCodeCompletionLoc(E))
10251023
return nullptr;
10261024

10271025
return HoleType::get(CS.getASTContext(), E);

lib/Sema/CSSimplify.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ constraints::matchCallArguments(
986986
}
987987

988988
static Optional<unsigned>
989-
getCompletionArgIndex(ASTNode anchor, SourceManager &SM) {
989+
getCompletionArgIndex(ASTNode anchor, ConstraintSystem &CS) {
990990
Expr *arg = nullptr;
991991
if (auto *CE = getAsExpr<CallExpr>(anchor))
992992
arg = CE->getArg();
@@ -998,20 +998,15 @@ getCompletionArgIndex(ASTNode anchor, SourceManager &SM) {
998998
if (!arg)
999999
return None;
10001000

1001-
auto containsCompletion = [&](Expr *elem) {
1002-
if (!elem)
1003-
return false;
1004-
SourceRange range = elem->getSourceRange();
1005-
return range.isValid() && SM.rangeContainsCodeCompletionLoc(range);
1006-
};
1007-
10081001
if (auto *TE = dyn_cast<TupleExpr>(arg)) {
10091002
auto elems = TE->getElements();
1010-
auto idx = llvm::find_if(elems, containsCompletion);
1003+
auto idx = llvm::find_if(elems, [&](Expr *elem) {
1004+
return CS.containsCodeCompletionLoc(elem);
1005+
});
10111006
if (idx != elems.end())
10121007
return std::distance(elems.begin(), idx);
10131008
} else if (auto *PE = dyn_cast<ParenExpr>(arg)) {
1014-
if (containsCompletion(PE->getSubExpr()))
1009+
if (CS.containsCodeCompletionLoc(PE->getSubExpr()))
10151010
return 0;
10161011
}
10171012
return None;
@@ -1069,9 +1064,8 @@ class ArgumentFailureTracker : public MatchCallArgumentListener {
10691064
// completion location, later arguments shouldn't be considered missing
10701065
// (causing the solution to have a worse score) as the user just hasn't
10711066
// written them yet. Early exit to avoid recording them in this case.
1072-
SourceManager &SM = CS.getASTContext().SourceMgr;
10731067
if (!CompletionArgIdx)
1074-
CompletionArgIdx = getCompletionArgIndex(Locator.getAnchor(), SM);
1068+
CompletionArgIdx = getCompletionArgIndex(Locator.getAnchor(), CS);
10751069
if (CompletionArgIdx && *CompletionArgIdx < argInsertIdx)
10761070
return newArgIdx;
10771071
}
@@ -1817,9 +1811,7 @@ static bool fixMissingArguments(ConstraintSystem &cs, ASTNode anchor,
18171811
// code completion location, since they may have just not been written yet.
18181812
if (cs.isForCodeCompletion()) {
18191813
if (auto *closure = getAsExpr<ClosureExpr>(anchor)) {
1820-
SourceManager &SM = closure->getASTContext().SourceMgr;
1821-
SourceRange range = closure->getSourceRange();
1822-
if (range.isValid() && SM.rangeContainsCodeCompletionLoc(range) &&
1814+
if (cs.containsCodeCompletionLoc(closure) &&
18231815
(closure->hasAnonymousClosureVars() ||
18241816
(args.empty() && closure->getInLoc().isInvalid())))
18251817
return false;
@@ -3812,9 +3804,7 @@ bool ConstraintSystem::repairFailures(
38123804
// other (explicit) argument's so source range containment alone isn't
38133805
// sufficient.
38143806
bool isSynthesizedArg = arg->isImplicit() && isa<DeclRefExpr>(arg);
3815-
SourceRange range = arg->getSourceRange();
3816-
if (!isSynthesizedArg && range.isValid() &&
3817-
Context.SourceMgr.rangeContainsCodeCompletionLoc(range) &&
3807+
if (!isSynthesizedArg && containsCodeCompletionLoc(arg) &&
38183808
!lhs->isVoid() && !lhs->isUninhabited())
38193809
return true;
38203810
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,13 @@ getAlternativeLiteralTypes(KnownProtocolKind kind) {
378378
return *AlternativeLiteralTypes[index];
379379
}
380380

381+
bool ConstraintSystem::containsCodeCompletionLoc(Expr *expr) const {
382+
SourceRange range = expr->getSourceRange();
383+
if (range.isInvalid())
384+
return false;
385+
return Context.SourceMgr.rangeContainsCodeCompletionLoc(range);
386+
}
387+
381388
ConstraintLocator *ConstraintSystem::getConstraintLocator(
382389
ASTNode anchor, ArrayRef<ConstraintLocator::PathElement> path) {
383390
auto summaryFlags = ConstraintLocator::getSummaryFlagsForPath(path);

0 commit comments

Comments
 (0)