diff --git a/gopls/internal/lsp/source/references.go b/gopls/internal/lsp/source/references.go index 4bc724fefe7..aa9b982550e 100644 --- a/gopls/internal/lsp/source/references.go +++ b/gopls/internal/lsp/source/references.go @@ -621,10 +621,8 @@ func localReferences(pkg Package, targets map[types.Object]bool, correspond bool if id, ok := n.(*gopast.Ident); ok { if obj, ok := pkg.GopTypesInfo().Uses[id]; ok { // goxls: use overload declaration to match - if overdecl, overloads := pkg.GopTypesInfo().OverloadOf(id); overdecl != nil && overloads != nil { - obj = overdecl - } - if matches(obj) { + overdecl, _ := pkg.GopTypesInfo().OverloadOf(id) + if matches(obj) || (overdecl != nil && matches(overdecl)) { report(gopMustLocation(pgf, id), false) } } diff --git a/gopls/internal/regtest/misc/references_gox_test.go b/gopls/internal/regtest/misc/references_gox_test.go index 179853ee5f9..d6b307eb1dd 100644 --- a/gopls/internal/regtest/misc/references_gox_test.go +++ b/gopls/internal/regtest/misc/references_gox_test.go @@ -14,7 +14,7 @@ func TestReferencesOnOverloadDecl1(t *testing.T) { -- go.mod -- module mod.com -go 1.12 +go 1.19 -- def.gop -- func add = ( func(a, b int) int { @@ -27,6 +27,22 @@ func add = ( -- test.gop -- println add(1,2) println add("Hello", "World") +-- gop_autogen.go -- +package main + +import "fmt" + +const _ = true +func add__0(a int, b int) int { + return a + b +} +func add__1(a string, b string) string { + return a + b +} +func main() { + fmt.Println(add__0(1, 2)) + fmt.Println(add__1("Hello", "World")) +} ` Run(t, files, func(t *testing.T, env *Env) { env.OpenFile("def.gop") @@ -54,7 +70,7 @@ func TestReferencesOnOverloadDecl2(t *testing.T) { -- go.mod -- module mod.com -go 1.12 +go 1.19 -- def.gop -- func mulInt(a, b int) int { return a * b @@ -75,7 +91,29 @@ func mul = ( println mul(100, 7) println mul("Hello", "World") println mul(1.2, 3.14) +-- gop_autogen.go -- +package main + +import "fmt" + +const _ = true +const Gopo_mul = "mulInt,,mulFloat" +func mulInt(a int, b int) int { + return a * b +} +func mul__1(a string, b string) string { + return a + b +} +func mulFloat(a float64, b float64) float64 { + return a * b +} +func main() { + fmt.Println(mulInt(100, 7)) + fmt.Println(mul__1("Hello", "World")) + fmt.Println(mulFloat(1.2, 3.14)) +} ` + // goxls: overload decl reference Run(t, files, func(t *testing.T, env *Env) { env.OpenFile("def.gop") loc := env.GoToDefinition(env.RegexpSearch("def.gop", `func (mul) = \(`)) @@ -96,6 +134,26 @@ println mul(1.2, 3.14) t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff) } }) + // goxls: overload member reference + Run(t, files, func(t *testing.T, env *Env) { + env.OpenFile("def.gop") + loc := env.GoToDefinition(env.RegexpSearch("def.gop", `func mul = \(\n\s+(mulInt)`)) + refs, err := env.Editor.References(env.Ctx, loc) + if err != nil { + t.Fatalf("references on (*s).Error failed: %v", err) + } + var buf strings.Builder + for _, ref := range refs { + fmt.Fprintf(&buf, "%s %s\n", env.Sandbox.Workdir.URIToPath(ref.URI), ref.Range) + } + got := buf.String() + want := "def.gop 0:5-0:11\n" + // mulInt + "def.gop 9:4-9:10\n" + // overload mulInt + "test.gop 0:8-0:11\n" // use overload mulInt + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff) + } + }) } func TestReferencesOnOverloadDecl3(t *testing.T) { @@ -103,7 +161,7 @@ func TestReferencesOnOverloadDecl3(t *testing.T) { -- go.mod -- module mod.com -go 1.12 +go 1.19 -- def.gop -- type foo struct { } @@ -121,6 +179,28 @@ func (foo).mul = ( var a *foo var b = a.mul(100) var c = a.mul(a) +-- gop_autogen.go -- +package main + +const _ = true + +type foo struct { +} + +const Gopo_foo_mul = ".mulInt,.mulFoo" +func (a *foo) mulInt(b int) *foo { + return a +} +func (a *foo) mulFoo(b *foo) *foo { + return a +} + +var a *foo +var b = a.mulInt(100) +var c = a.mulFoo(a) + +func main() { +} ` Run(t, files, func(t *testing.T, env *Env) { env.OpenFile("def.gop") @@ -141,4 +221,23 @@ var c = a.mul(a) t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff) } }) + Run(t, files, func(t *testing.T, env *Env) { + env.OpenFile("def.gop") + loc := env.GoToDefinition(env.RegexpSearch("def.gop", `\(foo\)\.(mulInt)`)) + refs, err := env.Editor.References(env.Ctx, loc) + if err != nil { + t.Fatalf("references on (*s).Error failed: %v", err) + } + var buf strings.Builder + for _, ref := range refs { + fmt.Fprintf(&buf, "%s %s\n", env.Sandbox.Workdir.URIToPath(ref.URI), ref.Range) + } + got := buf.String() + want := "def.gop 2:14-2:20\n" + + "def.gop 9:10-9:16\n" + + "test.gop 1:10-1:13\n" + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff) + } + }) }