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 { if lpkg.needsrc {
srcPkgs = append(srcPkgs, lpkg) srcPkgs = append(srcPkgs, lpkg)
} }
if ld.Mode&NeedTypesSizes != 0 {
lpkg.TypesSizes = ld.sizes
}
stack = stack[:len(stack)-1] // pop stack = stack[:len(stack)-1] // pop
lpkg.color = black 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) { func (v *View) checkMetadata(ctx context.Context, f *File) ([]packages.Error, error) {
if v.reparseImports(ctx, f, f.filename) { if v.reparseImports(ctx, f, f.filename) {
cfg := v.Config cfg := v.Config
cfg.Mode = packages.LoadImports cfg.Mode = packages.LoadImports | packages.NeedTypesSizes
pkgs, err := packages.Load(&cfg, fmt.Sprintf("file=%s", f.filename)) pkgs, err := packages.Load(&cfg, fmt.Sprintf("file=%s", f.filename))
if len(pkgs) == 0 { if len(pkgs) == 0 {
if err == nil { 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] m, ok := v.mcache.packages[pkgPath]
if !ok { if !ok {
m = &metadata{ m = &metadata{
pkgPath: pkgPath, pkgPath: pkgPath,
id: pkg.ID, id: pkg.ID,
parents: make(map[string]bool), typesSizes: pkg.TypesSizes,
children: make(map[string]bool), parents: make(map[string]bool),
children: make(map[string]bool),
} }
v.mcache.packages[pkgPath] = m v.mcache.packages[pkgPath] = m
} }
@ -225,11 +226,12 @@ func (imp *importer) typeCheck(pkgPath string) (*Package, error) {
typ = types.NewPackage(meta.pkgPath, meta.name) typ = types.NewPackage(meta.pkgPath, meta.name)
} }
pkg := &Package{ pkg := &Package{
id: meta.id, id: meta.id,
pkgPath: meta.pkgPath, pkgPath: meta.pkgPath,
files: meta.files, files: meta.files,
imports: make(map[string]*Package), imports: make(map[string]*Package),
types: typ, types: typ,
typesSizes: meta.typesSizes,
typesInfo: &types.Info{ typesInfo: &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue), Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object), Defs: make(map[*ast.Ident]types.Object),

View File

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

View File

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

View File

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

View File

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