-
Notifications
You must be signed in to change notification settings - Fork 17.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
slices: better panic message for out of range Insert #64152
Comments
What would end up in |
I hoped that slices.Insert would fill them with default values for me (.0) if index is bigger than cap or replace the existing values if index is lower than cap. |
It is stated in the doc comment: "Insert panics if i is out of range."
Because it is a standard out of bounds panic.
|
I would really like some of the functions in this package to be able to do make/append and fill the passed slice up to requested index as needed. Is there any workaround or slice trick that combines insert and growing the length? |
Slice tricks' a = append(a[:i], append(make([]T, j), a[i:]...)...) |
The one-line trick is quite inefficient for some cases. For OP's specified case:
just do:
|
Thanks for suggestion, but I also need to be able to insert elements at any index location other than extending the slice, so the previous one is more suitable.
I feel like it would be better to state "or slice is empty" separately, even if the first option implies this.
+1 |
Change https://go.dev/cl/542455 mentions this issue: |
I think it could help if the term "out of range" is not used in the documentation:
because it might not be clear that "out of range" refers to the slicing To remove this ambiguity, I would write:
|
Change https://go.dev/cl/542535 mentions this issue: |
@gazerro Also panics when i < 0. |
@callthingsoff You're right. We can use a similar sentence used for
|
|
For #64152 Change-Id: I32531aa8d147f4f10f6498f5ea1474555e93b6de GitHub-Last-Rev: 48bb3bb GitHub-Pull-Request: #64180 Reviewed-on: https://go-review.googlesource.com/c/go/+/542535 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Jes Cok <xigua67damn@gmail.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: David Chase <drchase@google.com>
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: 1bbec23 GitHub-Pull-Request: #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>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
For some reason
slices.Insert
causes out of bounds panic if there no values atif !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:
What did you expect to see?
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.
The text was updated successfully, but these errors were encountered: