cmd/compile: refactor symbol sorting logic

This used to be duplicated in methcmp and siglt, because Sig used its
own representation for Syms. Instead, just use Syms, and add a
(*Sym).Less method that both methcmp and siglt can use.

Also, prune some impossible cases purportedly related to blank
methods: the Go spec disallows blank methods in interface method sets,
and addmethod drops blank methods without actually recording them in
the type's method set.

Passes toolstash-check.

Updates #24693.

Change-Id: I24e981659b68504d71518160486989a82505f513
Reviewed-on: https://go-review.googlesource.com/105936
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Matthew Dempsky 2018-04-09 13:57:56 -07:00
parent 71bac7efe4
commit c3473c4f10
6 changed files with 117 additions and 149 deletions

View file

@ -369,44 +369,12 @@ func (n *Node) copy() *Node {
return &n2
}
// methcmp sorts methods by name with exported methods first,
// and then non-exported methods by their package path.
// methcmp sorts methods by symbol.
type methcmp []*types.Field
func (x methcmp) Len() int { return len(x) }
func (x methcmp) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x methcmp) Less(i, j int) bool {
a := x[i]
b := x[j]
if a.Sym == b.Sym {
return false
}
// Blank methods to the end.
if a.Sym == nil {
return false
}
if b.Sym == nil {
return true
}
// Exported methods to the front.
ea := types.IsExported(a.Sym.Name)
eb := types.IsExported(b.Sym.Name)
if ea != eb {
return ea
}
// Sort by name and then package.
if a.Sym.Name != b.Sym.Name {
return a.Sym.Name < b.Sym.Name
}
if !ea && a.Sym.Pkg.Path != b.Sym.Pkg.Path {
return a.Sym.Pkg.Path < b.Sym.Pkg.Path
}
return false
}
func (x methcmp) Len() int { return len(x) }
func (x methcmp) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x methcmp) Less(i, j int) bool { return x[i].Sym.Less(x[j].Sym) }
func nodintconst(v int64) *Node {
u := new(Mpint)