Skip to content

Commit 5625751

Browse files
committed
[CodeCompletion] Don't attempt lookup if base type couldn't be determined
If base type couldn't be determined (e.g. because base expression is an invalid reference), let's not attempt to do a lookup since it wouldn't produce any useful results anyway.
1 parent 20f8a37 commit 5625751

File tree

1 file changed

+43
-38
lines changed

1 file changed

+43
-38
lines changed

lib/Sema/TypeCheckCodeCompletion.cpp

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -988,46 +988,51 @@ void DotExprLookup::sawSolution(const constraints::Solution &S) {
988988
auto *ParsedExpr = CompletionExpr->getBase();
989989
auto *SemanticExpr = ParsedExpr->getSemanticsProvidingExpr();
990990

991-
if (Type BaseTy = GetType(ParsedExpr)) {
992-
auto *Locator = CS.getConstraintLocator(SemanticExpr);
993-
Type ExpectedTy = GetType(CompletionExpr);
994-
Expr *ParentExpr = CS.getParentExpr(CompletionExpr);
995-
if (!ParentExpr)
996-
ExpectedTy = CS.getContextualType(CompletionExpr);
997-
998-
auto *CalleeLocator = S.getCalleeLocator(Locator);
999-
ValueDecl *ReferencedDecl = nullptr;
1000-
if (auto SelectedOverload = S.getOverloadChoiceIfAvailable(CalleeLocator))
1001-
ReferencedDecl = SelectedOverload->choice.getDeclOrNull();
1002-
1003-
auto Key = std::make_pair(BaseTy, ReferencedDecl);
1004-
auto Ret = ResultToIndex.insert({Key, Solutions.size()});
1005-
if (!Ret.second && ExpectedTy) {
1006-
Solutions[Ret.first->getSecond()].ExpectedTypes.push_back(ExpectedTy);
1007-
} else {
1008-
bool ISDMT = S.isStaticallyDerivedMetatype(ParsedExpr);
1009-
bool ISEC = false;
1010-
bool DisallowVoid = ExpectedTy
1011-
? !ExpectedTy->isVoid()
1012-
: !ParentExpr && CS.getContextualTypePurpose(CompletionExpr) != CTP_Unused;
1013-
1014-
if (!ParentExpr) {
1015-
if (CS.getContextualTypePurpose(CompletionExpr) == CTP_ReturnSingleExpr)
991+
auto BaseTy = GetType(ParsedExpr);
992+
// If base type couldn't be determined (e.g. because base expression
993+
// is an invalid reference), let's not attempt to do a lookup since
994+
// it wouldn't produce any useful results anyway.
995+
if (!BaseTy || BaseTy->is<UnresolvedType>())
996+
return;
997+
998+
auto *Locator = CS.getConstraintLocator(SemanticExpr);
999+
Type ExpectedTy = GetType(CompletionExpr);
1000+
Expr *ParentExpr = CS.getParentExpr(CompletionExpr);
1001+
if (!ParentExpr)
1002+
ExpectedTy = CS.getContextualType(CompletionExpr);
1003+
1004+
auto *CalleeLocator = S.getCalleeLocator(Locator);
1005+
ValueDecl *ReferencedDecl = nullptr;
1006+
if (auto SelectedOverload = S.getOverloadChoiceIfAvailable(CalleeLocator))
1007+
ReferencedDecl = SelectedOverload->choice.getDeclOrNull();
1008+
1009+
auto Key = std::make_pair(BaseTy, ReferencedDecl);
1010+
auto Ret = ResultToIndex.insert({Key, Solutions.size()});
1011+
if (!Ret.second && ExpectedTy) {
1012+
Solutions[Ret.first->getSecond()].ExpectedTypes.push_back(ExpectedTy);
1013+
} else {
1014+
bool ISDMT = S.isStaticallyDerivedMetatype(ParsedExpr);
1015+
bool ISEC = false;
1016+
bool DisallowVoid = ExpectedTy
1017+
? !ExpectedTy->isVoid()
1018+
: !ParentExpr && CS.getContextualTypePurpose(
1019+
CompletionExpr) != CTP_Unused;
1020+
1021+
if (!ParentExpr) {
1022+
if (CS.getContextualTypePurpose(CompletionExpr) == CTP_ReturnSingleExpr)
1023+
ISEC = true;
1024+
} else if (auto *ParentCE = dyn_cast<ClosureExpr>(ParentExpr)) {
1025+
if (ParentCE->hasSingleExpressionBody() &&
1026+
ParentCE->getSingleExpressionBody() == CompletionExpr) {
1027+
ASTNode Last = ParentCE->getBody()->getLastElement();
1028+
if (!Last.isStmt(StmtKind::Return) || Last.isImplicit())
10161029
ISEC = true;
1017-
} else if (auto *ParentCE = dyn_cast<ClosureExpr>(ParentExpr)) {
1018-
if (ParentCE->hasSingleExpressionBody() &&
1019-
ParentCE->getSingleExpressionBody() == CompletionExpr) {
1020-
ASTNode Last = ParentCE->getBody()->getLastElement();
1021-
if (!Last.isStmt(StmtKind::Return) || Last.isImplicit())
1022-
ISEC = true;
1023-
}
10241030
}
1025-
1026-
Solutions.push_back({
1027-
BaseTy, ReferencedDecl, {}, DisallowVoid, ISDMT, ISEC
1028-
});
1029-
if (ExpectedTy)
1030-
Solutions.back().ExpectedTypes.push_back(ExpectedTy);
10311031
}
1032+
1033+
Solutions.push_back(
1034+
{BaseTy, ReferencedDecl, {}, DisallowVoid, ISDMT, ISEC});
1035+
if (ExpectedTy)
1036+
Solutions.back().ExpectedTypes.push_back(ExpectedTy);
10321037
}
10331038
}

0 commit comments

Comments
 (0)