lsp/completion: fix bogus generic func conversion

For example:

    func foo[A int | float64](a A) A { return a }

    var _ int = fo<>

Previously at <> we would complete to "int(foo[A int|float64](a A))".
We added the int() type conversion because the returned type param A
was convertible to the expected "int" (i.e. both "int" and "float64"
can be converted). This is a premature suggestion, though, so fix by
suppressing such type conversions for type parameters.

Fixes golang/go#51780.

Change-Id: I63e3bd401a4d9927b9261659812c521c02e33d94
Reviewed-on: https://go-review.googlesource.com/c/tools/+/394016
Run-TryBot: Muir Manders <muir@mnd.rs>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
Muir Manders 2022-03-19 11:21:36 -07:00 committed by Robert Findley
parent cbdab0337f
commit 1428e83b47
3 changed files with 13 additions and 1 deletions

View File

@ -2695,6 +2695,10 @@ func considerTypeConversion(from, to types.Type, path []types.Object) bool {
return false
}
if _, ok := from.(*typeparams.TypeParam); ok {
return false
}
if !types.ConvertibleTo(from, to) {
return false
}

View File

@ -2,7 +2,7 @@
CallHierarchyCount = 2
CodeLensCount = 5
CompletionsCount = 266
CompletionSnippetCount = 109
CompletionSnippetCount = 110
UnimportedCompletionsCount = 5
DeepCompletionsCount = 5
FuzzyCompletionsCount = 8

View File

@ -23,3 +23,11 @@ type s[a int | string] struct{}
func _() {
s[]{} //@rank("]", int, float64)
}
func returnTP[A int | float64](a A) A { //@item(returnTP, "returnTP", "something", "func")
return a
}
func _() {
var _ int = returnTP //@snippet(" //", returnTP, "returnTP[${1:}](${2:})", "returnTP[${1:A int|float64}](${2:a A})")
}