Skip to content

Commit 16af937

Browse files
authored
Merge pull request swiftlang#63515 from ahoppen/ahoppen/look-through-optional
[CodeCompletion] Look through optional when determining the function type of a called overload
2 parents 4b80f0a + 5d986ec commit 16af937

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

include/swift/IDE/ArgumentCompletion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ArgumentTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
3030
/// The FuncDecl or SubscriptDecl associated with the call.
3131
ValueDecl *FuncD;
3232
/// The type of the function being called.
33-
Type FuncTy;
33+
AnyFunctionType *FuncTy;
3434
/// The index of the argument containing the completion location
3535
unsigned ArgIdx;
3636
/// The index of the parameter corresponding to the completion argument.

lib/IDE/ArgumentCompletion.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ bool ArgumentTypeCheckCompletionCallback::addPossibleParams(
3636
return true;
3737
}
3838

39-
ArrayRef<AnyFunctionType::Param> ParamsToPass =
40-
Res.FuncTy->getAs<AnyFunctionType>()->getParams();
39+
ArrayRef<AnyFunctionType::Param> ParamsToPass = Res.FuncTy->getParams();
4140

4241
ParameterList *PL = nullptr;
4342
if (Res.FuncD) {
@@ -176,8 +175,12 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
176175
llvm::SmallDenseMap<const VarDecl *, Type> SolutionSpecificVarTypes;
177176
getSolutionSpecificVarTypes(S, SolutionSpecificVarTypes);
178177

178+
AnyFunctionType *FuncTy = nullptr;
179+
if (Info.ValueTy) {
180+
FuncTy = Info.ValueTy->lookThroughAllOptionalTypes()->getAs<AnyFunctionType>();
181+
}
179182
Results.push_back({ExpectedTy, isa<SubscriptExpr>(ParentCall), Info.Value,
180-
Info.ValueTy, ArgIdx, ParamIdx, std::move(ClaimedParams),
183+
FuncTy, ArgIdx, ParamIdx, std::move(ClaimedParams),
181184
IsNoninitialVariadic, Info.BaseTy, HasLabel, IsAsync,
182185
SolutionSpecificVarTypes});
183186
}
@@ -225,8 +228,7 @@ void ArgumentTypeCheckCompletionCallback::deliverResults(
225228
}
226229
}
227230
if (Result.FuncTy) {
228-
if (auto FuncTy = Result.FuncTy->lookThroughAllOptionalTypes()
229-
->getAs<AnyFunctionType>()) {
231+
if (auto FuncTy = Result.FuncTy) {
230232
if (Result.IsSubscript) {
231233
assert(SemanticContext != SemanticContextKind::None);
232234
auto *SD = dyn_cast_or_null<SubscriptDecl>(Result.FuncD);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t
3+
4+
func foo(_ x: ((_ x: Int, _ y: Int) -> Void)?) {
5+
x?(1, #^OPTIONAL_PARAMETER^#)
6+
// OPTIONAL_PARAMETER: Begin completions
7+
// OPTIONAL_PARAMETER-DAG: Literal[Integer]/None/TypeRelation[Convertible]: 0[#Int#]; name=0
8+
// OPTIONAL_PARAMETER: End completions
9+
}

0 commit comments

Comments
 (0)