Skip to content

Commit

Permalink
test:overload anonymous reference
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed May 13, 2024
1 parent b2ea1ec commit 2cbaf58
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
8 changes: 7 additions & 1 deletion gopls/internal/lsp/source/references_gox.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package source

import (
"bytes"
"context"
"fmt"
"go/token"
Expand Down Expand Up @@ -586,10 +587,15 @@ func gopObjectsAt(info *typesutil.Info, file *ast.File, pos token.Pos) (map[type
}
targets[obj] = leaf
case *ast.FuncLit:
// Look up the implicit *types.Func (overload member)
obj := info.Implicits[leaf]
if obj == nil {
var buf bytes.Buffer
typesutil.WriteExpr(&buf, leaf)
return nil, nil, fmt.Errorf("%w for %q", errNoObjectFound, buf.String())
}
targets[obj] = leaf
}

if len(targets) == 0 {
return nil, nil, fmt.Errorf("objectAt: internal error: no targets") // can't happen
}
Expand Down
51 changes: 51 additions & 0 deletions gopls/internal/regtest/misc/references_gox_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package misc

import (
"fmt"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
. "golang.org/x/tools/gopls/internal/lsp/regtest"
)

func TestReferencesOnOverloadMember(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.12
-- def.gop --
func add = (
func(a, b int) int {
return a + b
}
func(a, b string) string {
return a + b
}
)
-- test.gop --
println add(1,2)
println add("Hello", "World")
println add("Bye", "World")
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("test.gop")
loc := env.GoToDefinition(env.RegexpSearch("test.gop", `println (add)\("Hello", "World"\)`))
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 4:1-4:25\n" + // anonymous overload func
"test.gop 1:8-1:11\n" +
"test.gop 2:8-2:11\n"
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("unexpected references on (*s).Error (-want +got):\n%s", diff)
}
})
}

0 comments on commit 2cbaf58

Please sign in to comment.