@@ -285,40 +285,61 @@ bool swift::ide::removeCodeCompletionExpr(ASTContext &Ctx, Expr *&expr) {
285
285
}
286
286
287
287
// ===----------------------------------------------------------------------===//
288
- // getReturnTypeFromContext (DeclContext)
288
+ // collectPossibleReturnTypesFromContext (DeclContext, SmallVectorImpl<Type> )
289
289
// ===----------------------------------------------------------------------===//
290
290
291
- Type swift::ide::getReturnTypeFromContext (const DeclContext *DC) {
291
+ void swift::ide::collectPossibleReturnTypesFromContext (
292
+ DeclContext *DC, SmallVectorImpl<Type> &candidates) {
292
293
if (auto FD = dyn_cast<AbstractFunctionDecl>(DC)) {
293
294
auto Ty = FD->getInterfaceType ();
294
295
if (FD->getDeclContext ()->isTypeContext ())
295
296
Ty = FD->getMethodInterfaceType ();
296
- if (auto FT = Ty->getAs <AnyFunctionType>())
297
- return DC->mapTypeIntoContext (FT->getResult ());
298
- } else if (auto ACE = dyn_cast<AbstractClosureExpr>(DC)) {
299
- if (ACE->getType () && !ACE->getType ()->hasError ())
300
- return ACE->getResultType ();
297
+ if (auto FT = Ty->getAs <AnyFunctionType>()) {
298
+ candidates.push_back (DC->mapTypeIntoContext (FT->getResult ()));
299
+ }
300
+ }
301
+
302
+ if (auto ACE = dyn_cast<AbstractClosureExpr>(DC)) {
303
+ // Use the type checked type if it has.
304
+ if (ACE->getType () && !ACE->getType ()->hasError () &&
305
+ !ACE->getResultType ()->hasUnresolvedType ()) {
306
+ candidates.push_back (ACE->getResultType ());
307
+ return ;
308
+ }
309
+
301
310
if (auto CE = dyn_cast<ClosureExpr>(ACE)) {
302
311
if (CE->hasExplicitResultType ()) {
312
+ // If the closure has a explicit return type, use it.
303
313
if (auto ty = CE->getExplicitResultType ()) {
304
- return ty;
314
+ candidates.push_back (ty);
315
+ return ;
316
+ } else {
317
+ auto typeLoc = TypeLoc{CE->getExplicitResultTypeRepr ()};
318
+ if (!swift::performTypeLocChecking (
319
+ DC->getASTContext (), typeLoc, /* isSILMode=*/ false ,
320
+ /* isSILType=*/ false , DC->getGenericEnvironmentOfContext (),
321
+ const_cast <DeclContext *>(DC), /* diagnostics=*/ false )) {
322
+ candidates.push_back (typeLoc.getType ());
323
+ return ;
324
+ }
305
325
}
306
-
307
- auto typeLoc = TypeLoc{CE->getExplicitResultTypeRepr ()};
308
- if (swift::performTypeLocChecking (DC->getASTContext (),
309
- typeLoc,
310
- /* isSILMode*/ false ,
311
- /* isSILType*/ false ,
312
- DC->getGenericEnvironmentOfContext (),
313
- const_cast <DeclContext *>(DC),
314
- /* diagnostics*/ false )) {
315
- return Type ();
326
+ } else {
327
+ // Otherwise, check the context type of the closure.
328
+ ExprContextInfo closureCtxInfo (CE->getParent (), CE);
329
+ for (auto closureTy : closureCtxInfo.getPossibleTypes ()) {
330
+ if (auto funcTy = closureTy->getAs <AnyFunctionType>())
331
+ candidates.push_back (funcTy->getResult ());
316
332
}
317
- return typeLoc.getType ();
333
+ if (!candidates.empty ())
334
+ return ;
318
335
}
319
336
}
337
+
338
+ // Even if the type checked type has unresolved types, it's better than
339
+ // nothing.
340
+ if (ACE->getType () && !ACE->getType ()->hasError ())
341
+ candidates.push_back (ACE->getResultType ());
320
342
}
321
- return Type ();
322
343
}
323
344
324
345
// ===----------------------------------------------------------------------===//
@@ -929,7 +950,10 @@ class ExprContextAnalyzer {
929
950
auto *CE = cast<ClosureExpr>(Parent);
930
951
assert (isSingleExpressionBodyForCodeCompletion (CE->getBody ()));
931
952
singleExpressionBody = true ;
932
- recordPossibleType (getReturnTypeFromContext (CE));
953
+ SmallVector<Type, 2 > candidates;
954
+ collectPossibleReturnTypesFromContext (CE, candidates);
955
+ for (auto ty : candidates)
956
+ recordPossibleType (ty);
933
957
break ;
934
958
}
935
959
default :
@@ -939,9 +963,13 @@ class ExprContextAnalyzer {
939
963
940
964
void analyzeStmt (Stmt *Parent) {
941
965
switch (Parent->getKind ()) {
942
- case StmtKind::Return:
943
- recordPossibleType (getReturnTypeFromContext (DC));
966
+ case StmtKind::Return: {
967
+ SmallVector<Type, 2 > candidates;
968
+ collectPossibleReturnTypesFromContext (DC, candidates);
969
+ for (auto ty : candidates)
970
+ recordPossibleType (ty);
944
971
break ;
972
+ }
945
973
case StmtKind::ForEach:
946
974
if (auto SEQ = cast<ForEachStmt>(Parent)->getSequence ()) {
947
975
if (containsTarget (SEQ)) {
@@ -1004,7 +1032,10 @@ class ExprContextAnalyzer {
1004
1032
if (auto *FD = dyn_cast<FuncDecl>(D)) {
1005
1033
assert (isSingleExpressionBodyForCodeCompletion (FD->getBody ()));
1006
1034
singleExpressionBody = true ;
1007
- recordPossibleType (getReturnTypeFromContext (FD));
1035
+ SmallVector<Type, 2 > candidates;
1036
+ collectPossibleReturnTypesFromContext (DC, candidates);
1037
+ for (auto ty : candidates)
1038
+ recordPossibleType (ty);
1008
1039
break ;
1009
1040
}
1010
1041
llvm_unreachable (" Unhandled decl kind." );
0 commit comments