Skip to content

Commit 2392117

Browse files
committed
[CodeCompletion] Record solution-specific variable types in argument completion
We are doing global completion for function arguments, so we should also record the variable types determined by the solution.
1 parent f223050 commit 2392117

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

include/swift/IDE/ArgumentCompletion.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class ArgumentTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
4848
/// Whether the surrounding context is async and thus calling async
4949
/// functions is supported.
5050
bool IsInAsyncContext;
51+
52+
/// Types of variables that were determined in the solution that produced
53+
/// this result. This in particular includes parameters of closures that
54+
/// were type-checked with the code completion expression.
55+
llvm::SmallDenseMap<const VarDecl *, Type> SolutionSpecificVarTypes;
5156
};
5257

5358
CodeCompletionExpr *CompletionExpr;

include/swift/IDE/TypeCheckCompletionCallback.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ Type getTypeForCompletion(const constraints::Solution &S, ASTNode Node);
7575
/// completion expression \p E.
7676
Type getPatternMatchType(const constraints::Solution &S, Expr *E);
7777

78+
/// Populate \p Result with types of variables that were determined in the
79+
/// solution \p S. This in particular includes parameters of closures that
80+
/// were type-checked with the code completion expression.
81+
void getSolutionSpecificVarTypes(
82+
const constraints::Solution &S,
83+
llvm::SmallDenseMap<const VarDecl *, Type> &Result);
84+
7885
/// Whether the given completion expression is the only expression in its
7986
/// containing closure or function body and its value is implicitly returned.
8087
///

lib/IDE/ArgumentCompletion.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,13 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
193193
return;
194194
}
195195

196+
llvm::SmallDenseMap<const VarDecl *, Type> SolutionSpecificVarTypes;
197+
getSolutionSpecificVarTypes(S, SolutionSpecificVarTypes);
198+
196199
Results.push_back({ExpectedTy, isa<SubscriptExpr>(ParentCall), FuncD, FuncTy,
197200
ArgIdx, ParamIdx, std::move(ClaimedParams),
198-
IsNoninitialVariadic, CallBaseTy, HasLabel, IsAsync});
201+
IsNoninitialVariadic, CallBaseTy, HasLabel, IsAsync,
202+
SolutionSpecificVarTypes});
199203
}
200204

201205
void ArgumentTypeCheckCompletionCallback::deliverResults(
@@ -267,6 +271,7 @@ void ArgumentTypeCheckCompletionCallback::deliverResults(
267271
if (shouldPerformGlobalCompletion) {
268272
for (auto &Result : Results) {
269273
ExpectedTypes.push_back(Result.ExpectedType);
274+
Lookup.setSolutionSpecificVarTypes(Result.SolutionSpecificVarTypes);
270275
}
271276
Lookup.setExpectedTypes(ExpectedTypes, false);
272277
bool IsInAsyncContext = llvm::any_of(

lib/IDE/ExprCompletion.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ void ExprTypeCheckCompletionCallback::sawSolutionImpl(
7878
bool IsAsync = isContextAsync(S, DC);
7979

8080
llvm::SmallDenseMap<const VarDecl *, Type> SolutionSpecificVarTypes;
81-
for (auto NT : S.nodeTypes) {
82-
if (auto VD = dyn_cast_or_null<VarDecl>(NT.first.dyn_cast<Decl *>())) {
83-
SolutionSpecificVarTypes[VD] = S.simplifyType(NT.second);
84-
}
85-
}
81+
getSolutionSpecificVarTypes(S, SolutionSpecificVarTypes);
8682

8783
addResult(ImplicitReturn, IsAsync, SolutionSpecificVarTypes);
8884
addExpectedType(ExpectedTy);

lib/IDE/TypeCheckCompletionCallback.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ Type swift::ide::getPatternMatchType(const constraints::Solution &S, Expr *E) {
125125
return nullptr;
126126
}
127127

128+
void swift::ide::getSolutionSpecificVarTypes(
129+
const constraints::Solution &S,
130+
llvm::SmallDenseMap<const VarDecl *, Type> &Result) {
131+
assert(Result.empty());
132+
for (auto NT : S.nodeTypes) {
133+
if (auto VD = dyn_cast_or_null<VarDecl>(NT.first.dyn_cast<Decl *>())) {
134+
Result[VD] = S.simplifyType(NT.second);
135+
}
136+
}
137+
}
138+
128139
bool swift::ide::isImplicitSingleExpressionReturn(ConstraintSystem &CS,
129140
Expr *CompletionExpr) {
130141
Expr *ParentExpr = CS.getParentExpr(CompletionExpr);

0 commit comments

Comments
 (0)