go/packages: initialise Fset if mode >= Types
...as it is required not just for Syntax processing but also when loading export data. Clarify comments surrounding Types, Fset. Remove bogus comment re: Dir. Change-Id: I8d234bc311d73cbed4fd271ef883ebba22ba2708 Reviewed-on: https://go-review.googlesource.com/128595 Run-TryBot: Michael Matloob <matloob@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
parent
e832138124
commit
201986631b
|
@ -42,16 +42,16 @@ const (
|
||||||
LoadImports
|
LoadImports
|
||||||
|
|
||||||
// LoadTypes adds type information for the package's exported symbols.
|
// LoadTypes adds type information for the package's exported symbols.
|
||||||
// Package fields added: Types, IllTyped.
|
// Package fields added: Types, Fset, IllTyped.
|
||||||
LoadTypes
|
LoadTypes
|
||||||
|
|
||||||
// LoadSyntax adds typed syntax trees for the packages matching the patterns.
|
// LoadSyntax adds typed syntax trees for the packages matching the patterns.
|
||||||
// Package fields added: Syntax, TypesInfo, Fset, for direct pattern matches only.
|
// Package fields added: Syntax, TypesInfo, for direct pattern matches only.
|
||||||
LoadSyntax
|
LoadSyntax
|
||||||
|
|
||||||
// LoadAllSyntax adds typed syntax trees for the packages matching the patterns
|
// LoadAllSyntax adds typed syntax trees for the packages matching the patterns
|
||||||
// and all dependencies.
|
// and all dependencies.
|
||||||
// Package fields added: Syntax, TypesInfo, Fset, for all packages in import graph.
|
// Package fields added: Syntax, TypesInfo, for all packages in import graph.
|
||||||
LoadAllSyntax
|
LoadAllSyntax
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -95,7 +95,8 @@ type Config struct {
|
||||||
// TODO(rsc): What happens in the Metadata loader? Currently nothing.
|
// TODO(rsc): What happens in the Metadata loader? Currently nothing.
|
||||||
Error func(error)
|
Error func(error)
|
||||||
|
|
||||||
// Fset is the token.FileSet to use when parsing loaded source files.
|
// Fset is the token.FileSet to use when parsing source files or
|
||||||
|
// type information provided by the build system.
|
||||||
// If Fset is nil, the loader will create one.
|
// If Fset is nil, the loader will create one.
|
||||||
Fset *token.FileSet
|
Fset *token.FileSet
|
||||||
|
|
||||||
|
@ -199,8 +200,16 @@ type Package struct {
|
||||||
|
|
||||||
// Types is the type information for the package.
|
// Types is the type information for the package.
|
||||||
// Modes LoadTypes and above set this field for all packages.
|
// Modes LoadTypes and above set this field for all packages.
|
||||||
|
//
|
||||||
|
// TODO(adonovan): all packages? In Types mode this entails
|
||||||
|
// asymptotically more export data processing than is required
|
||||||
|
// to load the requested packages. Is that what we want?
|
||||||
Types *types.Package
|
Types *types.Package
|
||||||
|
|
||||||
|
// Fset provides position information for Types, TypesInfo, and Syntax.
|
||||||
|
// Modes LoadTypes and above set this for field for all packages.
|
||||||
|
Fset *token.FileSet
|
||||||
|
|
||||||
// IllTyped indicates whether the package has any type errors.
|
// IllTyped indicates whether the package has any type errors.
|
||||||
// Modes LoadTypes and above set this field for all packages.
|
// Modes LoadTypes and above set this field for all packages.
|
||||||
IllTyped bool
|
IllTyped bool
|
||||||
|
@ -214,11 +223,6 @@ type Package struct {
|
||||||
// TypesInfo is the type-checking results for the package's syntax trees.
|
// TypesInfo is the type-checking results for the package's syntax trees.
|
||||||
// It is set only when Syntax is set.
|
// It is set only when Syntax is set.
|
||||||
TypesInfo *types.Info
|
TypesInfo *types.Info
|
||||||
|
|
||||||
// Fset is the token.FileSet for the package's syntax trees listed in Syntax.
|
|
||||||
// It is set only when Syntax is set.
|
|
||||||
// All packages loaded together share a single Fset.
|
|
||||||
Fset *token.FileSet
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// loaderPackage augments Package with state used during the loading phase
|
// loaderPackage augments Package with state used during the loading phase
|
||||||
|
@ -250,17 +254,20 @@ func newLoader(cfg *Config) *loader {
|
||||||
if ld.Context == nil {
|
if ld.Context == nil {
|
||||||
ld.Context = context.Background()
|
ld.Context = context.Background()
|
||||||
}
|
}
|
||||||
// Determine directory to be used for relative contains: paths.
|
|
||||||
if ld.Dir == "" {
|
if ld.Dir == "" {
|
||||||
if cwd, err := os.Getwd(); err == nil {
|
if cwd, err := os.Getwd(); err == nil {
|
||||||
ld.Dir = cwd
|
ld.Dir = cwd
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ld.Mode >= LoadSyntax {
|
|
||||||
|
if ld.Mode >= LoadTypes {
|
||||||
if ld.Fset == nil {
|
if ld.Fset == nil {
|
||||||
ld.Fset = token.NewFileSet()
|
ld.Fset = token.NewFileSet()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error and ParseFile are required even in LoadTypes mode
|
||||||
|
// because we load source if export data is missing.
|
||||||
|
|
||||||
if ld.Error == nil {
|
if ld.Error == nil {
|
||||||
ld.Error = func(e error) {
|
ld.Error = func(e error) {
|
||||||
fmt.Fprintln(os.Stderr, e)
|
fmt.Fprintln(os.Stderr, e)
|
||||||
|
|
Loading…
Reference in New Issue