Open
Description
What version of Go are you using (go version
)?
$ go version go version go1.21.4 linux/amd64
Does this issue reproduce with the latest release?
Yes
What did you do?
https://github.com/golang/go/blob/go1.21.4/src/slices/slices.go#L346-L385
func Compact[S ~[]E, E comparable](s S) S {
if len(s) < 2 {
return s
}
i := 1
for k := 1; k < len(s); k++ {
if s[k] != s[k-1] {
if i != k {
s[i] = s[k] // Found IsInBounds
}
i++
}
}
clear(s[i:]) // Found IsSliceInBounds
return s[:i]
}
func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S {
if len(s) < 2 {
return s
}
i := 1
for k := 1; k < len(s); k++ {
if !eq(s[k], s[k-1]) {
if i != k {
s[i] = s[k] // Found IsInBounds
}
i++
}
}
clear(s[i:]) // Found IsSliceInBounds
return s[:i]
}
And
func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S {
i := IndexFunc(s, del)
if i == -1 {
return s
}
// Don't start copying elements until we find one to delete.
for j := i + 1; j < len(s); j++ {
if v := s[j]; !del(v) { // Found IsInBounds
s[i] = v // Found IsInBounds
i++
}
}
clear(s[i:]) // Found IsSliceInBounds
return s[:i]
}
What did you expect to see?
No bound checks.
What did you see instead?
Many bound checks.
This problem might have been reported before, but I couldn't find it now.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Todo