internal/lsp: make sure that deps are only checked in trimmed mode

This change encodes an invariant that, a dependency package will only
ever be parsed with trimmed ASTs.

Updates golang/go#34410

Change-Id: I2ceab3672c0bae0b98cec2a8e60b92a0c01a900f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/196537
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2019-09-19 12:39:03 -04:00
parent 5adc211bf7
commit db1d4edb46
2 changed files with 13 additions and 13 deletions

View File

@ -151,6 +151,10 @@ func (cph *checkPackageHandle) ID() string {
}
func (cph *checkPackageHandle) Cached(ctx context.Context) (source.Package, error) {
return cph.cached(ctx)
}
func (cph *checkPackageHandle) cached(ctx context.Context) (*pkg, error) {
v := cph.handle.Cached()
if v == nil {
return nil, errors.Errorf("no cached type information for %s", cph.m.pkgPath)
@ -171,13 +175,6 @@ func (imp *importer) parseGoHandles(ctx context.Context, m *metadata) ([]source.
if imp.topLevelPackageID == m.id {
mode = source.ParseFull
}
// If we have the full AST cached, don't bother getting the trimmed version.
if imp.view.session.cache.store.Cached(parseKey{
file: fh.Identity(),
mode: source.ParseFull,
}) != nil {
mode = source.ParseFull
}
phs = append(phs, imp.view.session.cache.ParseGoHandle(fh, mode))
}
return phs, nil
@ -192,13 +189,16 @@ func (imp *importer) Import(pkgPath string) (*types.Package, error) {
return nil, errors.Errorf("no parent package for import %s", pkgPath)
}
// Get the package metadata from the importing package.
// Get the CheckPackageHandle from the importing package.
cph, ok := imp.parentCheckPackageHandle.imports[packagePath(pkgPath)]
if !ok {
return nil, errors.Errorf("no package data for import path %s", pkgPath)
}
// Create a check package handle to get the type information for this package.
for _, ph := range cph.Files() {
if ph.Mode() != source.ParseExported {
panic("dependency parsed in full mode")
}
}
pkg, err := cph.check(ctx)
if err != nil {
return nil, err

View File

@ -44,10 +44,10 @@ type pkg struct {
diagnostics map[*analysis.Analyzer][]source.Diagnostic
}
// packageID is a type that abstracts a package ID.
// Declare explicit types for package paths and IDs to ensure that we never use
// an ID where a path belongs, and vice versa. If we confused the two, it would
// result in confusing errors because package IDs often look like package paths.
type packageID string
// packagePath is a type that abstracts a package path.
type packagePath string
type analysisEntry struct {