diff --git a/gopls/internal/regtest/misc/hover_test.go b/gopls/internal/regtest/misc/hover_test.go index 1442178034..fbd0ac5c9e 100644 --- a/gopls/internal/regtest/misc/hover_test.go +++ b/gopls/internal/regtest/misc/hover_test.go @@ -99,3 +99,17 @@ func main() { } }) } + +// Tests that hovering does not trigger the panic in golang/go#48249. +func TestPanicInHoverBrokenCode(t *testing.T) { + testenv.NeedsGo1Point(t, 13) + const source = ` +-- main.go -- +package main + +type Example struct` + Run(t, source, func(t *testing.T, env *Env) { + env.OpenFile("main.go") + env.Editor.Hover(env.Ctx, "main.go", env.RegexpSearch("main.go", "Example")) + }) +} diff --git a/internal/lsp/source/hover.go b/internal/lsp/source/hover.go index f7fb3cb690..0bc92d1fb4 100644 --- a/internal/lsp/source/hover.go +++ b/internal/lsp/source/hover.go @@ -342,7 +342,6 @@ func HoverInfo(ctx context.Context, s Snapshot, pkg Package, obj types.Object, p var spec ast.Spec for _, s := range node.Specs { // Avoid panics by guarding the calls to token.Offset (golang/go#48249). - // TODO(rstambler): Investigate this further and adjust if needed. if InRange(tok2, s.Pos()) && InRange(tok2, s.End()) && tok2.Offset(s.Pos()) <= offset && offset <= tok2.Offset(s.End()) { spec = s break