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 <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
diff --git a/gopls/internal/regtest/misc/hover_test.go b/gopls/internal/regtest/misc/hover_test.go
index 1442178..fbd0ac5 100644
--- a/gopls/internal/regtest/misc/hover_test.go
+++ b/gopls/internal/regtest/misc/hover_test.go
@@ -99,3 +99,17 @@
}
})
}
+
+// 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 f7fb3cb..0bc92d1 100644
--- a/internal/lsp/source/hover.go
+++ b/internal/lsp/source/hover.go
@@ -342,7 +342,6 @@
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