Skip to content

Commit

Permalink
gopls/internal/lsp/source: fix signatureHelp with pointer receivers
Browse files Browse the repository at this point in the history
I just ran into this today. Alas, it took us too long to fix this.

Fixes golang/go#61189

Change-Id: Iac28a6ba30d099cb2bb6f9d761ee658e190aa07c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/539677
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
findleyr committed Nov 6, 2023
1 parent 4124316 commit 118c362
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion gopls/internal/lsp/source/types_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,11 @@ func FormatVarType(ctx context.Context, snapshot Snapshot, srcpkg Package, obj *
return types.TypeString(obj.Type(), qf), nil // in generic function
}
if decl.Recv != nil && len(decl.Recv.List) > 0 {
if x, _, _, _ := typeparams.UnpackIndexExpr(decl.Recv.List[0].Type); x != nil {
rtype := decl.Recv.List[0].Type
if e, ok := rtype.(*ast.StarExpr); ok {
rtype = e.X
}
if x, _, _, _ := typeparams.UnpackIndexExpr(rtype); x != nil {
return types.TypeString(obj.Type(), qf), nil // in method of generic type
}
}
Expand Down
21 changes: 21 additions & 0 deletions gopls/internal/regtest/marker/testdata/signature/generic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
This test checks signature help on generic signatures.

-- g.go --
package g

type M[K comparable, V any] map[K]V

// golang/go#61189: signatureHelp must handle pointer receivers.
func (m *M[K, V]) Get(k K) V {
return (*m)[k]
}

func Get[K comparable, V any](m M[K, V], k K) V {
return m[k]
}

func _() {
var m M[int, string]
_ = m.Get(0) //@signature("(", "Get(k int) string", 0)
_ = Get(m, 0) //@signature("0", "Get(m M[int, string], k int) string", 1)
}

0 comments on commit 118c362

Please sign in to comment.