Skip to content

Commit 2428bad

Browse files
authored
Merge pull request #70010 from ahoppen/ahoppen/prefer-call-if-reference-and-call-are-possible
[CodeComplete] Prefer function call if both reference and call are possible
2 parents 6ac7915 + cf12f2b commit 2428bad

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

lib/IDE/CompletionLookup.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,10 +1972,11 @@ bool CompletionLookup::addCompoundFunctionNameIfDesiable(
19721972
if (!useFunctionReference && funcTy) {
19731973
// We know that the CodeCompletionResultType is AST-based so we can pass
19741974
// nullptr for USRTypeContext.
1975-
auto maxRel = CodeCompletionResultType(funcTy).calculateTypeRelation(
1975+
auto maxFuncTyRel = CodeCompletionResultType(funcTy).calculateTypeRelation(
19761976
&expectedTypeContext, CurrDeclContext, /*USRTypeContext=*/nullptr);
1977-
useFunctionReference =
1978-
maxRel >= CodeCompletionResultTypeRelation::Convertible;
1977+
auto maxResultTyRel = CodeCompletionResultType(funcTy->getResult()).calculateTypeRelation(
1978+
&expectedTypeContext, CurrDeclContext, /*USRTypeContext=*/nullptr);
1979+
useFunctionReference = maxFuncTyRel > maxResultTyRel;
19791980
}
19801981
if (!useFunctionReference)
19811982
return false;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %batch-code-completion
2+
3+
struct Route {
4+
func makeDetailView() -> Int {
5+
return 52
6+
}
7+
}
8+
9+
@resultBuilder struct ViewBuilder {
10+
static func buildBlock(_ x: Int) -> Int { return x }
11+
}
12+
13+
func foo(@ViewBuilder destination: () -> Int) {}
14+
func foo(destination: Int) {}
15+
16+
func test(route: Route?) {
17+
route.map { route in
18+
foo(destination: route.#^COMPLETE^#)
19+
}
20+
}
21+
22+
23+
// COMPLETE-DAG: Keyword[self]/CurrNominal: self[#Route#];
24+
// COMPLETE-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Convertible]: makeDetailView()[#Int#];
25+
26+
func testTask(route: Route) {
27+
Task {
28+
route.#^IN_TASK?check=COMPLETE^#
29+
}
30+
}
31+
32+
protocol Builder {
33+
func recv<T>(_: (T) throws -> Void)
34+
}
35+
36+
struct S {
37+
func foo(x: Int) throws {}
38+
func test(builder: Builder) {
39+
builder.recv(#^IN_CONTEXT_FOR_UNAPPLIED_REF^#)
40+
}
41+
}
42+
// IN_CONTEXT_FOR_UNAPPLIED_REF-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Convertible]: foo(x:)[#(Int) throws -> ()#]; name=foo(x:)
43+
// IN_CONTEXT_FOR_UNAPPLIED_REF-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Convertible]: test(builder:)[#(any Builder) -> ()#]; name=test(builder:)

0 commit comments

Comments
 (0)