aboutsummaryrefslogtreecommitdiff
path: root/src/slices
diff options
context:
space:
mode:
authorgo101 <tapir.liu@gmail.com>2023-11-16 15:34:32 +0000
committerGopher Robot <gobot@golang.org>2023-11-19 17:06:47 +0000
commit2551fffd2c06cf0655ebbbd11d9b1e70a5b2e9cb (patch)
treeab0ccb0157e02c5ca1dab3b47dec992033e2d5d7 /src/slices
parent1c15291fa0efaeb14a76d0b0bcd8390665c9b97d (diff)
downloadgo-2551fffd2c06cf0655ebbbd11d9b1e70a5b2e9cb.tar.gz
go-2551fffd2c06cf0655ebbbd11d9b1e70a5b2e9cb.zip
slices: improve Insert panic message for index out of range
The panic message of the current implementation for index out of range is not ideal. This PR tries to improve it. Fixes #63913 and #64152 Change-Id: Ibcf6c9c0f555c8b8bf46b7d6f20f0ccc5698acd4 GitHub-Last-Rev: 1bbec230f4b0ec547d21c8d38dd46007bd282855 GitHub-Pull-Request: golang/go#64163 Reviewed-on: https://go-review.googlesource.com/c/go/+/542455 Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Keith Randall <khr@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@golang.org> Auto-Submit: Keith Randall <khr@golang.org>
Diffstat (limited to 'src/slices')
-rw-r--r--src/slices/slices.go7
1 files changed, 3 insertions, 4 deletions
diff --git a/src/slices/slices.go b/src/slices/slices.go
index 38b0fc14ad..b0f048a656 100644
--- a/src/slices/slices.go
+++ b/src/slices/slices.go
@@ -130,14 +130,13 @@ func ContainsFunc[S ~[]E, E any](s S, f func(E) bool) bool {
// Insert panics if i is out of range.
// This function is O(len(s) + len(v)).
func Insert[S ~[]E, E any](s S, i int, v ...E) S {
- n := len(s)
+ _ = s[i:] // bounds check
+
m := len(v)
if m == 0 {
- // Panic if i is not in the range [0:n] inclusive.
- // See issue 63913.
- _ = s[:n:n][i:]
return s
}
+ n := len(s)
if i == n {
return append(s, v...)
}