]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile/internal/types2: factor out sorting of methods
authorRobert Griesemer <gri@golang.org>
Sat, 23 Jan 2021 00:24:05 +0000 (16:24 -0800)
committerRobert Griesemer <gri@golang.org>
Sat, 23 Jan 2021 01:08:47 +0000 (01:08 +0000)
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>
src/cmd/compile/internal/types2/predicates.go
src/cmd/compile/internal/types2/type.go
src/cmd/compile/internal/types2/typexpr.go
src/cmd/compile/internal/types2/unify.go

index 9cce189140719cd2e86eb61e419408987560d5a5..94a9b6476135fffeb816e7379a756269a6cadabb 100644 (file)
@@ -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 {
@@ -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]
index 1bfde41159644325ea36f7e2001d787dc086f040..22901b2ba976f8d30ef5c018c9ec874991d06597 100644 (file)
@@ -8,7 +8,6 @@ package types2
 import (
        "cmd/compile/internal/syntax"
        "fmt"
-       "sort"
 )
 
 // A Type represents a type of Go.
@@ -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
@@ -685,7 +684,7 @@ func (t *Interface) Complete() *Interface {
        }
 
        if methods != nil {
-               sort.Sort(byUniqueMethodName(methods))
+               sortMethods(methods)
                t.allMethods = methods
        }
        t.allTypes = allTypes
index f0461d589539c745939f8a96270070aa090ed026..d0bf229be9c78777f733272806891688c12a489f 100644 (file)
@@ -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) })
 }
@@ -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
@@ -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
 
@@ -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
 
index 60ccf625b909fd897a2d7ff4ebb8586b2d0c8490..ab19c5a38bb803645ce62966e60ac354b86593ef 100644 (file)
@@ -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
@@ -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]