Skip to content

slices: better panic message for out of range Insert #64152

@sedyh

Description

@sedyh

What version of Go are you using (go version)?

$ go version 
go version go1.21 darwin/arm64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/sedyh/Library/Caches/go-build"
GOENV="/Users/sedyh/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/sedyh/Desktop/share/go/path/go1.21.1/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/sedyh/Desktop/share/go/path/go1.21.1"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/sedyh/Desktop/share/go/root/go1.21.1"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/sedyh/Desktop/share/go/root/go1.21.1/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.21.1"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/x5/kntr8jy137vg9cjlm83chwmh0000gn/T/go-build3952104386=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

For some reason slices.Insert causes out of bounds panic if there no values at if !overlaps(v, s[i+m:]) {}.

As far as I can see in https://go.dev/cl/540155, this is intended, but it seems wrong to me, because:

  • I don't see any point in this. I think this is the only case why I wanted to use it.
  • This case is not processed separately, it is a standard out of bounds panic.
  • This is not stated in the function comments.

What did you expect to see?

s := []float64{}
s = slices.Grow(s, 5)
s = slices.Insert(s, 4, 1.0)

The function works the same regardless of how full the slice is.

What did you see instead?

panic: runtime error: slice bounds out of range [5:1]

This is not a specially caused panic, it looks like a missed case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions