Skip to content

slices: Insert function does not panic if i is out of range and there are no values to insert #63913

Closed
@gazerro

Description

@gazerro

The slices.Insert function should panic if i is out of range, but it does not panic if there are no values to insert:

s := []string{"a", "b", "c"}
s = slices.Insert(s, -1) // does not panic
s = slices.Insert(s, 5) // does not panic
fmt.Println(s)

Activity

changed the title [-]slices: Insert function does not panic if i is out of range and there are not values to insert[/-] [+]slices: Insert function does not panic if i is out of range and there are no values to insert[/+] on Nov 2, 2023
bcmills

bcmills commented on Nov 2, 2023

@bcmills
Contributor

The documentation does indeed claim that it will panic:
“Insert panics if i is out of range.”

(attn @ianlancetaylor @eliben)

added
NeedsFixThe path to resolution is known, but the work has not been done.
on Nov 2, 2023
eliben

eliben commented on Nov 2, 2023

@eliben
Member

Interesting edge case. I looked back at the original implementation of slices.Insert when it was just moved into the stdlib, and that one panics for the test cases specified here. However, following the optimizations in https://go-review.googlesource.com/c/go/+/494817 it no longer panics for these cases. @randall77 @cuonglm WDYT?

randall77

randall77 commented on Nov 3, 2023

@randall77
Contributor

Making it panic in these cases is fine.

gopherbot

gopherbot commented on Nov 6, 2023

@gopherbot
Contributor

Change https://go.dev/cl/540155 mentions this issue: slices: make Insert panic if index is out of range and there are no values

gopherbot

gopherbot commented on Nov 15, 2023

@gopherbot
Contributor

Change https://go.dev/cl/542455 mentions this issue: slices: imporve Insert panic message for index out of range

added a commit that references this issue on Nov 19, 2023
2551fff
locked and limited conversation to collaborators on Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @gazerro@eliben@bcmills@randall77@gopherbot

      Issue actions

        slices: Insert function does not panic if i is out of range and there are no values to insert · Issue #63913 · golang/go