[gopls-release-branch.0.10] gopls/internal/lsp/source/completion: fix panic in completion on *error

Fix a panic during completion on variables of type *error. As a
predeclared type, the error type has nil package. Fix the crash
resulting from this oversight, as well as a related crash in the tests
analyzer, from which the new completion code was adapted.

Fixes golang/go#56505

Change-Id: I0707924d0666b238821fd14b6fc34639cc7a9c53
Reviewed-on: https://go-review.googlesource.com/c/tools/+/446815
Auto-Submit: Robert Findley <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
(cherry picked from commit 6e9dc865e2d3de2afb0dc7096f92fa4b3995b5b5)
Reviewed-on: https://go-review.googlesource.com/c/tools/+/446861
diff --git a/go/analysis/passes/tests/testdata/src/a/go118_test.go b/go/analysis/passes/tests/testdata/src/a/go118_test.go
index dc898da..e2bc3f3 100644
--- a/go/analysis/passes/tests/testdata/src/a/go118_test.go
+++ b/go/analysis/passes/tests/testdata/src/a/go118_test.go
@@ -94,3 +94,8 @@
 	}
 	f.Fuzz(obj.myVar) // ok
 }
+
+// Test for golang/go#56505: checking fuzz arguments should not panic on *error.
+func FuzzIssue56505(f *testing.F) {
+	f.Fuzz(func(e *error) {}) // want "the first parameter of a fuzz target must be \\*testing.T"
+}
diff --git a/go/analysis/passes/tests/tests.go b/go/analysis/passes/tests/tests.go
index cab2fa2..935aad0 100644
--- a/go/analysis/passes/tests/tests.go
+++ b/go/analysis/passes/tests/tests.go
@@ -269,7 +269,9 @@
 	if !ok {
 		return false
 	}
-	return named.Obj().Pkg().Path() == "testing" && named.Obj().Name() == testingType
+	obj := named.Obj()
+	// obj.Pkg is nil for the error type.
+	return obj != nil && obj.Pkg() != nil && obj.Pkg().Path() == "testing" && obj.Name() == testingType
 }
 
 // Validate that fuzz target function's arguments are of accepted types.
diff --git a/gopls/internal/lsp/source/completion/completion.go b/gopls/internal/lsp/source/completion/completion.go
index 7d98205..c3b7c2b 100644
--- a/gopls/internal/lsp/source/completion/completion.go
+++ b/gopls/internal/lsp/source/completion/completion.go
@@ -1280,7 +1280,9 @@
 	if named == nil {
 		return false
 	}
-	return named.Obj() != nil && named.Obj().Pkg().Path() == "testing" && named.Obj().Name() == "F"
+	obj := named.Obj()
+	// obj.Pkg is nil for the error type.
+	return obj != nil && obj.Pkg() != nil && obj.Pkg().Path() == "testing" && obj.Name() == "F"
 }
 
 // lexical finds completions in the lexical environment.
diff --git a/gopls/internal/lsp/testdata/issues/issue56505.go b/gopls/internal/lsp/testdata/issues/issue56505.go
new file mode 100644
index 0000000..8c641bf
--- /dev/null
+++ b/gopls/internal/lsp/testdata/issues/issue56505.go
@@ -0,0 +1,8 @@
+package issues
+
+// Test for golang/go#56505: completion on variables of type *error should not
+// panic.
+func _() {
+	var e *error
+	e.x //@complete(" //")
+}
diff --git a/gopls/internal/lsp/testdata/summary.txt.golden b/gopls/internal/lsp/testdata/summary.txt.golden
index 107e900..cfe8e4a 100644
--- a/gopls/internal/lsp/testdata/summary.txt.golden
+++ b/gopls/internal/lsp/testdata/summary.txt.golden
@@ -1,7 +1,7 @@
 -- summary --
 CallHierarchyCount = 2
 CodeLensCount = 5
-CompletionsCount = 262
+CompletionsCount = 263
 CompletionSnippetCount = 106
 UnimportedCompletionsCount = 5
 DeepCompletionsCount = 5
diff --git a/gopls/internal/lsp/testdata/summary_go1.18.txt.golden b/gopls/internal/lsp/testdata/summary_go1.18.txt.golden
index 1e1c876..2b7bf97 100644
--- a/gopls/internal/lsp/testdata/summary_go1.18.txt.golden
+++ b/gopls/internal/lsp/testdata/summary_go1.18.txt.golden
@@ -1,7 +1,7 @@
 -- summary --
 CallHierarchyCount = 2
 CodeLensCount = 5
-CompletionsCount = 263
+CompletionsCount = 264
 CompletionSnippetCount = 115
 UnimportedCompletionsCount = 5
 DeepCompletionsCount = 5