Skip to content

Commit b5bc717

Browse files
findleyrgopherbot
authored andcommitted
[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 <[email protected]> Reviewed-by: Alan Donovan <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Findley <[email protected]> (cherry picked from commit 6e9dc86) Reviewed-on: https://go-review.googlesource.com/c/tools/+/446861
1 parent c7b65d8 commit b5bc717

File tree

6 files changed

+21
-4
lines changed

6 files changed

+21
-4
lines changed

go/analysis/passes/tests/testdata/src/a/go118_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,8 @@ func FuzzObjectMethod(f *testing.F) {
9494
}
9595
f.Fuzz(obj.myVar) // ok
9696
}
97+
98+
// Test for golang/go#56505: checking fuzz arguments should not panic on *error.
99+
func FuzzIssue56505(f *testing.F) {
100+
f.Fuzz(func(e *error) {}) // want "the first parameter of a fuzz target must be \\*testing.T"
101+
}

go/analysis/passes/tests/tests.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ func isTestingType(typ types.Type, testingType string) bool {
269269
if !ok {
270270
return false
271271
}
272-
return named.Obj().Pkg().Path() == "testing" && named.Obj().Name() == testingType
272+
obj := named.Obj()
273+
// obj.Pkg is nil for the error type.
274+
return obj != nil && obj.Pkg() != nil && obj.Pkg().Path() == "testing" && obj.Name() == testingType
273275
}
274276

275277
// Validate that fuzz target function's arguments are of accepted types.

gopls/internal/lsp/source/completion/completion.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,9 @@ func isStarTestingDotF(typ types.Type) bool {
12801280
if named == nil {
12811281
return false
12821282
}
1283-
return named.Obj() != nil && named.Obj().Pkg().Path() == "testing" && named.Obj().Name() == "F"
1283+
obj := named.Obj()
1284+
// obj.Pkg is nil for the error type.
1285+
return obj != nil && obj.Pkg() != nil && obj.Pkg().Path() == "testing" && obj.Name() == "F"
12841286
}
12851287

12861288
// lexical finds completions in the lexical environment.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package issues
2+
3+
// Test for golang/go#56505: completion on variables of type *error should not
4+
// panic.
5+
func _() {
6+
var e *error
7+
e.x //@complete(" //")
8+
}

gopls/internal/lsp/testdata/summary.txt.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- summary --
22
CallHierarchyCount = 2
33
CodeLensCount = 5
4-
CompletionsCount = 262
4+
CompletionsCount = 263
55
CompletionSnippetCount = 106
66
UnimportedCompletionsCount = 5
77
DeepCompletionsCount = 5

gopls/internal/lsp/testdata/summary_go1.18.txt.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- summary --
22
CallHierarchyCount = 2
33
CodeLensCount = 5
4-
CompletionsCount = 263
4+
CompletionsCount = 264
55
CompletionSnippetCount = 115
66
UnimportedCompletionsCount = 5
77
DeepCompletionsCount = 5

0 commit comments

Comments
 (0)