Skip to content

Commit a9922d0

Browse files
ianlancetaylorgopherbot
authored andcommitted
reflect: consistently document when value must be settable
Fixes #70760 Change-Id: Ia00723698b7e502fa2c63f8f1dbe1143af22e0a5 Reviewed-on: https://go-review.googlesource.com/c/go/+/634799 Reviewed-by: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Commit-Queue: Ian Lance Taylor <[email protected]> Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent 4ce116a commit a9922d0

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

src/reflect/map_noswiss.go

+2
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ func (iter *MapIter) Key() Value {
289289
// It is equivalent to v.Set(iter.Key()), but it avoids allocating a new Value.
290290
// As in Go, the key must be assignable to v's type and
291291
// must not be derived from an unexported field.
292+
// It panics if [Value.CanSet] returns false.
292293
func (v Value) SetIterKey(iter *MapIter) {
293294
if !iter.hiter.initialized() {
294295
panic("reflect: Value.SetIterKey called before Next")
@@ -332,6 +333,7 @@ func (iter *MapIter) Value() Value {
332333
// It is equivalent to v.Set(iter.Value()), but it avoids allocating a new Value.
333334
// As in Go, the value must be assignable to v's type and
334335
// must not be derived from an unexported field.
336+
// It panics if [Value.CanSet] returns false.
335337
func (v Value) SetIterValue(iter *MapIter) {
336338
if !iter.hiter.initialized() {
337339
panic("reflect: Value.SetIterValue called before Next")

src/reflect/map_swiss.go

+2
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ func (iter *MapIter) Key() Value {
240240
// It is equivalent to v.Set(iter.Key()), but it avoids allocating a new Value.
241241
// As in Go, the key must be assignable to v's type and
242242
// must not be derived from an unexported field.
243+
// It panics if [Value.CanSet] returns false.
243244
func (v Value) SetIterKey(iter *MapIter) {
244245
if !iter.hiter.Initialized() {
245246
panic("reflect: Value.SetIterKey called before Next")
@@ -283,6 +284,7 @@ func (iter *MapIter) Value() Value {
283284
// It is equivalent to v.Set(iter.Value()), but it avoids allocating a new Value.
284285
// As in Go, the value must be assignable to v's type and
285286
// must not be derived from an unexported field.
287+
// It panics if [Value.CanSet] returns false.
286288
func (v Value) SetIterValue(iter *MapIter) {
287289
if !iter.hiter.Initialized() {
288290
panic("reflect: Value.SetIterValue called before Next")

src/reflect/value.go

+23-13
Original file line numberDiff line numberDiff line change
@@ -2072,7 +2072,8 @@ func (v Value) SetBool(x bool) {
20722072
}
20732073

20742074
// SetBytes sets v's underlying value.
2075-
// It panics if v's underlying value is not a slice of bytes.
2075+
// It panics if v's underlying value is not a slice of bytes
2076+
// or if [Value.CanSet] returns false.
20762077
func (v Value) SetBytes(x []byte) {
20772078
v.mustBeAssignable()
20782079
v.mustBe(Slice)
@@ -2083,7 +2084,8 @@ func (v Value) SetBytes(x []byte) {
20832084
}
20842085

20852086
// setRunes sets v's underlying value.
2086-
// It panics if v's underlying value is not a slice of runes (int32s).
2087+
// It panics if v's underlying value is not a slice of runes (int32s)
2088+
// or if [Value.CanSet] returns false.
20872089
func (v Value) setRunes(x []rune) {
20882090
v.mustBeAssignable()
20892091
v.mustBe(Slice)
@@ -2094,7 +2096,8 @@ func (v Value) setRunes(x []rune) {
20942096
}
20952097

20962098
// SetComplex sets v's underlying value to x.
2097-
// It panics if v's Kind is not [Complex64] or [Complex128], or if [Value.CanSet] returns false.
2099+
// It panics if v's Kind is not [Complex64] or [Complex128],
2100+
// or if [Value.CanSet] returns false.
20982101
func (v Value) SetComplex(x complex128) {
20992102
v.mustBeAssignable()
21002103
switch k := v.kind(); k {
@@ -2108,7 +2111,8 @@ func (v Value) SetComplex(x complex128) {
21082111
}
21092112

21102113
// SetFloat sets v's underlying value to x.
2111-
// It panics if v's Kind is not [Float32] or [Float64], or if [Value.CanSet] returns false.
2114+
// It panics if v's Kind is not [Float32] or [Float64],
2115+
// or if [Value.CanSet] returns false.
21122116
func (v Value) SetFloat(x float64) {
21132117
v.mustBeAssignable()
21142118
switch k := v.kind(); k {
@@ -2122,7 +2126,8 @@ func (v Value) SetFloat(x float64) {
21222126
}
21232127

21242128
// SetInt sets v's underlying value to x.
2125-
// It panics if v's Kind is not [Int], [Int8], [Int16], [Int32], or [Int64], or if [Value.CanSet] returns false.
2129+
// It panics if v's Kind is not [Int], [Int8], [Int16], [Int32], or [Int64],
2130+
// or if [Value.CanSet] returns false.
21262131
func (v Value) SetInt(x int64) {
21272132
v.mustBeAssignable()
21282133
switch k := v.kind(); k {
@@ -2142,8 +2147,9 @@ func (v Value) SetInt(x int64) {
21422147
}
21432148

21442149
// SetLen sets v's length to n.
2145-
// It panics if v's Kind is not [Slice] or if n is negative or
2146-
// greater than the capacity of the slice.
2150+
// It panics if v's Kind is not [Slice], or if n is negative or
2151+
// greater than the capacity of the slice,
2152+
// or if [Value.CanSet] returns false.
21472153
func (v Value) SetLen(n int) {
21482154
v.mustBeAssignable()
21492155
v.mustBe(Slice)
@@ -2155,8 +2161,9 @@ func (v Value) SetLen(n int) {
21552161
}
21562162

21572163
// SetCap sets v's capacity to n.
2158-
// It panics if v's Kind is not [Slice] or if n is smaller than the length or
2159-
// greater than the capacity of the slice.
2164+
// It panics if v's Kind is not [Slice], or if n is smaller than the length or
2165+
// greater than the capacity of the slice,
2166+
// or if [Value.CanSet] returns false.
21602167
func (v Value) SetCap(n int) {
21612168
v.mustBeAssignable()
21622169
v.mustBe(Slice)
@@ -2168,7 +2175,8 @@ func (v Value) SetCap(n int) {
21682175
}
21692176

21702177
// SetUint sets v's underlying value to x.
2171-
// It panics if v's Kind is not [Uint], [Uintptr], [Uint8], [Uint16], [Uint32], or [Uint64], or if [Value.CanSet] returns false.
2178+
// It panics if v's Kind is not [Uint], [Uintptr], [Uint8], [Uint16], [Uint32], or [Uint64],
2179+
// or if [Value.CanSet] returns false.
21722180
func (v Value) SetUint(x uint64) {
21732181
v.mustBeAssignable()
21742182
switch k := v.kind(); k {
@@ -2190,7 +2198,8 @@ func (v Value) SetUint(x uint64) {
21902198
}
21912199

21922200
// SetPointer sets the [unsafe.Pointer] value v to x.
2193-
// It panics if v's Kind is not [UnsafePointer].
2201+
// It panics if v's Kind is not [UnsafePointer]
2202+
// or if [Value.CanSet] returns false.
21942203
func (v Value) SetPointer(x unsafe.Pointer) {
21952204
v.mustBeAssignable()
21962205
v.mustBe(UnsafePointer)
@@ -2558,8 +2567,8 @@ func arrayAt(p unsafe.Pointer, i int, eltSize uintptr, whySafe string) unsafe.Po
25582567
// another n elements. After Grow(n), at least n elements can be appended
25592568
// to the slice without another allocation.
25602569
//
2561-
// It panics if v's Kind is not a [Slice] or if n is negative or too large to
2562-
// allocate the memory.
2570+
// It panics if v's Kind is not a [Slice], or if n is negative or too large to
2571+
// allocate the memory, or if [Value.CanSet] returns false.
25632572
func (v Value) Grow(n int) {
25642573
v.mustBeAssignable()
25652574
v.mustBe(Slice)
@@ -2647,6 +2656,7 @@ func AppendSlice(s, t Value) Value {
26472656
// It returns the number of elements copied.
26482657
// Dst and src each must have kind [Slice] or [Array], and
26492658
// dst and src must have the same element type.
2659+
// It dst is an [Array], it panics if [Value.CanSet] returns false.
26502660
//
26512661
// As a special case, src can have kind [String] if the element type of dst is kind [Uint8].
26522662
func Copy(dst, src Value) int {

0 commit comments

Comments
 (0)