gopls/test/completion: fix reported bug in collectCompletions The bug reporter has reported multiple occurrences of an unexpected case in determining completions, namely completing in notypeinfo.(type) This code handles that case, and tests that the requested completion no longer panics. Fixes: golang/go#75192 Change-Id: I191468430c7554301e0ecd7b17d24e97e1bbfa7d Reviewed-on: https://go-review.googlesource.com/c/tools/+/805221 Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/gopls/internal/golang/completion/completion.go b/gopls/internal/golang/completion/completion.go index dc9b96a..f9f53b7 100644 --- a/gopls/internal/golang/completion/completion.go +++ b/gopls/internal/golang/completion/completion.go
@@ -1379,14 +1379,11 @@ prefix := sel.Sel.Name if c.surrounding != nil { if c.surrounding.content != sel.Sel.Name { - // the bug reports do not include the Reportf strings just the line numbers - if len(c.surrounding.content) == 0 { - bug.Reportf("surrounding is empty, should be %q", sel.Sel.Name) - } else if len(sel.Sel.Name) == 0 { - bug.Reportf("sel.Sel.Name is empty, should be %q", c.surrounding.content) - } else { + if sel.Sel.Name != "_" && sel.Sel.Name != "" { bug.Reportf("unexpected surrounding: %q != %q", c.surrounding.content, sel.Sel.Name) + } + prefix = c.surrounding.Prefix() } else { prefix = sel.Sel.Name[:c.surrounding.cursor-c.surrounding.start] }
diff --git a/gopls/internal/test/integration/completion/completion_test.go b/gopls/internal/test/integration/completion/completion_test.go index 7c5e5ef..b110046 100644 --- a/gopls/internal/test/integration/completion/completion_test.go +++ b/gopls/internal/test/integration/completion/completion_test.go
@@ -1688,3 +1688,25 @@ } }) } + +// Check that the completion code no longer panics. +func TestIssue75192(t *testing.T) { + const src = ` +-- go.mod -- +module mod.com +go 1.22 +-- main.go -- +package main + +func main() { + _ = notypeinfo.(type) +} +` + Run(t, src, func(t *testing.T, env *Env) { + env.OpenFile("main.go") + env.Await(env.DoneWithOpen()) + loc := env.RegexpSearch("main.go", `notypeinfo\.\(ty()pe\)`) + // this used to panic. + env.Completion(loc) + }) +}