@@ -17,6 +17,7 @@ import (
17
17
"golang.org/x/tools/gopls/internal/cache/metadata"
18
18
"golang.org/x/tools/gopls/internal/cache/parsego"
19
19
"golang.org/x/tools/gopls/internal/protocol"
20
+ "golang.org/x/tools/gopls/internal/util/bug"
20
21
"golang.org/x/tools/gopls/internal/util/frob"
21
22
)
22
23
@@ -43,15 +44,6 @@ func Index(files []*parsego.File, pkg *types.Package, info *types.Info) []byte {
43
44
objectpathFor := new (objectpath.Encoder ).For
44
45
45
46
for fileIndex , pgf := range files {
46
-
47
- nodeRange := func (n ast.Node ) protocol.Range {
48
- rng , err := pgf .PosRange (n .Pos (), n .End ())
49
- if err != nil {
50
- panic (err ) // can't fail
51
- }
52
- return rng
53
- }
54
-
55
47
ast .Inspect (pgf .File , func (n ast.Node ) bool {
56
48
switch n := n .(type ) {
57
49
case * ast.Ident :
@@ -82,10 +74,15 @@ func Index(files []*parsego.File, pkg *types.Package, info *types.Info) []byte {
82
74
objects [obj ] = gobObj
83
75
}
84
76
85
- gobObj .Refs = append (gobObj .Refs , gobRef {
86
- FileIndex : fileIndex ,
87
- Range : nodeRange (n ),
88
- })
77
+ // golang/go#66683: nodes can under/overflow the file.
78
+ // For example, "var _ = x." creates a SelectorExpr(Sel=Ident("_"))
79
+ // that is beyond EOF. (Arguably Ident.Name should be "".)
80
+ if rng , err := pgf .NodeRange (n ); err == nil {
81
+ gobObj .Refs = append (gobObj .Refs , gobRef {
82
+ FileIndex : fileIndex ,
83
+ Range : rng ,
84
+ })
85
+ }
89
86
}
90
87
}
91
88
@@ -102,10 +99,15 @@ func Index(files []*parsego.File, pkg *types.Package, info *types.Info) []byte {
102
99
gobObj = & gobObject {Path : "" }
103
100
objects [nil ] = gobObj
104
101
}
105
- gobObj .Refs = append (gobObj .Refs , gobRef {
106
- FileIndex : fileIndex ,
107
- Range : nodeRange (n .Path ),
108
- })
102
+ // golang/go#66683: nodes can under/overflow the file.
103
+ if rng , err := pgf .NodeRange (n .Path ); err == nil {
104
+ gobObj .Refs = append (gobObj .Refs , gobRef {
105
+ FileIndex : fileIndex ,
106
+ Range : rng ,
107
+ })
108
+ } else {
109
+ bug .Reportf ("out of bounds import spec %+v" , n .Path )
110
+ }
109
111
}
110
112
return true
111
113
})
0 commit comments