Skip to content

Commit

Permalink
[gopls-release-branch.0.10] gopls/internal/lsp/source/completion: fix…
Browse files Browse the repository at this point in the history
… 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 6e9dc86)
Reviewed-on: https://go-review.googlesource.com/c/tools/+/446861
  • Loading branch information
findleyr authored and gopherbot committed Nov 1, 2022
1 parent c7b65d8 commit b5bc717
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions go/analysis/passes/tests/testdata/src/a/go118_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 3 additions & 1 deletion go/analysis/passes/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion gopls/internal/lsp/source/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions gopls/internal/lsp/testdata/issues/issue56505.go
Original file line number Diff line number Diff line change
@@ -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(" //")
}
2 changes: 1 addition & 1 deletion gopls/internal/lsp/testdata/summary.txt.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- summary --
CallHierarchyCount = 2
CodeLensCount = 5
CompletionsCount = 262
CompletionsCount = 263
CompletionSnippetCount = 106
UnimportedCompletionsCount = 5
DeepCompletionsCount = 5
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/lsp/testdata/summary_go1.18.txt.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- summary --
CallHierarchyCount = 2
CodeLensCount = 5
CompletionsCount = 263
CompletionsCount = 264
CompletionSnippetCount = 115
UnimportedCompletionsCount = 5
DeepCompletionsCount = 5
Expand Down

0 comments on commit b5bc717

Please sign in to comment.