Skip to content

Commit 81dc4dc

Browse files
authored
Merge pull request #41883 from ahoppen/pr/solver-based-completion-improvements
[CodeCompletion] Small improvements to solver-based code completion
2 parents e6308ed + 82fc059 commit 81dc4dc

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,7 +2415,7 @@ class ConstraintSystem {
24152415

24162416
/// Arguments after the code completion token that were thus ignored (i.e.
24172417
/// assigned fresh type variables) for type checking.
2418-
llvm::SetVector<ConstraintLocator *> IgnoredArguments;
2418+
llvm::SetVector<Expr *> IgnoredArguments;
24192419

24202420
/// Maps node types used within all portions of the constraint
24212421
/// system, instead of directly using the types on the
@@ -3187,18 +3187,25 @@ class ConstraintSystem {
31873187
bool containsCodeCompletionLoc(ASTNode node) const;
31883188
bool containsCodeCompletionLoc(const ArgumentList *args) const;
31893189

3190-
/// Marks the argument with the \p ArgLoc locator as being ignored because it
3191-
/// occurs after the code completion token. This assumes that the argument is
3192-
/// not type checked (by assigning it a fresh type variable) and prevents
3193-
/// fixes from being generated for this argument.
3194-
void markArgumentIgnoredForCodeCompletion(ConstraintLocator *ArgLoc) {
3195-
IgnoredArguments.insert(ArgLoc);
3190+
/// Marks the argument \p Arg as being ignored because it occurs after the
3191+
/// code completion token. This assumes that the argument is not type checked
3192+
/// (by assigning it a fresh type variable) and prevents fixes from being
3193+
/// generated for this argument.
3194+
void markArgumentIgnoredForCodeCompletion(Expr *Arg) {
3195+
IgnoredArguments.insert(Arg);
31963196
}
31973197

3198-
/// Whether the argument with the \p ArgLoc locator occurs after the code
3199-
/// completion tokena and thus should be ignored and not generate any fixes.
3200-
bool isArgumentIgnoredForCodeCompletion(ConstraintLocator *ArgLoc) {
3201-
return IgnoredArguments.count(ArgLoc) > 0;
3198+
/// Whether the argument \p Arg occurs after the code completion token and
3199+
/// thus should be ignored and not generate any fixes.
3200+
bool isArgumentIgnoredForCodeCompletion(Expr *Arg) const {
3201+
return IgnoredArguments.count(Arg) > 0;
3202+
}
3203+
3204+
/// Whether the constraint system has ignored any arguments for code
3205+
/// completion, i.e. whether there is an expression for which
3206+
/// \c isArgumentIgnoredForCodeCompletion returns \c true.
3207+
bool hasArgumentsIgnoredForCodeCompletion() const {
3208+
return !IgnoredArguments.empty();
32023209
}
32033210

32043211
void setClosureType(const ClosureExpr *closure, FunctionType *type) {

lib/Sema/BuilderTransform.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,7 @@ Optional<BraceStmt *> TypeChecker::applyResultBuilderBodyTransform(
17441744
for (const auto &solution : solutions) {
17451745
cs.getASTContext().CompletionCallback->sawSolution(solution);
17461746
}
1747+
return nullptr;
17471748
}
17481749

17491750
if (solvingFailed || solutions.size() != 1) {

lib/Sema/CSBindings.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,9 +2024,13 @@ bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {
20242024
return false;
20252025

20262026
// Don't penailze solutions that have holes for ignored arguments.
2027-
if (cs.isArgumentIgnoredForCodeCompletion(
2028-
TypeVar->getImpl().getLocator())) {
2029-
return false;
2027+
if (cs.hasArgumentsIgnoredForCodeCompletion()) {
2028+
// Avoid simplifying the locator if the constriant system didn't ignore
2029+
// any arguments.
2030+
auto argExpr = simplifyLocatorToAnchor(TypeVar->getImpl().getLocator());
2031+
if (cs.isArgumentIgnoredForCodeCompletion(argExpr.dyn_cast<Expr *>())) {
2032+
return false;
2033+
}
20302034
}
20312035
}
20322036
// Reflect in the score that this type variable couldn't be

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -788,8 +788,7 @@ namespace {
788788
CS(cs) {}
789789

790790
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
791-
if (CS.isArgumentIgnoredForCodeCompletion(
792-
CS.getConstraintLocator(expr))) {
791+
if (CS.isArgumentIgnoredForCodeCompletion(expr)) {
793792
return {false, expr};
794793
}
795794

@@ -3647,8 +3646,7 @@ namespace {
36473646
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
36483647
auto &CS = CG.getConstraintSystem();
36493648

3650-
if (CS.isArgumentIgnoredForCodeCompletion(
3651-
CS.getConstraintLocator(expr))) {
3649+
if (CS.isArgumentIgnoredForCodeCompletion(expr)) {
36523650
CG.setTypeForArgumentIgnoredForCompletion(expr);
36533651
return {false, expr};
36543652
}
@@ -3723,8 +3721,7 @@ namespace {
37233721
SmallVector<Expr *, 2> ignoredArgs;
37243722
getArgumentsAfterCodeCompletionToken(expr, CS, ignoredArgs);
37253723
for (auto ignoredArg : ignoredArgs) {
3726-
CS.markArgumentIgnoredForCodeCompletion(
3727-
CS.getConstraintLocator(ignoredArg));
3724+
CS.markArgumentIgnoredForCodeCompletion(ignoredArg);
37283725
}
37293726
}
37303727

lib/Sema/CSSimplify.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12470,9 +12470,14 @@ bool ConstraintSystem::recordFix(ConstraintFix *fix, unsigned impact) {
1247012470
log << ")\n";
1247112471
}
1247212472

12473-
if (isArgumentIgnoredForCodeCompletion(fix->getLocator())) {
12474-
// The argument was ignored. Don't record any fixes for it.
12475-
return false;
12473+
if (hasArgumentsIgnoredForCodeCompletion()) {
12474+
// Avoid simplifying the locator if the constraint system didn't ignore any
12475+
// arguments.
12476+
auto argExpr = simplifyLocatorToAnchor(fix->getLocator());
12477+
if (isArgumentIgnoredForCodeCompletion(getAsExpr<Expr>(argExpr))) {
12478+
// The argument was ignored. Don't record any fixes for it.
12479+
return false;
12480+
}
1247612481
}
1247712482

1247812483
// Record the fix.

0 commit comments

Comments
 (0)