Skip to content

Commit 25a90be

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/golang: Implementations for func types
This CL adds support to the Implementations query for function types. The query relates two sets of locations: 1. the "func" token of each function declaration (FuncDecl or FuncLit). These are analogous to declarations of concrete methods. 2. uses of abstract functions: (a) the "func" token of each FuncType that is not part of Func{Decl,Lit}. These are analogous to interface{...} types. (b) the "(" paren of each dynamic call on a value of an abstract function type. These are analogous to references to interface method names, but no names are involved, which has historically made them hard to search for. An Implementations query on a location in set 1 returns set 2, and vice versa. Only the local algorithm is implemented for now; the global one (using an index analogous to methodsets) will follow. This CL supersedes CL 448035 and CL 619515, both of which attempt to unify the treatment of functions and interfaces in the methodsets algorithm and in the index; but the two problems are not precisely analogous, and I think we'll end up with more but simpler code if we implement themn separately. + tests, docs, relnotes Updates golang/go#56572 Change-Id: I18e1a7cc2f6c320112b9f3589323d04f9a52ef3c Reviewed-on: https://go-review.googlesource.com/c/tools/+/654556 Commit-Queue: Alan Donovan <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent db6008c commit 25a90be

File tree

6 files changed

+403
-33
lines changed

6 files changed

+403
-33
lines changed

Diff for: gopls/doc/features/navigation.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ Client support:
8585

8686
The LSP
8787
[`textDocument/implementation`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_implementation)
88-
request queries the "implements" relation between interfaces and concrete types:
88+
request queries the relation between abstract and concrete types and
89+
their methods.
90+
91+
Interfaces and concrete types are matched using method sets:
8992

9093
- When invoked on a reference to an **interface type**, it returns the
9194
location of the declaration of each type that implements
@@ -111,6 +114,17 @@ types with methods due to embedding) may be missing from the results.
111114
but that is not consistent with the "scalable" gopls design.
112115
-->
113116

117+
Functions, `func` types, and dynamic function calls are matched using signatures:
118+
119+
- When invoked on the `func` token of a **function definition**,
120+
it returns the locations of the matching signature types
121+
and dynamic call expressions.
122+
- When invoked on the `func` token of a **signature type**,
123+
it returns the locations of the matching concrete function definitions.
124+
- When invoked on the `(` token of a **dynamic function call**,
125+
it returns the locations of the matching concrete function
126+
definitions.
127+
114128
If either the target type or the candidate type are generic, the
115129
results will include the candidate type if there is any instantiation
116130
of the two types that would allow one to implement the other.
@@ -120,6 +134,12 @@ types, without regard to consistency of substitutions across the
120134
method set or even within a single method.
121135
This may lead to occasional spurious matches.)
122136

137+
Since a type may be both a function type and a named type with methods
138+
(for example, `http.HandlerFunc`), it may participate in both kinds of
139+
implementation queries (by method-sets and function signatures).
140+
Queries using method-sets should be invoked on the type or method name,
141+
and queries using signatures should be invoked on a `func` or `(` token.
142+
123143
Client support:
124144
- **VS Code**: Use [Go to Implementations](https://code.visualstudio.com/docs/editor/editingevolved#_go-to-implementation) (`⌘F12`).
125145
- **Emacs + eglot**: Use `M-x eglot-find-implementation`.

Diff for: gopls/doc/release/v0.19.0.md

+27
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@
77

88
# New features
99

10+
## "Implementations" supports signature types
11+
12+
The Implementations query reports the correspondence between abstract
13+
and concrete types and their methods based on their method sets.
14+
Now, it also reports the correspondence between function types,
15+
dynamic function calls, and function definitions, based on their signatures.
16+
17+
To use it, invoke an Implementations query on the `func` token of the
18+
definition of a named function, named method, or function literal.
19+
Gopls reports the set of function signature types that abstract this
20+
function, and the set of dynamic calls through values of such types.
21+
22+
Conversely, an Implementations query on the `func` token of a
23+
signature type, or on the `(` paren of a dynamic function call,
24+
reports the set of concrete functions that the signature abstracts
25+
or that the call dispatches to.
26+
27+
Since a type may be both a function type and a named type with methods
28+
(for example, `http.HandlerFunc`), it may participate in both kinds of
29+
Implements queries (method-sets and function signatures).
30+
Queries using method-sets should be invoked on the type or method name,
31+
and queries using signatures should be invoked on a `func` or `(` token.
32+
33+
Only the local (same-package) algorithm is currently supported.
34+
TODO: implement global.
35+
36+
1037
## "Eliminate dot import" code action
1138

1239
This code action, available on a dotted import, will offer to replace

0 commit comments

Comments
 (0)