Skip to content

reflect: document missing panic behavior for non-assignable values in reflect.Grow #70760

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

Closed
gazerro opened this issue Dec 10, 2024 · 3 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation Issues describing a change to documentation. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@gazerro
Copy link
Contributor

gazerro commented Dec 10, 2024

Currently, the documentation for reflect.Grow does not mention that the method panics if the value passed is not assignable:

It panics if v's Kind is not a Slice or if n is negative or too large to allocate the memory.

However, if v is not assignable (either because it was obtained using an unexported field or it is not addressable), Grow will also panic. For example:

package main

import (
	"reflect"
)

func main() {
	reflect.ValueOf([]int{1, 2, 3}).Grow(5)
}
@gopherbot gopherbot added compiler/runtime Issues related to the Go compiler and/or runtime. Documentation Issues describing a change to documentation. labels Dec 10, 2024
@randall77
Copy link
Contributor

Same is true for the Set* methods, or Copy when the destination is an array.
We mention in some cases that they panic if CanSet is false, but fairly inconsistently.

@gazerro
Copy link
Contributor Author

gazerro commented Dec 10, 2024

It’s not immediately obvious that Grow can panic for certain slices, such as in the following example, which correctly panics:

s := []int{1, 2, 3}
reflect.ValueOf(s).Grow(5)

I believe this behavior should be documented, along with the other cases mentioned by @randall77.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/634799 mentions this issue: reflect: consistently document when value must be settable

@dmitshur dmitshur added this to the Go1.24 milestone Dec 11, 2024
@dmitshur dmitshur added the NeedsFix The path to resolution is known, but the work has not been done. label Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Documentation Issues describing a change to documentation. NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants