Skip to content

Commit 5af564a

Browse files
committed
gopls/internal/cache: add missing error check
Also, minor style tweaks: - use moremaps helpers - use more informative variable names - use explicit field names in comp.lit. Fixes golang/go#74055 Change-Id: I56b27b8c0de491878c955f542d422f8fd6dd9e15 Reviewed-on: https://go-review.googlesource.com/c/tools/+/680258 Reviewed-by: Peter Weinberger <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 80d3570 commit 5af564a

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

gopls/internal/cache/source.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"golang.org/x/tools/gopls/internal/cache/metadata"
1515
"golang.org/x/tools/gopls/internal/cache/symbols"
1616
"golang.org/x/tools/gopls/internal/protocol"
17-
"golang.org/x/tools/internal/event"
17+
"golang.org/x/tools/gopls/internal/util/moremaps"
1818
"golang.org/x/tools/internal/imports"
1919
)
2020

@@ -136,27 +136,27 @@ func (s *goplsSource) ResolveReferences(ctx context.Context, filename string, mi
136136
func (s *goplsSource) resolveCacheReferences(missing imports.References) ([]*result, error) {
137137
ix, err := s.S.view.ModcacheIndex()
138138
if err != nil {
139-
event.Error(s.ctx, "resolveCacheReferences", err)
139+
return nil, err
140140
}
141141

142142
found := make(map[string]*result)
143-
for pkg, nms := range missing {
144-
var ks []string
145-
for k := range nms {
146-
ks = append(ks, k)
147-
}
148-
cs := ix.LookupAll(pkg, ks...) // map[importPath][]Candidate
149-
for k, cands := range cs {
150-
res := found[k]
143+
for pkgName, nameSet := range missing {
144+
names := moremaps.KeySlice(nameSet)
145+
for importPath, cands := range ix.LookupAll(pkgName, names...) {
146+
res := found[importPath]
151147
if res == nil {
152148
res = &result{
153-
&imports.Result{
154-
Import: &imports.ImportInfo{ImportPath: k},
155-
Package: &imports.PackageInfo{Name: pkg, Exports: make(map[string]bool)},
149+
res: &imports.Result{
150+
Import: &imports.ImportInfo{
151+
ImportPath: importPath,
152+
},
153+
Package: &imports.PackageInfo{
154+
Name: pkgName,
155+
Exports: make(map[string]bool)},
156156
},
157-
false,
157+
deprecated: false,
158158
}
159-
found[k] = res
159+
found[importPath] = res
160160
}
161161
for _, c := range cands {
162162
res.res.Package.Exports[c.Name] = true
@@ -166,11 +166,7 @@ func (s *goplsSource) resolveCacheReferences(missing imports.References) ([]*res
166166
}
167167

168168
}
169-
var ans []*result
170-
for _, x := range found {
171-
ans = append(ans, x)
172-
}
173-
return ans, nil
169+
return moremaps.ValueSlice(found), nil
174170
}
175171

176172
type found struct {

internal/modindex/lookup.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ const (
3636
)
3737

3838
// LookupAll only returns those Candidates whose import path
39-
// finds all the nms.
40-
func (ix *Index) LookupAll(pkg string, names ...string) map[string][]Candidate {
39+
// finds all the names.
40+
func (ix *Index) LookupAll(pkgName string, names ...string) map[string][]Candidate {
4141
// this can be made faster when benchmarks show that it needs to be
4242
names = uniquify(names)
4343
byImpPath := make(map[string][]Candidate)
4444
for _, nm := range names {
45-
cands := ix.Lookup(pkg, nm, false)
45+
cands := ix.Lookup(pkgName, nm, false)
4646
for _, c := range cands {
4747
byImpPath[c.ImportPath] = append(byImpPath[c.ImportPath], c)
4848
}
@@ -67,9 +67,9 @@ func uniquify(in []string) []string {
6767

6868
// Lookup finds all the symbols in the index with the given PkgName and name.
6969
// If prefix is true, it finds all of these with name as a prefix.
70-
func (ix *Index) Lookup(pkg, name string, prefix bool) []Candidate {
71-
loc, ok := slices.BinarySearchFunc(ix.Entries, pkg, func(e Entry, pkg string) int {
72-
return strings.Compare(e.PkgName, pkg)
70+
func (ix *Index) Lookup(pkgName, name string, prefix bool) []Candidate {
71+
loc, ok := slices.BinarySearchFunc(ix.Entries, pkgName, func(e Entry, pkg string) int {
72+
return strings.Compare(e.PkgName, pkgName)
7373
})
7474
if !ok {
7575
return nil // didn't find the package
@@ -78,7 +78,7 @@ func (ix *Index) Lookup(pkg, name string, prefix bool) []Candidate {
7878
// loc is the first entry for this package name, but there may be several
7979
for i := loc; i < len(ix.Entries); i++ {
8080
e := ix.Entries[i]
81-
if e.PkgName != pkg {
81+
if e.PkgName != pkgName {
8282
break // end of sorted package names
8383
}
8484
nloc, ok := slices.BinarySearchFunc(e.Names, name, func(s string, name string) int {
@@ -105,7 +105,7 @@ func (ix *Index) Lookup(pkg, name string, prefix bool) []Candidate {
105105
continue // should never happen
106106
}
107107
px := Candidate{
108-
PkgName: pkg,
108+
PkgName: pkgName,
109109
Name: flds[0],
110110
Dir: string(e.Dir),
111111
ImportPath: e.ImportPath,

0 commit comments

Comments
 (0)