1
0
mirror of https://github.com/golang/go synced 2024-11-26 17:56:55 -07:00

[dev.typeparams] cmd/compile/internal/types2: factor out sorting of methods

Cleanup and first step towards uniformly changing the sort criteria.

Change-Id: I0a7b6a10b5b646fc83f4897e4915ef4dae24aa66
Reviewed-on: https://go-review.googlesource.com/c/go/+/285993
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Robert Griesemer 2021-01-22 16:24:05 -08:00
parent e4ef30a667
commit 6923019a71
4 changed files with 27 additions and 17 deletions

View File

@ -6,10 +6,6 @@
package types2 package types2
import (
"sort"
)
// isNamed reports whether typ has a name. // isNamed reports whether typ has a name.
// isNamed may be called with types that are not fully set up. // isNamed may be called with types that are not fully set up.
func isNamed(typ Type) bool { func isNamed(typ Type) bool {
@ -329,8 +325,8 @@ func (check *Checker) identical0(x, y Type, cmpTags bool, p *ifacePair) bool {
p = p.prev p = p.prev
} }
if debug { if debug {
assert(sort.IsSorted(byUniqueMethodName(a))) assertSortedMethods(a)
assert(sort.IsSorted(byUniqueMethodName(b))) assertSortedMethods(b)
} }
for i, f := range a { for i, f := range a {
g := b[i] g := b[i]

View File

@ -8,7 +8,6 @@ package types2
import ( import (
"cmd/compile/internal/syntax" "cmd/compile/internal/syntax"
"fmt" "fmt"
"sort"
) )
// A Type represents a type of Go. // A Type represents a type of Go.
@ -481,8 +480,8 @@ func NewInterfaceType(methods []*Func, embeddeds []Type) *Interface {
} }
// sort for API stability // sort for API stability
sort.Sort(byUniqueMethodName(methods)) sortMethods(methods)
sort.Stable(byUniqueTypeName(embeddeds)) sortTypes(embeddeds)
typ.methods = methods typ.methods = methods
typ.embeddeds = embeddeds typ.embeddeds = embeddeds
@ -685,7 +684,7 @@ func (t *Interface) Complete() *Interface {
} }
if methods != nil { if methods != nil {
sort.Sort(byUniqueMethodName(methods)) sortMethods(methods)
t.allMethods = methods t.allMethods = methods
} }
t.allTypes = allTypes t.allTypes = allTypes

View File

@ -876,8 +876,8 @@ func (check *Checker) interfaceType(ityp *Interface, iface *syntax.InterfaceType
} }
// sort for API stability // sort for API stability
sort.Sort(byUniqueMethodName(ityp.methods)) sortMethods(ityp.methods)
sort.Stable(byUniqueTypeName(ityp.embeddeds)) sortTypes(ityp.embeddeds)
check.later(func() { check.completeInterface(iface.Pos(), ityp) }) check.later(func() { check.completeInterface(iface.Pos(), ityp) })
} }
@ -985,7 +985,7 @@ func (check *Checker) completeInterface(pos syntax.Pos, ityp *Interface) {
} }
if methods != nil { if methods != nil {
sort.Sort(byUniqueMethodName(methods)) sortMethods(methods)
ityp.allMethods = methods ityp.allMethods = methods
} }
ityp.allTypes = allTypes ityp.allTypes = allTypes
@ -1029,6 +1029,10 @@ func intersect(x, y Type) (r Type) {
return NewSum(rtypes) return NewSum(rtypes)
} }
func sortTypes(list []Type) {
sort.Stable(byUniqueTypeName(list))
}
// byUniqueTypeName named type lists can be sorted by their unique type names. // byUniqueTypeName named type lists can be sorted by their unique type names.
type byUniqueTypeName []Type type byUniqueTypeName []Type
@ -1043,6 +1047,19 @@ func sortName(t Type) string {
return "" return ""
} }
func sortMethods(list []*Func) {
sort.Sort(byUniqueMethodName(list))
}
func assertSortedMethods(list []*Func) {
if !debug {
panic("internal error: assertSortedMethods called outside debug mode")
}
if !sort.IsSorted(byUniqueMethodName(list)) {
panic("internal error: methods not sorted")
}
}
// byUniqueMethodName method lists can be sorted by their unique method names. // byUniqueMethodName method lists can be sorted by their unique method names.
type byUniqueMethodName []*Func type byUniqueMethodName []*Func

View File

@ -6,8 +6,6 @@
package types2 package types2
import "sort"
// The unifier maintains two separate sets of type parameters x and y // The unifier maintains two separate sets of type parameters x and y
// which are used to resolve type parameters in the x and y arguments // which are used to resolve type parameters in the x and y arguments
// provided to the unify call. For unidirectional unification, only // provided to the unify call. For unidirectional unification, only
@ -386,8 +384,8 @@ func (u *unifier) nify(x, y Type, p *ifacePair) bool {
p = p.prev p = p.prev
} }
if debug { if debug {
assert(sort.IsSorted(byUniqueMethodName(a))) assertSortedMethods(a)
assert(sort.IsSorted(byUniqueMethodName(b))) assertSortedMethods(b)
} }
for i, f := range a { for i, f := range a {
g := b[i] g := b[i]