From 295a39ba3c2f2fbb24c08da87cd94fe098a41c03 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Thu, 9 Sep 2021 17:24:36 -0400 Subject: [PATCH] internal/lsp: remove the TODO and add a test for the hover panic Fixes golang/go#48249 Change-Id: I86da0f185f414848bf89243737668f1d427c3e4c Reviewed-on: https://go-review.googlesource.com/c/tools/+/348969 Trust: Rebecca Stambler Run-TryBot: Rebecca Stambler Reviewed-by: Robert Findley gopls-CI: kokoro --- gopls/internal/regtest/misc/hover_test.go | 14 ++++++++++++++ internal/lsp/source/hover.go | 1 - 2 files changed, 14 insertions(+), 1 deletion(-) 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