-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gopls/internal/lsp/source/completion: infer parameter types
This change causes completion to omit type parameters in the snippet template when the instantiations can be inferred from the arguments to the call. This is only implemented when types are available, not for unimported completions. (In principle it could be implemented using a best-effort syntactic traversal, similar to inlining's "freeishNames", but it seems fiddly.) Fixes golang/go#51783 Change-Id: I4c793628e7f2cbbb28a7f5ca7b5e305ab683491e Reviewed-on: https://go-review.googlesource.com/c/tools/+/554855 Reviewed-by: Robert Findley <rfindley@google.com> Auto-Submit: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
- Loading branch information
Showing
6 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
gopls/internal/test/marker/testdata/completion/issue51783.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
Regression test for "completion gives unneeded generic type | ||
instantiation snippet", #51783. | ||
|
||
Type parameters that can be inferred from the arguments | ||
are not part of the offered completion snippet. | ||
|
||
-- flags -- | ||
-ignore_extra_diags | ||
|
||
-- a.go -- | ||
package a | ||
|
||
// identity has a single simple type parameter. | ||
// The completion omits the instantiation. | ||
func identity[T any](x T) T | ||
|
||
// clone has a second type parameter that is nonetheless constrained by the parameter. | ||
// The completion omits the instantiation. | ||
func clone[S ~[]E, E any](s S) S | ||
|
||
// unconstrained has a type parameter constrained only by the result. | ||
// The completion suggests instantiation. | ||
func unconstrained[X, Y any](x X) Y | ||
|
||
// partial has three type parameters, | ||
// only the last two of which may be omitted as they | ||
// are constrained by the arguments. | ||
func partial[R any, S ~[]E, E any](s S) R | ||
|
||
//@item(identity, "identity", "details", "kind") | ||
//@item(clone, "clone", "details", "kind") | ||
//@item(unconstrained, "unconstrained", "details", "kind") | ||
//@item(partial, "partial", "details", "kind") | ||
|
||
func _() { | ||
_ = identity //@snippet("identity", identity, "identity(${1:})") | ||
|
||
_ = clone //@snippet("clone", clone, "clone(${1:})") | ||
|
||
_ = unconstrained //@snippet("unconstrained", unconstrained, "unconstrained[${1:}](${2:})") | ||
|
||
_ = partial //@snippet("partial", partial, "partial[${1:}](${2:})") | ||
|
||
// Result-type inference permits us to omit Y in this (rare) case, | ||
// but completion doesn't support that. | ||
var _ int = unconstrained //@snippet("unconstrained", unconstrained, "unconstrained[${1:}](${2:})") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters