Skip to content

Commit 21e787b

Browse files
committed
[Sema] Walk SyntacticElementTarget for completion
Instead of walking the single ASTNode from the target, walk all AST nodes associated with the target to find the completion expr. This is needed to find the completion expr in a pattern for an initialization target.
1 parent 2a3b15c commit 21e787b

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

include/swift/Sema/CompletionContextFinder.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
namespace swift {
2222

23+
namespace constraints {
24+
class SyntacticElementTarget;
25+
}
26+
2327
class CompletionContextFinder : public ASTWalker {
2428
enum class ContextKind {
2529
FallbackExpression,
@@ -53,12 +57,9 @@ class CompletionContextFinder : public ASTWalker {
5357
return MacroWalking::Arguments;
5458
}
5559

56-
/// Finder for completion contexts within the provided initial expression.
57-
CompletionContextFinder(ASTNode initialNode, DeclContext *DC)
58-
: InitialExpr(initialNode.dyn_cast<Expr *>()), InitialDC(DC) {
59-
assert(DC);
60-
initialNode.walk(*this);
61-
};
60+
/// Finder for completion contexts within the provided SyntacticElementTarget.
61+
CompletionContextFinder(constraints::SyntacticElementTarget target,
62+
DeclContext *DC);
6263

6364
/// Finder for completion contexts within the outermost non-closure context of
6465
/// the code completion expression's direct context.

lib/Sema/BuilderTransform.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,8 @@ Optional<BraceStmt *> TypeChecker::applyResultBuilderBodyTransform(
984984
SmallVector<Solution, 4> solutions;
985985
cs.solveForCodeCompletion(solutions);
986986

987-
CompletionContextFinder analyzer(func, func->getDeclContext());
987+
SyntacticElementTarget funcTarget(func);
988+
CompletionContextFinder analyzer(funcTarget, func->getDeclContext());
988989
if (analyzer.hasCompletion()) {
989990
filterSolutionsForCodeCompletion(solutions, analyzer);
990991
for (const auto &solution : solutions) {

lib/Sema/CompletionContextFinder.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,19 @@
1212

1313
#include "swift/Sema/CompletionContextFinder.h"
1414
#include "swift/Parse/Lexer.h"
15+
#include "swift/Sema/SyntacticElementTarget.h"
1516

1617
using namespace swift;
18+
using namespace constraints;
1719
using Fallback = CompletionContextFinder::Fallback;
1820

21+
CompletionContextFinder::CompletionContextFinder(
22+
SyntacticElementTarget target, DeclContext *DC)
23+
: InitialExpr(target.getAsExpr()), InitialDC(DC) {
24+
assert(DC);
25+
target.walk(*this);
26+
}
27+
1928
ASTWalker::PreWalkResult<Expr *>
2029
CompletionContextFinder::walkToExprPre(Expr *E) {
2130
if (auto *closure = dyn_cast<ClosureExpr>(E)) {

lib/Sema/TypeCheckCodeCompletion.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -574,15 +574,12 @@ bool TypeChecker::typeCheckForCodeCompletion(
574574
return false;
575575
}
576576

577-
auto node = target.getAsASTNode();
578-
if (!node)
579-
return false;
580-
581-
if (auto *expr = getAsExpr(node)) {
582-
node = expr->walk(SanitizeExpr(Context));
577+
if (getAsExpr(target.getAsASTNode())) {
578+
SanitizeExpr sanitizer(Context);
579+
target = *target.walk(sanitizer);
583580
}
584581

585-
CompletionContextFinder contextAnalyzer(node, DC);
582+
CompletionContextFinder contextAnalyzer(target, DC);
586583

587584
// If there was no completion expr (e.g. if the code completion location was
588585
// among tokens that were skipped over during parser error recovery) bail.

0 commit comments

Comments
 (0)