]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile/internal/types2: use same sort criteria for methods...
authorRobert Griesemer <gri@golang.org>
Sat, 23 Jan 2021 00:49:21 +0000 (16:49 -0800)
committerRobert Griesemer <gri@golang.org>
Sat, 23 Jan 2021 01:40:32 +0000 (01:40 +0000)
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 <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/types2/object.go
src/cmd/compile/internal/types2/typexpr.go

index 42fae762d31253990b6acd705da4656f9b23aeb1..b42662222f6780f0f23e253ae58bb242ae2c1971 100644 (file)
@@ -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.
index d0bf229be9c78777f733272806891688c12a489f..9ab84b594bc4aad7521406f220b1cae8f50a8a5a 100644 (file)
@@ -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 {