internal/lsp: fix nil pointer in hover when (types.Object).Pkg() is nil

We haven't been able to reproduce this scenario, but it may be possible
when the user is in a broken state. Avoid panicking by gating every
use of obj.Pkg() with nil checks.

Fixes golang/go#44300

Change-Id: Ia0c56a7fd5d6b89795dded1efdf05838f3de8209
Reviewed-on: https://go-review.googlesource.com/c/tools/+/292671
Trust: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
(cherry picked from commit ffc207509abf826f12724821620192fb29524064)
Reviewed-on: https://go-review.googlesource.com/c/tools/+/292730
Reviewed-by: Robert Findley <rfindley@google.com>
diff --git a/internal/lsp/source/hover.go b/internal/lsp/source/hover.go
index 1e3e789..21e1c57 100644
--- a/internal/lsp/source/hover.go
+++ b/internal/lsp/source/hover.go
@@ -187,6 +187,10 @@
 			}
 		}
 	}
+	if obj.Pkg() == nil {
+		event.Log(ctx, fmt.Sprintf("nil package for %s", obj))
+		return h, nil
+	}
 	h.importPath = obj.Pkg().Path()
 	h.LinkPath = h.importPath
 	if mod, version, ok := moduleAtVersion(h.LinkPath, i); ok {