diff --git a/src/pkg/reflect/all_test.go b/src/pkg/reflect/all_test.go index 16b5ef6e96e..dc01890945c 100644 --- a/src/pkg/reflect/all_test.go +++ b/src/pkg/reflect/all_test.go @@ -384,6 +384,13 @@ func TestPtrPointTo(t *testing.T) { if *ip != 1234 { t.Errorf("got %d, want 1234", *ip) } + + ip = nil + vp := NewValue(ip).(*PtrValue) + vp.PointTo(vp.Elem()) + if ip != nil { + t.Errorf("got non-nil (%p), want nil", ip) + } } func TestPtrSetNil(t *testing.T) { diff --git a/src/pkg/reflect/value.go b/src/pkg/reflect/value.go index 56a5d69d867..dd677b4ea81 100644 --- a/src/pkg/reflect/value.go +++ b/src/pkg/reflect/value.go @@ -1058,7 +1058,12 @@ func (v *PtrValue) SetValue(x Value) { } // PointTo changes v to point to x. +// If x is a nil Value, PointTo sets v to nil. func (v *PtrValue) PointTo(x Value) { + if x == nil { + *(**uintptr)(v.addr) = nil + return + } if !x.CanSet() { panic("cannot set x; cannot point to x") }