Commit a48b35e9 authored by Robert Griesemer's avatar Robert Griesemer

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
parent 660ce142
...@@ -384,6 +384,13 @@ func TestPtrPointTo(t *testing.T) { ...@@ -384,6 +384,13 @@ func TestPtrPointTo(t *testing.T) {
if *ip != 1234 { if *ip != 1234 {
t.Errorf("got %d, want 1234", *ip) 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) { func TestPtrSetNil(t *testing.T) {
......
...@@ -1058,7 +1058,12 @@ func (v *PtrValue) SetValue(x Value) { ...@@ -1058,7 +1058,12 @@ func (v *PtrValue) SetValue(x Value) {
} }
// PointTo changes v to point to x. // PointTo changes v to point to x.
// If x is a nil Value, PointTo sets v to nil.
func (v *PtrValue) PointTo(x Value) { func (v *PtrValue) PointTo(x Value) {
if x == nil {
*(**uintptr)(v.addr) = nil
return
}
if !x.CanSet() { if !x.CanSet() {
panic("cannot set x; cannot point to x") panic("cannot set x; cannot point to x")
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment