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>
This commit is contained in:
Robert Findley 2022-10-31 20:53:11 -04:00 committed by Gopher Robot
parent 73fcd88827
commit 6e9dc865e2
6 changed files with 21 additions and 4 deletions

View File

@ -94,3 +94,8 @@ func FuzzObjectMethod(f *testing.F) {
}
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"
}

View File

@ -269,7 +269,9 @@ func isTestingType(typ types.Type, testingType string) bool {
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.

View File

@ -1280,7 +1280,9 @@ func isStarTestingDotF(typ types.Type) bool {
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.

View File

@ -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(" //")
}

View File

@ -1,7 +1,7 @@
-- summary --
CallHierarchyCount = 2
CodeLensCount = 5
CompletionsCount = 262
CompletionsCount = 263
CompletionSnippetCount = 106
UnimportedCompletionsCount = 5
DeepCompletionsCount = 5

View File

@ -1,7 +1,7 @@
-- summary --
CallHierarchyCount = 2
CodeLensCount = 5
CompletionsCount = 263
CompletionsCount = 264
CompletionSnippetCount = 115
UnimportedCompletionsCount = 5
DeepCompletionsCount = 5