diff options
author | Robert Griesemer <gri@golang.org> | 2021-01-26 12:08:18 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2021-01-27 19:42:42 +0000 |
commit | 217a461f56cecee1756bef29f9ad2dcd389a255b (patch) | |
tree | 8917d7d61a7645069c0d6296b9e2570c3a8241ba /src/cmd/compile/internal/types2/check.go | |
parent | 08a598f8c1c123fda3b7ad30659fa05a8be1ccde (diff) | |
download | go-217a461f56cecee1756bef29f9ad2dcd389a255b.tar.gz go-217a461f56cecee1756bef29f9ad2dcd389a255b.zip |
[dev.typeparams] cmd/compile/internal/types2: report unused packages in source order
1) Rather than map-iterate through all file scopes and collect unused
packages, collect all imports in the Checker.imports list so that
errors are reported in source order.
2) From cmd/compile, borrow the idea of a "dotImportRefs" map to map
dot-imported objects to the package they were dot-imported through
(we call the map "dotImportMap").
3) From cmd/compile, borrow the "pkgnotused" function
(called Checker.errorUnusedPkg in this code) and clean up
unused package error reporting.
4) Adjust unused package error message to match compiler message exactly.
5) Enable one more excluded test case in test/run.go.
Change-Id: I4e4e55512a6043a7fd54f576c7441e3dd4077d6f
Reviewed-on: https://go-review.googlesource.com/c/go/+/287072
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'src/cmd/compile/internal/types2/check.go')
-rw-r--r-- | src/cmd/compile/internal/types2/check.go | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/src/cmd/compile/internal/types2/check.go b/src/cmd/compile/internal/types2/check.go index 6ba8506916..e2c6c4f606 100644 --- a/src/cmd/compile/internal/types2/check.go +++ b/src/cmd/compile/internal/types2/check.go @@ -74,6 +74,12 @@ type importKey struct { path, dir string } +// A dotImportKey describes a dot-imported object in the given scope. +type dotImportKey struct { + scope *Scope + obj Object +} + // A Checker maintains the state of the type checker. // It must be created with NewChecker. type Checker struct { @@ -92,8 +98,9 @@ type Checker struct { // information collected during type-checking of a set of package files // (initialized by Files, valid only for the duration of check.Files; // maps and lists are allocated on demand) - files []*syntax.File // package files - unusedDotImports map[*Scope]map[*Package]syntax.Pos // positions of unused dot-imported packages for each file scope + files []*syntax.File // list of package files + imports []*PkgName // list of imported packages + dotImportMap map[dotImportKey]*PkgName // maps dot-imported objects to the package they were dot-imported through firstErr error // first error encountered methods map[*TypeName][]*Func // maps package scope type names to associated non-blank (non-interface) methods @@ -110,22 +117,6 @@ type Checker struct { indent int // indentation for tracing } -// addUnusedImport adds the position of a dot-imported package -// pkg to the map of dot imports for the given file scope. -func (check *Checker) addUnusedDotImport(scope *Scope, pkg *Package, pos syntax.Pos) { - mm := check.unusedDotImports - if mm == nil { - mm = make(map[*Scope]map[*Package]syntax.Pos) - check.unusedDotImports = mm - } - m := mm[scope] - if m == nil { - m = make(map[*Package]syntax.Pos) - mm[scope] = m - } - m[pkg] = pos -} - // addDeclDep adds the dependency edge (check.decl -> to) if check.decl exists func (check *Checker) addDeclDep(to Object) { from := check.decl @@ -209,7 +200,8 @@ func NewChecker(conf *Config, pkg *Package, info *Info) *Checker { func (check *Checker) initFiles(files []*syntax.File) { // start with a clean slate (check.Files may be called multiple times) check.files = nil - check.unusedDotImports = nil + check.imports = nil + check.dotImportMap = nil check.firstErr = nil check.methods = nil @@ -291,6 +283,9 @@ func (check *Checker) checkFiles(files []*syntax.File) (err error) { print("== unusedImports ==") check.unusedImports() } + // no longer needed - release memory + check.imports = nil + check.dotImportMap = nil print("== recordUntyped ==") check.recordUntyped() @@ -301,6 +296,9 @@ func (check *Checker) checkFiles(files []*syntax.File) (err error) { } check.pkg.complete = true + + // TODO(gri) There's more memory we should release at this point. + return } |