Skip to content

Commit a48b35e

Browse files
committed
reflect: allow PtrValue.PointTo(nil)
(Argument: For any *PtrValue p, it should always be possible to do: p.PointTo(p.Elem()), even if p.Elem() is nil.) Fixes #1028. R=rsc CC=golang-dev, r https://golang.org/cl/1938044
1 parent 660ce14 commit a48b35e

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/pkg/reflect/all_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,13 @@ func TestPtrPointTo(t *testing.T) {
384384
if *ip != 1234 {
385385
t.Errorf("got %d, want 1234", *ip)
386386
}
387+
388+
ip = nil
389+
vp := NewValue(ip).(*PtrValue)
390+
vp.PointTo(vp.Elem())
391+
if ip != nil {
392+
t.Errorf("got non-nil (%p), want nil", ip)
393+
}
387394
}
388395

389396
func TestPtrSetNil(t *testing.T) {

src/pkg/reflect/value.go

+5
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,12 @@ func (v *PtrValue) SetValue(x Value) {
10581058
}
10591059

10601060
// PointTo changes v to point to x.
1061+
// If x is a nil Value, PointTo sets v to nil.
10611062
func (v *PtrValue) PointTo(x Value) {
1063+
if x == nil {
1064+
*(**uintptr)(v.addr) = nil
1065+
return
1066+
}
10621067
if !x.CanSet() {
10631068
panic("cannot set x; cannot point to x")
10641069
}

0 commit comments

Comments
 (0)