aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/compile')
-rw-r--r--src/cmd/compile/internal/reflectdata/reflect.go2
-rw-r--r--src/cmd/compile/internal/typecheck/subr.go2
-rw-r--r--src/cmd/compile/internal/types/size.go19
-rw-r--r--src/cmd/compile/internal/types/sort.go16
-rw-r--r--src/cmd/compile/internal/types/sym.go25
-rw-r--r--src/cmd/compile/internal/types/sym_test.go6
-rw-r--r--src/cmd/compile/internal/types/type.go5
7 files changed, 34 insertions, 41 deletions
diff --git a/src/cmd/compile/internal/reflectdata/reflect.go b/src/cmd/compile/internal/reflectdata/reflect.go
index de7e4755d3..c26ac3d74c 100644
--- a/src/cmd/compile/internal/reflectdata/reflect.go
+++ b/src/cmd/compile/internal/reflectdata/reflect.go
@@ -145,7 +145,7 @@ func imethods(t *types.Type) []*typeSig {
}
if n := len(methods); n > 0 {
last := methods[n-1]
- if !last.name.Less(f.Sym) {
+ if types.CompareSyms(last.name, f.Sym) >= 0 {
base.Fatalf("sigcmp vs sortinter %v %v", last.name, f.Sym)
}
}
diff --git a/src/cmd/compile/internal/typecheck/subr.go b/src/cmd/compile/internal/typecheck/subr.go
index af7ab38638..3b22d260bf 100644
--- a/src/cmd/compile/internal/typecheck/subr.go
+++ b/src/cmd/compile/internal/typecheck/subr.go
@@ -144,7 +144,7 @@ func CalcMethods(t *types.Type) {
}
ms = append(ms, t.Methods()...)
- slices.SortFunc(ms, types.MethodsByNameCmp)
+ slices.SortFunc(ms, types.CompareFields)
t.SetAllMethods(ms)
}
diff --git a/src/cmd/compile/internal/types/size.go b/src/cmd/compile/internal/types/size.go
index 308245d9b7..48729884df 100644
--- a/src/cmd/compile/internal/types/size.go
+++ b/src/cmd/compile/internal/types/size.go
@@ -7,7 +7,6 @@ package types
import (
"math"
"slices"
- "sort"
"cmd/compile/internal/base"
"cmd/internal/src"
@@ -94,21 +93,21 @@ func expandiface(t *Type) {
{
methods := t.Methods()
- sort.SliceStable(methods, func(i, j int) bool {
- mi, mj := methods[i], methods[j]
-
+ slices.SortStableFunc(methods, func(a, b *Field) int {
// Sort embedded types by type name (if any).
- if mi.Sym == nil && mj.Sym == nil {
- return mi.Type.Sym().Less(mj.Type.Sym())
+ if a.Sym == nil && b.Sym == nil {
+ return CompareSyms(a.Type.Sym(), b.Type.Sym())
}
// Sort methods before embedded types.
- if mi.Sym == nil || mj.Sym == nil {
- return mi.Sym != nil
+ if a.Sym == nil {
+ return -1
+ } else if b.Sym == nil {
+ return +1
}
// Sort methods by symbol name.
- return mi.Sym.Less(mj.Sym)
+ return CompareSyms(a.Sym, b.Sym)
})
}
@@ -147,7 +146,7 @@ func expandiface(t *Type) {
m.Pos = src.NoXPos
}
- slices.SortFunc(methods, MethodsByNameCmp)
+ slices.SortFunc(methods, CompareFields)
if int64(len(methods)) >= MaxWidth/int64(PtrSize) {
base.ErrorfAt(typePos(t), 0, "interface too large")
diff --git a/src/cmd/compile/internal/types/sort.go b/src/cmd/compile/internal/types/sort.go
deleted file mode 100644
index 83b1237634..0000000000
--- a/src/cmd/compile/internal/types/sort.go
+++ /dev/null
@@ -1,16 +0,0 @@
-// Copyright 2009 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package types
-
-// MethodsByNameCmp sorts methods by name.
-func MethodsByNameCmp(x, y *Field) int {
- if x.Sym.Less(y.Sym) {
- return -1
- }
- if y.Sym.Less(x.Sym) {
- return +1
- }
- return 0
-}
diff --git a/src/cmd/compile/internal/types/sym.go b/src/cmd/compile/internal/types/sym.go
index 67fa6bb1d0..97175d745c 100644
--- a/src/cmd/compile/internal/types/sym.go
+++ b/src/cmd/compile/internal/types/sym.go
@@ -7,6 +7,7 @@ package types
import (
"cmd/compile/internal/base"
"cmd/internal/obj"
+ "strings"
"unicode"
"unicode/utf8"
)
@@ -92,39 +93,43 @@ func (sym *Sym) LinksymABI(abi obj.ABI) *obj.LSym {
return base.PkgLinksym(sym.Pkg.Prefix, sym.Name, abi)
}
-// Less reports whether symbol a is ordered before symbol b.
+// CompareSyms return the ordering of a and b, as for [cmp.Compare].
//
// Symbols are ordered exported before non-exported, then by name, and
// finally (for non-exported symbols) by package path.
-func (a *Sym) Less(b *Sym) bool {
+func CompareSyms(a, b *Sym) int {
if a == b {
- return false
+ return 0
}
// Nil before non-nil.
if a == nil {
- return true
+ return -1
}
if b == nil {
- return false
+ return +1
}
// Exported symbols before non-exported.
ea := IsExported(a.Name)
eb := IsExported(b.Name)
if ea != eb {
- return ea
+ if ea {
+ return -1
+ } else {
+ return +1
+ }
}
// Order by name and then (for non-exported names) by package
// height and path.
- if a.Name != b.Name {
- return a.Name < b.Name
+ if r := strings.Compare(a.Name, b.Name); r != 0 {
+ return r
}
if !ea {
- return a.Pkg.Path < b.Pkg.Path
+ return strings.Compare(a.Pkg.Path, b.Pkg.Path)
}
- return false
+ return 0
}
// IsExported reports whether name is an exported Go symbol (that is,
diff --git a/src/cmd/compile/internal/types/sym_test.go b/src/cmd/compile/internal/types/sym_test.go
index 94efd42aa4..cdb17c36f5 100644
--- a/src/cmd/compile/internal/types/sym_test.go
+++ b/src/cmd/compile/internal/types/sym_test.go
@@ -7,11 +7,11 @@ package types_test
import (
"cmd/compile/internal/types"
"reflect"
- "sort"
+ "slices"
"testing"
)
-func TestSymLess(t *testing.T) {
+func TestSymCompare(t *testing.T) {
var (
local = types.NewPkg("", "")
abc = types.NewPkg("abc", "")
@@ -50,7 +50,7 @@ func TestSymLess(t *testing.T) {
if reflect.DeepEqual(data, want) {
t.Fatal("data must be shuffled")
}
- sort.Slice(data, func(i, j int) bool { return data[i].Less(data[j]) })
+ slices.SortFunc(data, types.CompareSyms)
if !reflect.DeepEqual(data, want) {
t.Logf("want: %#v", want)
t.Logf("data: %#v", data)
diff --git a/src/cmd/compile/internal/types/type.go b/src/cmd/compile/internal/types/type.go
index 9d3dde8c13..79c890d46c 100644
--- a/src/cmd/compile/internal/types/type.go
+++ b/src/cmd/compile/internal/types/type.go
@@ -458,6 +458,11 @@ func (f *Field) IsMethod() bool {
return f.Type.kind == TFUNC && f.Type.Recv() != nil
}
+// CompareFields compares two Field values by name.
+func CompareFields(a, b *Field) int {
+ return CompareSyms(a.Sym, b.Sym)
+}
+
// fields is a pointer to a slice of *Field.
// This saves space in Types that do not have fields or methods
// compared to a simple slice of *Field.