internal/lsp/source: check for nil objects in call hierarchy

Fixes golang/go#49125

Change-Id: Id91415377240ab87aeaddb9d668281509d073510
Reviewed-on: https://go-review.googlesource.com/c/tools/+/358549
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Robert Findley 2021-10-26 16:51:01 -04:00
parent e1e2965795
commit d5520c2912
2 changed files with 37 additions and 1 deletions

View File

@ -0,0 +1,35 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package misc
import (
"testing"
"golang.org/x/tools/internal/lsp/protocol"
. "golang.org/x/tools/internal/lsp/regtest"
)
// Test for golang/go#49125
func TestCallHierarchy_Issue49125(t *testing.T) {
const files = `
-- go.mod --
module mod.com
go 1.12
-- p.go --
package pkg
`
// TODO(rfindley): this could probably just be a marker test.
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("p.go")
pos := env.RegexpSearch("p.go", "pkg")
var params protocol.CallHierarchyPrepareParams
params.TextDocument.URI = env.Sandbox.Workdir.URI("p.go")
params.Position = pos.ToProtocolPosition()
// Check that this doesn't panic.
env.Editor.Server.PrepareCallHierarchy(env.Ctx, &params)
})
}

View File

@ -32,8 +32,9 @@ func PrepareCallHierarchy(ctx context.Context, snapshot Snapshot, fh FileHandle,
}
return nil, err
}
// The identifier can be nil if it is an import spec.
if identifier == nil {
if identifier == nil || identifier.Declaration.obj == nil {
return nil, nil
}