@@ -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,9 @@ 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:
1559
+ case CompletionKind::YieldStmtExpr: {
1537
1560
assert (CurDeclContext);
1538
1561
1539
1562
bool AddUnresolvedMemberCompletions =
@@ -1705,6 +1728,8 @@ void CodeCompletionCallbacksImpl::doneParsing(SourceFile *SrcFile) {
1705
1728
case CompletionKind::CaseStmtBeginning:
1706
1729
case CompletionKind::PostfixExprParen:
1707
1730
case CompletionKind::PostfixExpr:
1731
+ case CompletionKind::ReturnStmtExpr:
1732
+ case CompletionKind::YieldStmtExpr:
1708
1733
llvm_unreachable (" should be already handled" );
1709
1734
return ;
1710
1735
@@ -1953,29 +1978,6 @@ void CodeCompletionCallbacksImpl::doneParsing(SourceFile *SrcFile) {
1953
1978
break ;
1954
1979
}
1955
1980
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
-
1979
1981
case CompletionKind::AfterPoundDirective: {
1980
1982
addPoundDirectives (CompletionContext.getResultSink ());
1981
1983
0 commit comments