internal/lsp: use new go/packages LoadMode to get TypesSizes

This change also fixes the corresponding code in go/packages, which was
actually not filling in the TypesSizes if the bit was set.

Change-Id: I2d5a849045768a81c94218eb41da2faec26189a3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/170010
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2019-03-29 16:39:22 -04:00
parent 24738cbdc1
commit 73054e8977
6 changed files with 29 additions and 17 deletions

View File

@ -554,6 +554,9 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) {
if lpkg.needsrc {
srcPkgs = append(srcPkgs, lpkg)
}
if ld.Mode&NeedTypesSizes != 0 {
lpkg.TypesSizes = ld.sizes
}
stack = stack[:len(stack)-1] // pop
lpkg.color = black

View File

@ -92,7 +92,7 @@ func (v *View) cachePackage(pkg *Package) {
func (v *View) checkMetadata(ctx context.Context, f *File) ([]packages.Error, error) {
if v.reparseImports(ctx, f, f.filename) {
cfg := v.Config
cfg.Mode = packages.LoadImports
cfg.Mode = packages.LoadImports | packages.NeedTypesSizes
pkgs, err := packages.Load(&cfg, fmt.Sprintf("file=%s", f.filename))
if len(pkgs) == 0 {
if err == nil {
@ -139,10 +139,11 @@ func (v *View) link(pkgPath string, pkg *packages.Package, parent *metadata) *me
m, ok := v.mcache.packages[pkgPath]
if !ok {
m = &metadata{
pkgPath: pkgPath,
id: pkg.ID,
parents: make(map[string]bool),
children: make(map[string]bool),
pkgPath: pkgPath,
id: pkg.ID,
typesSizes: pkg.TypesSizes,
parents: make(map[string]bool),
children: make(map[string]bool),
}
v.mcache.packages[pkgPath] = m
}
@ -225,11 +226,12 @@ func (imp *importer) typeCheck(pkgPath string) (*Package, error) {
typ = types.NewPackage(meta.pkgPath, meta.name)
}
pkg := &Package{
id: meta.id,
pkgPath: meta.pkgPath,
files: meta.files,
imports: make(map[string]*Package),
types: typ,
id: meta.id,
pkgPath: meta.pkgPath,
files: meta.files,
imports: make(map[string]*Package),
types: typ,
typesSizes: meta.typesSizes,
typesInfo: &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),

View File

@ -21,6 +21,7 @@ type Package struct {
imports map[string]*Package
types *types.Package
typesInfo *types.Info
typesSizes types.Sizes
// The analysis cache holds analysis information for all the packages in a view.
// Each graph node (action) is one unit of analysis.
@ -118,6 +119,10 @@ func (pkg *Package) GetTypesInfo() *types.Info {
return pkg.typesInfo
}
func (pkg *Package) GetTypesSizes() types.Sizes {
return pkg.typesSizes
}
func (pkg *Package) IsIllTyped() bool {
return pkg.types == nil && pkg.typesInfo == nil
}

View File

@ -7,6 +7,7 @@ package cache
import (
"context"
"go/token"
"go/types"
"os"
"sync"
@ -64,6 +65,7 @@ type metadataCache struct {
type metadata struct {
id, pkgPath, name string
files []string
typesSizes types.Sizes
parents, children map[string]bool
}

View File

@ -133,13 +133,12 @@ func (act *Action) execOnce(fset *token.FileSet) {
// Run the analysis.
pass := &analysis.Pass{
Analyzer: act.Analyzer,
Fset: fset,
Files: act.Pkg.GetSyntax(),
Pkg: act.Pkg.GetTypes(),
TypesInfo: act.Pkg.GetTypesInfo(),
// TODO(rstambler): Get real TypeSizes from go/packages (golang.org/issues/30139).
TypesSizes: &types.StdSizes{},
Analyzer: act.Analyzer,
Fset: fset,
Files: act.Pkg.GetSyntax(),
Pkg: act.Pkg.GetTypes(),
TypesInfo: act.Pkg.GetTypesInfo(),
TypesSizes: act.Pkg.GetTypesSizes(),
ResultOf: inputs,
Report: func(d analysis.Diagnostic) { act.diagnostics = append(act.diagnostics, d) },
ImportObjectFact: act.importObjectFact,

View File

@ -45,6 +45,7 @@ type Package interface {
GetErrors() []packages.Error
GetTypes() *types.Package
GetTypesInfo() *types.Info
GetTypesSizes() types.Sizes
IsIllTyped() bool
GetActionGraph(ctx context.Context, a *analysis.Analyzer) (*Action, error)
}