@@ -890,6 +890,25 @@ static void addObserverKeywords(CodeCompletionResultSink &Sink) {
890890 addKeyword (Sink, " didSet" , CodeCompletionKeywordKind::None);
891891}
892892
893+ static void addKeywordsAfterReturn (CodeCompletionResultSink &Sink, DeclContext *DC) {
894+ // `return nil` is not actually represented as a `ReturnExpr` in the AST but
895+ // gets translated to a `FailStmt`. We thus can't produce the 'nil' completion
896+ // using the solver-based implementation. Add the result manually.
897+ if (auto ctor = dyn_cast_or_null<ConstructorDecl>(DC->getAsDecl ())) {
898+ if (ctor->isFailable ()) {
899+ CodeCompletionResultBuilder Builder (Sink, CodeCompletionResultKind::Literal,
900+ SemanticContextKind::None);
901+ Builder.setLiteralKind (CodeCompletionLiteralKind::NilLiteral);
902+ Builder.addKeyword (" nil" );
903+ Builder.addTypeAnnotation (ctor->getResultInterfaceType (), {});
904+ Builder.setResultTypes (ctor->getResultInterfaceType ());
905+ ExpectedTypeContext TypeContext;
906+ TypeContext.setPossibleTypes ({ctor->getResultInterfaceType ()});
907+ Builder.setTypeContext (TypeContext, DC);
908+ }
909+ }
910+ }
911+
893912void swift::ide::addExprKeywords (CodeCompletionResultSink &Sink,
894913 DeclContext *DC) {
895914 // Expression is invalid at top-level of non-script files.
@@ -1026,6 +1045,8 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
10261045
10271046 LLVM_FALLTHROUGH;
10281047 case CompletionKind::ReturnStmtExpr:
1048+ addKeywordsAfterReturn (Sink, CurDeclContext);
1049+ LLVM_FALLTHROUGH;
10291050 case CompletionKind::YieldStmtExpr:
10301051 case CompletionKind::ForEachSequence:
10311052 addSuperKeyword (Sink, CurDeclContext);
@@ -1533,7 +1554,9 @@ bool CodeCompletionCallbacksImpl::trySolverCompletion(bool MaybeFuncBody) {
15331554 case CompletionKind::CaseStmtBeginning:
15341555 case CompletionKind::ForEachSequence:
15351556 case CompletionKind::PostfixExprBeginning:
1536- case CompletionKind::StmtOrExpr: {
1557+ case CompletionKind::StmtOrExpr:
1558+ case CompletionKind::ReturnStmtExpr:
1559+ case CompletionKind::YieldStmtExpr: {
15371560 assert (CurDeclContext);
15381561
15391562 bool AddUnresolvedMemberCompletions =
@@ -1705,6 +1728,8 @@ void CodeCompletionCallbacksImpl::doneParsing(SourceFile *SrcFile) {
17051728 case CompletionKind::CaseStmtBeginning:
17061729 case CompletionKind::PostfixExprParen:
17071730 case CompletionKind::PostfixExpr:
1731+ case CompletionKind::ReturnStmtExpr:
1732+ case CompletionKind::YieldStmtExpr:
17081733 llvm_unreachable (" should be already handled" );
17091734 return ;
17101735
@@ -1953,29 +1978,6 @@ void CodeCompletionCallbacksImpl::doneParsing(SourceFile *SrcFile) {
19531978 break ;
19541979 }
19551980
1956- case CompletionKind::ReturnStmtExpr : {
1957- SourceLoc Loc = P.Context .SourceMgr .getIDEInspectionTargetLoc ();
1958- SmallVector<Type, 2 > possibleReturnTypes;
1959- collectPossibleReturnTypesFromContext (CurDeclContext, possibleReturnTypes);
1960- Lookup.setExpectedTypes (possibleReturnTypes,
1961- /* isImplicitSingleExpressionReturn*/ false );
1962- Lookup.getValueCompletionsInDeclContext (Loc);
1963- break ;
1964- }
1965-
1966- case CompletionKind::YieldStmtExpr: {
1967- SourceLoc Loc = P.Context .SourceMgr .getIDEInspectionTargetLoc ();
1968- if (auto FD = dyn_cast<AccessorDecl>(CurDeclContext)) {
1969- if (FD->isCoroutine ()) {
1970- // TODO: handle multi-value yields.
1971- Lookup.setExpectedTypes (FD->getStorage ()->getValueInterfaceType (),
1972- /* isImplicitSingleExpressionReturn*/ false );
1973- }
1974- }
1975- Lookup.getValueCompletionsInDeclContext (Loc);
1976- break ;
1977- }
1978-
19791981 case CompletionKind::AfterPoundDirective: {
19801982 addPoundDirectives (CompletionContext.getResultSink ());
19811983
0 commit comments