mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile: make "imported and not used" errors deterministic
If there were more unused imports than the maximum default number of errors to report, the set of reported imports was non-deterministic. Fix by accumulating and sorting them prior to output. Fixes #20298 Change-Id: Ib3d5a15fd7dc40009523fcdc1b93ddc62a1b05f2 Reviewed-on: https://go-review.googlesource.com/42954 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
fb0ccc5d0a
commit
9fda4df9a0
3 changed files with 66 additions and 12 deletions
|
|
@ -1043,35 +1043,46 @@ func mkpackage(pkgname string) {
|
|||
}
|
||||
|
||||
func clearImports() {
|
||||
type importedPkg struct {
|
||||
pos src.XPos
|
||||
path string
|
||||
name string
|
||||
}
|
||||
var unused []importedPkg
|
||||
|
||||
for _, s := range localpkg.Syms {
|
||||
if asNode(s.Def) == nil {
|
||||
n := asNode(s.Def)
|
||||
if n == nil {
|
||||
continue
|
||||
}
|
||||
if asNode(s.Def).Op == OPACK {
|
||||
// throw away top-level package name leftover
|
||||
if n.Op == OPACK {
|
||||
// throw away top-level package name left over
|
||||
// from previous file.
|
||||
// leave s->block set to cause redeclaration
|
||||
// errors if a conflicting top-level name is
|
||||
// introduced by a different file.
|
||||
if !asNode(s.Def).Name.Used() && nsyntaxerrors == 0 {
|
||||
pkgnotused(asNode(s.Def).Pos, asNode(s.Def).Name.Pkg.Path, s.Name)
|
||||
if !n.Name.Used() && nsyntaxerrors == 0 {
|
||||
unused = append(unused, importedPkg{n.Pos, n.Name.Pkg.Path, s.Name})
|
||||
}
|
||||
s.Def = nil
|
||||
continue
|
||||
}
|
||||
|
||||
if IsAlias(s) {
|
||||
// throw away top-level name left over
|
||||
// from previous import . "x"
|
||||
if asNode(s.Def).Name != nil && asNode(s.Def).Name.Pack != nil && !asNode(s.Def).Name.Pack.Name.Used() && nsyntaxerrors == 0 {
|
||||
pkgnotused(asNode(s.Def).Name.Pack.Pos, asNode(s.Def).Name.Pack.Name.Pkg.Path, "")
|
||||
asNode(s.Def).Name.Pack.Name.SetUsed(true)
|
||||
if n.Name != nil && n.Name.Pack != nil && !n.Name.Pack.Name.Used() && nsyntaxerrors == 0 {
|
||||
unused = append(unused, importedPkg{n.Name.Pack.Pos, n.Name.Pack.Name.Pkg.Path, ""})
|
||||
n.Name.Pack.Name.SetUsed(true)
|
||||
}
|
||||
|
||||
s.Def = nil
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
obj.SortSlice(unused, func(i, j int) bool { return unused[i].pos.Before(unused[j].pos) })
|
||||
for _, pkg := range unused {
|
||||
pkgnotused(pkg.pos, pkg.path, pkg.name)
|
||||
}
|
||||
}
|
||||
|
||||
func IsAlias(sym *types.Sym) bool {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue