"internal/testenv"
"os"
"path/filepath"
- "sort"
+ "slices"
"strings"
"sync"
"testing"
t.Fatalf("opening golden.txt for package %q: %v", fi.Name(), err)
}
wanted := strings.Split(string(bs), "\n")
- sort.Strings(wanted)
+ slices.Sort(wanted)
for _, feature := range wanted {
if feature == "" {
continue
"path/filepath"
"regexp"
"runtime"
- "sort"
+ "slices"
"strconv"
"strings"
"sync"
featureSet := set(features)
exceptionSet := set(exception)
- sort.Strings(features)
- sort.Strings(required)
+ slices.Sort(features)
+ slices.Sort(required)
take := func(sl *[]string) string {
s := (*sl)[0]
for f := range w.features {
fs = append(fs, f)
}
- sort.Strings(fs)
+ slices.Sort(fs)
return
}
// an indirect imported package. See https://github.com/golang/go/issues/21181
// for more detail.
tags = append(tags, context.GOOS, context.GOARCH)
- sort.Strings(tags)
+ slices.Sort(tags)
for _, tag := range tags {
if ctags[tag] {
}
}
- sort.Strings(stdPackages)
+ slices.Sort(stdPackages)
imports = listImports{
stdPackages: stdPackages,
importMap: importMap,
for i := range list {
list[i] = typ.Method(i).Name()
}
- sort.Strings(list)
+ slices.Sort(list)
return list
}
list = append(list, buf.String())
}
}
- sort.Strings(list)
+ slices.Sort(list)
return list
}
return
}
- sort.Strings(methodNames)
+ slices.Sort(methodNames)
w.emitf("type %s interface { %s }", name, strings.Join(methodNames, ", "))
}
}
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)
}
}
}
ms = append(ms, t.Methods()...)
- slices.SortFunc(ms, types.MethodsByNameCmp)
+ slices.SortFunc(ms, types.CompareFields)
t.SetAllMethods(ms)
}
import (
"math"
"slices"
- "sort"
"cmd/compile/internal/base"
"cmd/internal/src"
{
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)
})
}
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")
+++ /dev/null
-// 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
-}
import (
"cmd/compile/internal/base"
"cmd/internal/obj"
+ "strings"
"unicode"
"unicode/utf8"
)
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,
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", "")
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)
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.