Commit e24cdbccd0 for golang
commit e24cdbccd0f15c1d670990d26ed11fd550f3e9a8
Author: Guido Iaquinti <guido.iaquinti@gmail.com>
Date: Tue Dec 1 17:41:16 2026 +0000
sort: make SliceIsSorted panic if x is not a slice
SliceIsSorted is documented to panic if x is not a slice, like Slice
and SliceStable. Unlike them, it only takes the length of x, so arrays,
strings, maps, and channels return a result instead of panicking.
Check that x is a slice, as Swapper does for Slice and SliceStable.
Change-Id: Id531f982fe13dfb49167995d68ce13257c6bfc89
GitHub-Last-Rev: 2fc09cd952f161d4584bd6f4857b75bc5bb36287
GitHub-Pull-Request: golang/go#83441
Reviewed-on: https://go-review.googlesource.com/c/go/+/867922
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/src/sort/slice.go b/src/sort/slice.go
index bc9dd84ed2..25a9e9e239 100644
--- a/src/sort/slice.go
+++ b/src/sort/slice.go
@@ -51,6 +51,9 @@ func SliceStable(x any, less func(i, j int) bool) {
// ergonomic and runs faster.
func SliceIsSorted(x any, less func(i, j int) bool) bool {
rv := reflectlite.ValueOf(x)
+ if rv.Kind() != reflectlite.Slice {
+ panic(&reflectlite.ValueError{Method: "sort.SliceIsSorted", Kind: rv.Kind()})
+ }
n := rv.Len()
for i := n - 1; i > 0; i-- {
if less(i, i-1) {
diff --git a/src/sort/sort_test.go b/src/sort/sort_test.go
index 12903ab6ba..762c49ed47 100644
--- a/src/sort/sort_test.go
+++ b/src/sort/sort_test.go
@@ -7,6 +7,7 @@ package sort_test
import (
"cmp"
"fmt"
+ "internal/reflectlite"
"internal/testenv"
"math"
"math/rand/v2"
@@ -103,6 +104,50 @@ func TestSlice(t *testing.T) {
}
}
+func TestSliceIsSortedNonSlice(t *testing.T) {
+ // SliceIsSorted must panic unless the test name ends in "slice".
+ tests := []struct {
+ name string
+ value any
+ }{
+ {"nil interface", nil},
+ {"empty array", [2]string{}},
+ {"nil slice", []int64(nil)},
+ {"empty slice", []int{}},
+ {"non-empty slice", []int{1, 2}},
+ {"map", map[int]int{1: 10, 2: 20}},
+ {"string", "ab"},
+ {"chan", make(chan int)},
+ {"scalar", 2000},
+ {"slice pointer", (*[]int)(nil)},
+ {"array pointer", (*[3]int)(nil)},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ defer func() {
+ r := recover()
+ if strings.HasSuffix(tt.name, "slice") {
+ if r != nil {
+ t.Fatalf("unexpected panic: %v", r)
+ }
+ return
+ }
+ if r == nil {
+ t.Fatal("did not panic")
+ }
+ err, ok := r.(*reflectlite.ValueError)
+ if !ok {
+ t.Fatalf("panic value has type %T, want *reflectlite.ValueError", r)
+ }
+ if got, want := err.Method, "sort.SliceIsSorted"; got != want {
+ t.Fatalf("ValueError.Method = %q, want %q", got, want)
+ }
+ }()
+ SliceIsSorted(tt.value, func(i, j int) bool { return true })
+ })
+ }
+}
+
func TestSortLarge_Random(t *testing.T) {
n := 1000000
if testing.Short() {