From: Robert Griesemer Date: Sat, 23 Jan 2021 00:49:21 +0000 (-0800) Subject: [dev.typeparams] cmd/compile/internal/types2: use same sort criteria for methods... X-Git-Tag: go1.17beta1~1473^2~77 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=5347241b5e64eb9a7b0ef97b12d899f32a05c2b8;p=gostls13.git [dev.typeparams] cmd/compile/internal/types2: use same sort criteria for methods as compiler Note: This invalidates the implementation of MethodSet further (it also has not been updated to accomodate for type parameters). But types2 doesn't make use of it. We should remove it. Change-Id: Ia2601bdd59b3f3ee0b72bc2512153c42bf5053b5 Reviewed-on: https://go-review.googlesource.com/c/go/+/285994 Trust: Robert Griesemer Reviewed-by: Matthew Dempsky --- diff --git a/src/cmd/compile/internal/types2/object.go b/src/cmd/compile/internal/types2/object.go index 42fae762d3..b42662222f 100644 --- a/src/cmd/compile/internal/types2/object.go +++ b/src/cmd/compile/internal/types2/object.go @@ -330,6 +330,36 @@ func (obj *Func) FullName() string { // Scope returns the scope of the function's body block. func (obj *Func) Scope() *Scope { return obj.typ.(*Signature).scope } +// Less reports whether function a is ordered before function b. +// +// Functions are ordered exported before non-exported, then by name, +// and finally (for non-exported functions) by package path. +// +// TODO(gri) The compiler also sorts by package height before package +// path for non-exported names. +func (a *Func) less(b *Func) bool { + if a == b { + return false + } + + // Exported functions before non-exported. + ea := isExported(a.name) + eb := isExported(b.name) + if ea != eb { + return ea + } + + // Order by name and then (for non-exported names) by package. + if a.name != b.name { + return a.name < b.name + } + if !ea { + return a.pkg.path < b.pkg.path + } + + return false +} + func (*Func) isDependency() {} // a function may be a dependency of an initialization expression // A Label represents a declared label. diff --git a/src/cmd/compile/internal/types2/typexpr.go b/src/cmd/compile/internal/types2/typexpr.go index d0bf229be9..9ab84b594b 100644 --- a/src/cmd/compile/internal/types2/typexpr.go +++ b/src/cmd/compile/internal/types2/typexpr.go @@ -1064,7 +1064,7 @@ func assertSortedMethods(list []*Func) { type byUniqueMethodName []*Func func (a byUniqueMethodName) Len() int { return len(a) } -func (a byUniqueMethodName) Less(i, j int) bool { return a[i].Id() < a[j].Id() } +func (a byUniqueMethodName) Less(i, j int) bool { return a[i].less(a[j]) } func (a byUniqueMethodName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (check *Checker) tag(t *syntax.BasicLit) string {