diff options
author | Ian Lance Taylor <iant@golang.org> | 2022-04-16 18:28:29 -0700 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2022-04-21 23:38:34 +0000 |
commit | 01837ca7eb7d83dcbf760c032e1b25320f200510 (patch) | |
tree | fe13aeaa3f581850dd033afadb99a6ccc232c947 | |
parent | bb004a179a034c799809f42d525801ec4a791987 (diff) | |
download | go-01837ca7eb7d83dcbf760c032e1b25320f200510.tar.gz go-01837ca7eb7d83dcbf760c032e1b25320f200510.zip |
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>
-rw-r--r-- | src/reflect/value.go | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/reflect/value.go b/src/reflect/value.go index 6fe3cee017..de24d4c712 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -3056,9 +3056,10 @@ func NewAt(typ Type, p unsafe.Pointer) Value { return Value{t.ptrTo(), p, fl} } -// assignTo returns a value v that can be assigned directly to typ. -// It panics if v is not assignable to typ. -// For a conversion to an interface type, target is a suggested scratch space to use. +// assignTo returns a value v that can be assigned directly to dst. +// It panics if v is not assignable to dst. +// For a conversion to an interface type, target, if not nil, +// is a suggested scratch space to use. // target must be initialized memory (or nil). func (v Value) assignTo(context string, dst *rtype, target unsafe.Pointer) Value { if v.flag&flagMethod != 0 { @@ -3074,9 +3075,6 @@ func (v Value) assignTo(context string, dst *rtype, target unsafe.Pointer) Value return Value{dst, v.ptr, fl} case implements(dst, v.typ): - if target == nil { - target = unsafe_New(dst) - } if v.Kind() == Interface && v.IsNil() { // A nil ReadWriter passed to nil Reader is OK, // but using ifaceE2I below will panic. @@ -3084,6 +3082,9 @@ func (v Value) assignTo(context string, dst *rtype, target unsafe.Pointer) Value return Value{dst, nil, flag(Interface)} } x := valueInterface(v, false) + if target == nil { + target = unsafe_New(dst) + } if dst.NumMethod() == 0 { *(*any)(target) = x } else { |