Skip to content

Commit 9456fa1

Browse files
authored
Merge pull request #41922 from ahoppen/pr/migrage-accessorbeginning-solver-based
[CodeCompletion] Migrate AccessorBeginning to solver-based
2 parents 3505175 + 8cd5bbd commit 9456fa1

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,20 +1397,31 @@ bool CodeCompletionCallbacksImpl::trySolverCompletion(bool MaybeFuncBody) {
13971397
CurDeclContext, CompletionContext, Consumer);
13981398
return true;
13991399
}
1400-
case CompletionKind::StmtOrExpr:
1400+
case CompletionKind::AccessorBeginning:
14011401
case CompletionKind::ForEachSequence:
1402-
case CompletionKind::PostfixExprBeginning: {
1403-
assert(CodeCompleteTokenExpr);
1402+
case CompletionKind::PostfixExprBeginning:
1403+
case CompletionKind::StmtOrExpr: {
14041404
assert(CurDeclContext);
14051405

14061406
ExprTypeCheckCompletionCallback Lookup(CodeCompleteTokenExpr,
14071407
CurDeclContext);
1408-
llvm::SaveAndRestore<TypeCheckCompletionCallback *> CompletionCollector(
1409-
Context.CompletionCallback, &Lookup);
1410-
typeCheckContextAt(CurDeclContext, CompletionLoc);
1411-
1412-
if (!Lookup.gotCallback()) {
1413-
Lookup.fallbackTypeCheck(CurDeclContext);
1408+
if (CodeCompleteTokenExpr) {
1409+
// 'CodeCompletionTokenExpr == nullptr' happens when completing e.g.
1410+
// var x: Int {
1411+
// get { ... }
1412+
// #^COMPLETE^#
1413+
// }
1414+
// In this case we don't want to provide any expression results. We still
1415+
// need to have a TypeCheckCompletionCallback so we can call
1416+
// deliverResults on it to deliver the keyword results from the completion
1417+
// context's result sink to the consumer.
1418+
llvm::SaveAndRestore<TypeCheckCompletionCallback *> CompletionCollector(
1419+
Context.CompletionCallback, &Lookup);
1420+
typeCheckContextAt(CurDeclContext, CompletionLoc);
1421+
1422+
if (!Lookup.gotCallback()) {
1423+
Lookup.fallbackTypeCheck(CurDeclContext);
1424+
}
14141425
}
14151426

14161427
addKeywords(CompletionContext.getResultSink(), MaybeFuncBody);
@@ -1564,6 +1575,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
15641575
case CompletionKind::ForEachSequence:
15651576
case CompletionKind::PostfixExprBeginning:
15661577
case CompletionKind::AfterPoundExpr:
1578+
case CompletionKind::AccessorBeginning:
15671579
llvm_unreachable("should be already handled");
15681580
return;
15691581

@@ -1680,16 +1692,6 @@ void CodeCompletionCallbacksImpl::doneParsing() {
16801692
break;
16811693
}
16821694

1683-
case CompletionKind::AccessorBeginning: {
1684-
if (isa<AccessorDecl>(ParsedDecl)) {
1685-
ExprContextInfo ContextInfo(CurDeclContext, CodeCompleteTokenExpr);
1686-
Lookup.setExpectedTypes(ContextInfo.getPossibleTypes(),
1687-
ContextInfo.isImplicitSingleExpressionReturn());
1688-
DoPostfixExprBeginning();
1689-
}
1690-
break;
1691-
}
1692-
16931695
case CompletionKind::AttributeBegin: {
16941696
Lookup.getAttributeDeclCompletions(IsInSil, AttTargetDK);
16951697

lib/Parse/ParseDecl.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7377,12 +7377,16 @@ Parser::parseAbstractFunctionBodyImpl(AbstractFunctionDecl *AFD) {
73777377
SourceLoc LBraceLoc, RBraceLoc;
73787378
LBraceLoc = consumeToken(tok::l_brace);
73797379
auto *CCE = new (Context) CodeCompletionExpr(Tok.getLoc());
7380+
auto *Return =
7381+
new (Context) ReturnStmt(SourceLoc(), CCE, /*implicit=*/true);
73807382
CodeCompletion->setParsedDecl(accessor);
73817383
CodeCompletion->completeAccessorBeginning(CCE);
73827384
RBraceLoc = Tok.getLoc();
73837385
consumeToken(tok::code_complete);
7384-
auto *BS = BraceStmt::create(Context, LBraceLoc, ASTNode(CCE), RBraceLoc,
7385-
/*implicit*/ true);
7386+
auto *BS =
7387+
BraceStmt::create(Context, LBraceLoc, ASTNode(Return), RBraceLoc,
7388+
/*implicit*/ true);
7389+
AFD->setHasSingleExpressionBody();
73867390
AFD->setBodyParsed(BS);
73877391
return {BS, Fingerprint::ZERO()};
73887392
}

0 commit comments

Comments
 (0)