Skip to content

Commit 01837ca

Browse files
ianlancetaylorgopherbot
authored andcommitted
reflect: in assignTo only allocate target if needed
Also correct parameter name in comment. Change-Id: Ic9486e08c2eea184faccf181cda7da808793def6 Reviewed-on: https://go-review.googlesource.com/c/go/+/400674 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Dan Kortschak <dan@kortschak.io> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
1 parent bb004a1 commit 01837ca

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/reflect/value.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3056,9 +3056,10 @@ func NewAt(typ Type, p unsafe.Pointer) Value {
30563056
return Value{t.ptrTo(), p, fl}
30573057
}
30583058

3059-
// assignTo returns a value v that can be assigned directly to typ.
3060-
// It panics if v is not assignable to typ.
3061-
// For a conversion to an interface type, target is a suggested scratch space to use.
3059+
// assignTo returns a value v that can be assigned directly to dst.
3060+
// It panics if v is not assignable to dst.
3061+
// For a conversion to an interface type, target, if not nil,
3062+
// is a suggested scratch space to use.
30623063
// target must be initialized memory (or nil).
30633064
func (v Value) assignTo(context string, dst *rtype, target unsafe.Pointer) Value {
30643065
if v.flag&flagMethod != 0 {
@@ -3074,16 +3075,16 @@ func (v Value) assignTo(context string, dst *rtype, target unsafe.Pointer) Value
30743075
return Value{dst, v.ptr, fl}
30753076

30763077
case implements(dst, v.typ):
3077-
if target == nil {
3078-
target = unsafe_New(dst)
3079-
}
30803078
if v.Kind() == Interface && v.IsNil() {
30813079
// A nil ReadWriter passed to nil Reader is OK,
30823080
// but using ifaceE2I below will panic.
30833081
// Avoid the panic by returning a nil dst (e.g., Reader) explicitly.
30843082
return Value{dst, nil, flag(Interface)}
30853083
}
30863084
x := valueInterface(v, false)
3085+
if target == nil {
3086+
target = unsafe_New(dst)
3087+
}
30873088
if dst.NumMethod() == 0 {
30883089
*(*any)(target) = x
30893090
} else {

0 commit comments

Comments
 (0)