@@ -890,6 +890,25 @@ static void addObserverKeywords(CodeCompletionResultSink &Sink) {
890
890
addKeyword (Sink, " didSet" , CodeCompletionKeywordKind::None);
891
891
}
892
892
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
+
893
912
void swift::ide::addExprKeywords (CodeCompletionResultSink &Sink,
894
913
DeclContext *DC) {
895
914
// Expression is invalid at top-level of non-script files.
@@ -1026,6 +1045,8 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
1026
1045
1027
1046
LLVM_FALLTHROUGH;
1028
1047
case CompletionKind::ReturnStmtExpr:
1048
+ addKeywordsAfterReturn (Sink, CurDeclContext);
1049
+ LLVM_FALLTHROUGH;
1029
1050
case CompletionKind::YieldStmtExpr:
1030
1051
case CompletionKind::ForEachSequence:
1031
1052
addSuperKeyword (Sink, CurDeclContext);
@@ -1533,7 +1554,8 @@ bool CodeCompletionCallbacksImpl::trySolverCompletion(bool MaybeFuncBody) {
1533
1554
case CompletionKind::CaseStmtBeginning:
1534
1555
case CompletionKind::ForEachSequence:
1535
1556
case CompletionKind::PostfixExprBeginning:
1536
- case CompletionKind::StmtOrExpr: {
1557
+ case CompletionKind::StmtOrExpr:
1558
+ case CompletionKind::ReturnStmtExpr: {
1537
1559
assert (CurDeclContext);
1538
1560
1539
1561
bool AddUnresolvedMemberCompletions =
@@ -1705,6 +1727,7 @@ void CodeCompletionCallbacksImpl::doneParsing(SourceFile *SrcFile) {
1705
1727
case CompletionKind::CaseStmtBeginning:
1706
1728
case CompletionKind::PostfixExprParen:
1707
1729
case CompletionKind::PostfixExpr:
1730
+ case CompletionKind::ReturnStmtExpr:
1708
1731
llvm_unreachable (" should be already handled" );
1709
1732
return ;
1710
1733
@@ -1953,16 +1976,6 @@ void CodeCompletionCallbacksImpl::doneParsing(SourceFile *SrcFile) {
1953
1976
break ;
1954
1977
}
1955
1978
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
1979
case CompletionKind::YieldStmtExpr: {
1967
1980
SourceLoc Loc = P.Context .SourceMgr .getIDEInspectionTargetLoc ();
1968
1981
if (auto FD = dyn_cast<AccessorDecl>(CurDeclContext)) {
0 commit comments