Skip to content

Commit 17eb6ea

Browse files
committed
[CodeCompletion] Unify logic to retrieve completion expr type for all completion callbacks
1 parent 51777d5 commit 17eb6ea

File tree

6 files changed

+16
-20
lines changed

6 files changed

+16
-20
lines changed

lib/IDE/ArgumentCompletion.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ void ArgumentTypeCheckCompletionCallback::sawSolution(const Solution &S) {
9494
TypeCheckCompletionCallback::sawSolution(S);
9595

9696
Type ExpectedTy = getTypeForCompletion(S, CompletionExpr);
97-
if (!ExpectedTy) {
98-
return;
99-
}
10097

10198
auto &CS = S.getConstraintSystem();
10299

lib/IDE/DotExprCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void DotExprTypeCheckCompletionCallback::sawSolution(
5555
// If base type couldn't be determined (e.g. because base expression
5656
// is an invalid reference), let's not attempt to do a lookup since
5757
// it wouldn't produce any useful results anyway.
58-
if (!BaseTy || BaseTy->getRValueType()->is<UnresolvedType>())
58+
if (!BaseTy)
5959
return;
6060

6161
auto *Locator = CS.getConstraintLocator(SemanticExpr);

lib/IDE/ExprCompletion.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,7 @@ void ExprTypeCheckCompletionCallback::sawSolution(
2525

2626
auto &CS = S.getConstraintSystem();
2727

28-
// Prefer to get the expected type as the completion expression's contextual
29-
// type. If that fails (because there is no explicit contextual type spelled
30-
// out in the source), the code completion expression will have been
31-
// type-checked to its expected contextual type.
32-
Type ExpectedTy =
33-
CS.getContextualType(CompletionExpr, /*forConstraint=*/false);
34-
if (!ExpectedTy) {
35-
ExpectedTy = S.getResolvedType(CompletionExpr);
36-
}
37-
if (ExpectedTy->hasUnresolvedType()) {
38-
ExpectedTy = Type();
39-
}
28+
Type ExpectedTy = getTypeForCompletion(S, CompletionExpr);
4029

4130
bool ImplicitReturn = isImplicitSingleExpressionReturn(CS, CompletionExpr);
4231

lib/IDE/TypeCheckCompletionCallback.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Type swift::ide::getTypeForCompletion(const constraints::Solution &S, Expr *E) {
4747

4848
auto &CS = S.getConstraintSystem();
4949

50+
Type Result;
51+
5052
// To aid code completion, we need to attempt to convert type placeholders
5153
// back into underlying generic parameters if possible, since type
5254
// of the code completion expression is used as "expected" (or contextual)
@@ -70,7 +72,7 @@ Type swift::ide::getTypeForCompletion(const constraints::Solution &S, Expr *E) {
7072
return type;
7173
});
7274

73-
return S.simplifyType(completionTy.transform([&](Type type) {
75+
Result = S.simplifyType(completionTy.transform([&](Type type) {
7476
if (auto *placeholder = type->getAs<PlaceholderType>()) {
7577
if (auto *typeVar =
7678
placeholder->getOriginator().dyn_cast<TypeVariableType *>()) {
@@ -88,9 +90,17 @@ Type swift::ide::getTypeForCompletion(const constraints::Solution &S, Expr *E) {
8890

8991
return type;
9092
}));
93+
} else {
94+
Result = S.getResolvedType(E);
9195
}
9296

93-
return S.getResolvedType(E);
97+
if (!Result || Result->is<UnresolvedType>()) {
98+
Result = CS.getContextualType(E, /*forConstraint=*/false);
99+
}
100+
if (Result && Result->is<UnresolvedType>()) {
101+
Result = Type();
102+
}
103+
return Result;
94104
}
95105

96106
bool swift::ide::isImplicitSingleExpressionReturn(ConstraintSystem &CS,

lib/IDE/UnresolvedMemberCompletion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void UnresolvedMemberTypeCheckCompletionCallback::sawSolution(
8989
// If the type couldn't be determined (e.g. because there isn't any context
9090
// to derive it from), let's not attempt to do a lookup since it wouldn't
9191
// produce any useful results anyway.
92-
if (ExpectedTy && !ExpectedTy->is<UnresolvedType>()) {
92+
if (ExpectedTy) {
9393
// If ExpectedTy is a duplicate of any other result, ignore this solution.
9494
if (!llvm::any_of(ExprResults, [&](const ExprResult &R) {
9595
return R.ExpectedTy->isEqual(ExpectedTy);

test/IDE/complete_assignment.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func f2() {
202202
// ASSIGN_11-DAG: Decl[InstanceMethod]/CurrNominal: IntOpGen()[#Int?#]
203203
// ASSIGN_11-DAG: Decl[InstanceMethod]/CurrNominal: D1Gen()[#D1#]
204204
// ASSIGN_11-DAG: Decl[InstanceMethod]/CurrNominal: D2Gen()[#D2#]
205-
// ASSIGN_11-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Invalid]: VoidGen()[#Void#]
205+
// ASSIGN_11-DAG: Decl[InstanceMethod]/CurrNominal: VoidGen()[#Void#]
206206
// ASSIGN_11-DAG: Decl[InstanceVar]/CurrNominal: InternalC2[#C2#]
207207

208208
func f12() {

0 commit comments

Comments
 (0)