From 1428e83b473713a9252bebd0171aed3e14730135 Mon Sep 17 00:00:00 2001 From: Muir Manders Date: Sat, 19 Mar 2022 11:21:36 -0700 Subject: [PATCH] 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 gopls-CI: kokoro TryBot-Result: Gopher Robot Reviewed-by: Robert Findley Trust: Hyang-Ah Hana Kim --- internal/lsp/source/completion/completion.go | 4 ++++ internal/lsp/testdata/summary_go1.18.txt.golden | 2 +- internal/lsp/testdata/typeparams/type_params.go | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/lsp/source/completion/completion.go b/internal/lsp/source/completion/completion.go index 4334d6d433..60c404dc5f 100644 --- a/internal/lsp/source/completion/completion.go +++ b/internal/lsp/source/completion/completion.go @@ -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 } diff --git a/internal/lsp/testdata/summary_go1.18.txt.golden b/internal/lsp/testdata/summary_go1.18.txt.golden index 3224973f25..48639899ed 100644 --- a/internal/lsp/testdata/summary_go1.18.txt.golden +++ b/internal/lsp/testdata/summary_go1.18.txt.golden @@ -2,7 +2,7 @@ CallHierarchyCount = 2 CodeLensCount = 5 CompletionsCount = 266 -CompletionSnippetCount = 109 +CompletionSnippetCount = 110 UnimportedCompletionsCount = 5 DeepCompletionsCount = 5 FuzzyCompletionsCount = 8 diff --git a/internal/lsp/testdata/typeparams/type_params.go b/internal/lsp/testdata/typeparams/type_params.go index e9acb82ed0..1dfb1034a9 100644 --- a/internal/lsp/testdata/typeparams/type_params.go +++ b/internal/lsp/testdata/typeparams/type_params.go @@ -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})") +}