Skip to content

Commit cab6608

Browse files
committed
gopls/internal/golang/completion: fix crash adding receiver type params
Fix an inaccurate assumption that type names in receiver position must be Named or Alias types. In the presence of invalid code, such objects could also be have Basic type. Fixes golang/go#71044 Change-Id: Iad6a3f09aa210e7eee2edf82cec6a990bc137e17 Reviewed-on: https://go-review.googlesource.com/c/tools/+/643016 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 32c4665 commit cab6608

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

gopls/internal/golang/completion/format.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"golang.org/x/tools/gopls/internal/util/typesutil"
2121
"golang.org/x/tools/internal/event"
2222
"golang.org/x/tools/internal/imports"
23-
"golang.org/x/tools/internal/typesinternal"
2423
)
2524

2625
var (
@@ -60,9 +59,12 @@ func (c *completer) item(ctx context.Context, cand candidate) (CompletionItem, e
6059
if obj.Type() == nil {
6160
detail = ""
6261
}
63-
if isTypeName(obj) && c.wantTypeParams() {
64-
// obj is a *types.TypeName, so its type must be Alias|Named.
65-
tparams := typesinternal.TypeParams(obj.Type().(typesinternal.NamedOrAlias))
62+
63+
type hasTypeParams interface{ TypeParams() *types.TypeParamList }
64+
if genericType, _ := obj.Type().(hasTypeParams); genericType != nil && isTypeName(obj) && c.wantTypeParams() {
65+
// golang/go#71044: note that type names can be basic types, even in
66+
// receiver position, for invalid code.
67+
tparams := genericType.TypeParams()
6668
label += typesutil.FormatTypeParams(tparams)
6769
insert = label // maintain invariant above (label == insert)
6870
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
This test checks that we don't crash while completing receivers that may happen
2+
to be builtin types (due to invalid code). This crash was reported by telemetry
3+
in golang/go#71044.
4+
5+
-- flags --
6+
-ignore_extra_diags
7+
8+
-- go.mod --
9+
module example.com/amap
10+
11+
go 1.18
12+
13+
-- a.go --
14+
package amap
15+
16+
import "unsafe"
17+
18+
func (unsafe.Pointer) _() {} //@ rank("unsafe")

0 commit comments

Comments
 (0)