Skip to content

Commit cdfc8e7

Browse files
committed
fix:overload member reference
1 parent 5c0fff8 commit cdfc8e7

File tree

2 files changed

+104
-7
lines changed

2 files changed

+104
-7
lines changed

gopls/internal/lsp/source/references.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -621,10 +621,8 @@ func localReferences(pkg Package, targets map[types.Object]bool, correspond bool
621621
if id, ok := n.(*gopast.Ident); ok {
622622
if obj, ok := pkg.GopTypesInfo().Uses[id]; ok {
623623
// goxls: use overload declaration to match
624-
if overdecl, overloads := pkg.GopTypesInfo().OverloadOf(id); overdecl != nil && overloads != nil {
625-
obj = overdecl
626-
}
627-
if matches(obj) {
624+
overdecl, _ := pkg.GopTypesInfo().OverloadOf(id)
625+
if matches(obj) || (overdecl != nil && matches(overdecl)) {
628626
report(gopMustLocation(pgf, id), false)
629627
}
630628
}

gopls/internal/regtest/misc/references_gox_test.go

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestReferencesOnOverloadDecl1(t *testing.T) {
1414
-- go.mod --
1515
module mod.com
1616
17-
go 1.12
17+
go 1.19
1818
-- def.gop --
1919
func add = (
2020
func(a, b int) int {
@@ -27,6 +27,22 @@ func add = (
2727
-- test.gop --
2828
println add(1,2)
2929
println add("Hello", "World")
30+
-- gop_autogen.go --
31+
package main
32+
33+
import "fmt"
34+
35+
const _ = true
36+
func add__0(a int, b int) int {
37+
return a + b
38+
}
39+
func add__1(a string, b string) string {
40+
return a + b
41+
}
42+
func main() {
43+
fmt.Println(add__0(1, 2))
44+
fmt.Println(add__1("Hello", "World"))
45+
}
3046
`
3147
Run(t, files, func(t *testing.T, env *Env) {
3248
env.OpenFile("def.gop")
@@ -54,7 +70,7 @@ func TestReferencesOnOverloadDecl2(t *testing.T) {
5470
-- go.mod --
5571
module mod.com
5672
57-
go 1.12
73+
go 1.19
5874
-- def.gop --
5975
func mulInt(a, b int) int {
6076
return a * b
@@ -75,7 +91,29 @@ func mul = (
7591
println mul(100, 7)
7692
println mul("Hello", "World")
7793
println mul(1.2, 3.14)
94+
-- gop_autogen.go --
95+
package main
96+
97+
import "fmt"
98+
99+
const _ = true
100+
const Gopo_mul = "mulInt,,mulFloat"
101+
func mulInt(a int, b int) int {
102+
return a * b
103+
}
104+
func mul__1(a string, b string) string {
105+
return a + b
106+
}
107+
func mulFloat(a float64, b float64) float64 {
108+
return a * b
109+
}
110+
func main() {
111+
fmt.Println(mulInt(100, 7))
112+
fmt.Println(mul__1("Hello", "World"))
113+
fmt.Println(mulFloat(1.2, 3.14))
114+
}
78115
`
116+
// goxls: overload decl reference
79117
Run(t, files, func(t *testing.T, env *Env) {
80118
env.OpenFile("def.gop")
81119
loc := env.GoToDefinition(env.RegexpSearch("def.gop", `func (mul) = \(`))
@@ -96,14 +134,34 @@ println mul(1.2, 3.14)
96134
t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff)
97135
}
98136
})
137+
// goxls: overload member reference
138+
Run(t, files, func(t *testing.T, env *Env) {
139+
env.OpenFile("def.gop")
140+
loc := env.GoToDefinition(env.RegexpSearch("def.gop", `func mul = \(\n\s+(mulInt)`))
141+
refs, err := env.Editor.References(env.Ctx, loc)
142+
if err != nil {
143+
t.Fatalf("references on (*s).Error failed: %v", err)
144+
}
145+
var buf strings.Builder
146+
for _, ref := range refs {
147+
fmt.Fprintf(&buf, "%s %s\n", env.Sandbox.Workdir.URIToPath(ref.URI), ref.Range)
148+
}
149+
got := buf.String()
150+
want := "def.gop 0:5-0:11\n" + // mulInt
151+
"def.gop 9:4-9:10\n" + // overload mulInt
152+
"test.gop 0:8-0:11\n" // use overload mulInt
153+
if diff := cmp.Diff(want, got); diff != "" {
154+
t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff)
155+
}
156+
})
99157
}
100158

101159
func TestReferencesOnOverloadDecl3(t *testing.T) {
102160
const files = `
103161
-- go.mod --
104162
module mod.com
105163
106-
go 1.12
164+
go 1.19
107165
-- def.gop --
108166
type foo struct {
109167
}
@@ -121,6 +179,28 @@ func (foo).mul = (
121179
var a *foo
122180
var b = a.mul(100)
123181
var c = a.mul(a)
182+
-- gop_autogen.go --
183+
package main
184+
185+
const _ = true
186+
187+
type foo struct {
188+
}
189+
190+
const Gopo_foo_mul = ".mulInt,.mulFoo"
191+
func (a *foo) mulInt(b int) *foo {
192+
return a
193+
}
194+
func (a *foo) mulFoo(b *foo) *foo {
195+
return a
196+
}
197+
198+
var a *foo
199+
var b = a.mulInt(100)
200+
var c = a.mulFoo(a)
201+
202+
func main() {
203+
}
124204
`
125205
Run(t, files, func(t *testing.T, env *Env) {
126206
env.OpenFile("def.gop")
@@ -141,4 +221,23 @@ var c = a.mul(a)
141221
t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff)
142222
}
143223
})
224+
Run(t, files, func(t *testing.T, env *Env) {
225+
env.OpenFile("def.gop")
226+
loc := env.GoToDefinition(env.RegexpSearch("def.gop", `\(foo\)\.(mulInt)`))
227+
refs, err := env.Editor.References(env.Ctx, loc)
228+
if err != nil {
229+
t.Fatalf("references on (*s).Error failed: %v", err)
230+
}
231+
var buf strings.Builder
232+
for _, ref := range refs {
233+
fmt.Fprintf(&buf, "%s %s\n", env.Sandbox.Workdir.URIToPath(ref.URI), ref.Range)
234+
}
235+
got := buf.String()
236+
want := "def.gop 2:14-2:20\n" +
237+
"def.gop 9:10-9:16\n" +
238+
"test.gop 1:10-1:13\n"
239+
if diff := cmp.Diff(want, got); diff != "" {
240+
t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff)
241+
}
242+
})
144243
}

0 commit comments

Comments
 (0)