Skip to content

Commit

Permalink
[dev.typeparams] cmd/compile/internal/types2: factor out sorting of m…
Browse files Browse the repository at this point in the history
…ethods

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>
  • Loading branch information
griesemer committed Jan 23, 2021
1 parent e4ef30a commit 6923019
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
8 changes: 2 additions & 6 deletions src/cmd/compile/internal/types2/predicates.go
Expand Up @@ -6,10 +6,6 @@

package types2

import (
"sort"
)

// isNamed reports whether typ has a name.
// isNamed may be called with types that are not fully set up.
func isNamed(typ Type) bool {
Expand Down Expand Up @@ -329,8 +325,8 @@ func (check *Checker) identical0(x, y Type, cmpTags bool, p *ifacePair) bool {
p = p.prev
}
if debug {
assert(sort.IsSorted(byUniqueMethodName(a)))
assert(sort.IsSorted(byUniqueMethodName(b)))
assertSortedMethods(a)
assertSortedMethods(b)
}
for i, f := range a {
g := b[i]
Expand Down
7 changes: 3 additions & 4 deletions src/cmd/compile/internal/types2/type.go
Expand Up @@ -8,7 +8,6 @@ package types2
import (
"cmd/compile/internal/syntax"
"fmt"
"sort"
)

// A Type represents a type of Go.
Expand Down Expand Up @@ -481,8 +480,8 @@ func NewInterfaceType(methods []*Func, embeddeds []Type) *Interface {
}

// sort for API stability
sort.Sort(byUniqueMethodName(methods))
sort.Stable(byUniqueTypeName(embeddeds))
sortMethods(methods)
sortTypes(embeddeds)

typ.methods = methods
typ.embeddeds = embeddeds
Expand Down Expand Up @@ -685,7 +684,7 @@ func (t *Interface) Complete() *Interface {
}

if methods != nil {
sort.Sort(byUniqueMethodName(methods))
sortMethods(methods)
t.allMethods = methods
}
t.allTypes = allTypes
Expand Down
23 changes: 20 additions & 3 deletions src/cmd/compile/internal/types2/typexpr.go
Expand Up @@ -876,8 +876,8 @@ func (check *Checker) interfaceType(ityp *Interface, iface *syntax.InterfaceType
}

// sort for API stability
sort.Sort(byUniqueMethodName(ityp.methods))
sort.Stable(byUniqueTypeName(ityp.embeddeds))
sortMethods(ityp.methods)
sortTypes(ityp.embeddeds)

check.later(func() { check.completeInterface(iface.Pos(), ityp) })
}
Expand Down Expand Up @@ -985,7 +985,7 @@ func (check *Checker) completeInterface(pos syntax.Pos, ityp *Interface) {
}

if methods != nil {
sort.Sort(byUniqueMethodName(methods))
sortMethods(methods)
ityp.allMethods = methods
}
ityp.allTypes = allTypes
Expand Down Expand Up @@ -1029,6 +1029,10 @@ func intersect(x, y Type) (r Type) {
return NewSum(rtypes)
}

func sortTypes(list []Type) {
sort.Stable(byUniqueTypeName(list))
}

// byUniqueTypeName named type lists can be sorted by their unique type names.
type byUniqueTypeName []Type

Expand All @@ -1043,6 +1047,19 @@ func sortName(t Type) string {
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.
type byUniqueMethodName []*Func

Expand Down
6 changes: 2 additions & 4 deletions src/cmd/compile/internal/types2/unify.go
Expand Up @@ -6,8 +6,6 @@

package types2

import "sort"

// 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
// provided to the unify call. For unidirectional unification, only
Expand Down Expand Up @@ -386,8 +384,8 @@ func (u *unifier) nify(x, y Type, p *ifacePair) bool {
p = p.prev
}
if debug {
assert(sort.IsSorted(byUniqueMethodName(a)))
assert(sort.IsSorted(byUniqueMethodName(b)))
assertSortedMethods(a)
assertSortedMethods(b)
}
for i, f := range a {
g := b[i]
Expand Down

0 comments on commit 6923019

Please sign in to comment.