Skip to content

Commit 9ffaf69

Browse files
committed
gopls/internal/lsp/cache: minor analysis cleanups
This change moves the *pkg out of the actionHandle into the actionResult. (In future we may not need to load the package before creating the handle.) The actionResult retains only the exported type information, to avoid keeping the syntax trees (etc) live. Also, perform object fact filtering once after the analysis pass, instead of every time it is imported by another analysis. Also, filter out unexported constants. Also, log internal errors even when we don't bug.Reportf, since errors merely returned by analysis are usually ignored, making it harder to understand the analysis crashes. Change-Id: I7257e8da5dc269090bff2817409948e46dcf7176 Reviewed-on: https://go-review.googlesource.com/c/tools/+/442556 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]> Run-TryBot: Alan Donovan <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent ffb862b commit 9ffaf69

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

gopls/internal/lsp/cache/analysis.go

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ type actionHandleKey source.Hash
7373
// package (as different analyzers are applied, either in sequence or
7474
// parallel), and across packages (as dependencies are analyzed).
7575
type actionHandle struct {
76+
key actionKey // just for String()
7677
promise *memoize.Promise // [actionResult]
77-
78-
analyzer *analysis.Analyzer
79-
pkg *pkg
8078
}
8179

8280
// actionData is the successful result of analyzing a package.
8381
type actionData struct {
82+
analyzer *analysis.Analyzer
83+
pkgTypes *types.Package // types only; don't keep syntax live
8484
diagnostics []*source.Diagnostic
8585
result interface{}
8686
objectFacts map[objectFactKey]analysis.Fact
@@ -168,9 +168,8 @@ func (s *snapshot) actionHandle(ctx context.Context, id PackageID, a *analysis.A
168168
})
169169

170170
ah := &actionHandle{
171-
analyzer: a,
172-
pkg: pkg,
173-
promise: promise,
171+
key: key,
172+
promise: promise,
174173
}
175174

176175
s.mu.Lock()
@@ -191,8 +190,12 @@ func buildActionKey(a *analysis.Analyzer, ph *packageHandle) actionHandleKey {
191190
return actionHandleKey(source.Hashf("%p%s", a, ph.key[:]))
192191
}
193192

193+
func (key actionKey) String() string {
194+
return fmt.Sprintf("%s@%s", key.analyzer, key.pkgid)
195+
}
196+
194197
func (act *actionHandle) String() string {
195-
return fmt.Sprintf("%s@%s", act.analyzer, act.pkg.PkgPath())
198+
return act.key.String()
196199
}
197200

198201
// actionImpl runs the analysis for action node (analyzer, pkg),
@@ -222,29 +225,20 @@ func actionImpl(ctx context.Context, snapshot *snapshot, deps []*actionHandle, a
222225

223226
mu.Lock()
224227
defer mu.Unlock()
225-
if dep.pkg == pkg {
228+
if data.pkgTypes == pkg.types {
226229
// Same package, different analysis (horizontal edge):
227230
// in-memory outputs of prerequisite analyzers
228231
// become inputs to this analysis pass.
229-
inputs[dep.analyzer] = data.result
232+
inputs[data.analyzer] = data.result
230233

231-
} else if dep.analyzer == analyzer {
234+
} else if data.analyzer == analyzer {
232235
// Same analysis, different package (vertical edge):
233236
// serialized facts produced by prerequisite analysis
234237
// become available to this analysis pass.
235238
for key, fact := range data.objectFacts {
236-
// Filter out facts related to objects
237-
// that are irrelevant downstream
238-
// (equivalently: not in the compiler export data).
239-
if !exportedFrom(key.obj, dep.pkg.types) {
240-
continue
241-
}
242239
objectFacts[key] = fact
243240
}
244241
for key, fact := range data.packageFacts {
245-
// TODO: filter out facts that belong to
246-
// packages not mentioned in the export data
247-
// to prevent side channels.
248242
packageFacts[key] = fact
249243
}
250244

@@ -268,7 +262,11 @@ func actionImpl(ctx context.Context, snapshot *snapshot, deps []*actionHandle, a
268262
if source.IsCommandLineArguments(pkg.ID()) {
269263
errorf = fmt.Errorf // suppress reporting
270264
}
271-
return errorf("internal error: unexpected analysis dependency %s@%s -> %s", analyzer.Name, pkg.ID(), dep)
265+
err := errorf("internal error: unexpected analysis dependency %s@%s -> %s", analyzer.Name, pkg.ID(), dep)
266+
// Log the event in any case, as the ultimate
267+
// consumer of actionResult ignores errors.
268+
event.Error(ctx, "analysis", err)
269+
return err
272270
}
273271
return nil
274272
})
@@ -401,6 +399,16 @@ func actionImpl(ctx context.Context, snapshot *snapshot, deps []*actionHandle, a
401399
panic(fmt.Sprintf("%s:%s: Pass.ExportPackageFact(%T) called after Run", analyzer.Name, pkg.PkgPath(), fact))
402400
}
403401

402+
// Filter out facts related to objects that are irrelevant downstream
403+
// (equivalently: not in the compiler export data).
404+
for key := range objectFacts {
405+
if !exportedFrom(key.obj, pkg.types) {
406+
delete(objectFacts, key)
407+
}
408+
}
409+
// TODO: filter out facts that belong to packages not
410+
// mentioned in the export data to prevent side channels.
411+
404412
var diagnostics []*source.Diagnostic
405413
for _, diag := range rawDiagnostics {
406414
srcDiags, err := analysisDiagnosticDiagnostics(snapshot, pkg, analyzer, &diag)
@@ -411,6 +419,8 @@ func actionImpl(ctx context.Context, snapshot *snapshot, deps []*actionHandle, a
411419
diagnostics = append(diagnostics, srcDiags...)
412420
}
413421
return &actionData{
422+
analyzer: analyzer,
423+
pkgTypes: pkg.types,
414424
diagnostics: diagnostics,
415425
result: result,
416426
objectFacts: objectFacts,
@@ -434,8 +444,10 @@ func exportedFrom(obj types.Object, pkg *types.Package) bool {
434444
case *types.Var:
435445
return obj.Exported() && obj.Pkg() == pkg ||
436446
obj.IsField()
437-
case *types.TypeName, *types.Const:
447+
case *types.TypeName:
438448
return true
449+
case *types.Const:
450+
return obj.Exported() && obj.Pkg() == pkg
439451
}
440452
return false // Nil, Builtin, Label, or PkgName
441453
}

0 commit comments

Comments
 (0)