]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: allow PtrValue.PointTo(nil)
authorRobert Griesemer <gri@golang.org>
Tue, 17 Aug 2010 22:12:28 +0000 (15:12 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 17 Aug 2010 22:12:28 +0000 (15:12 -0700)
(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

src/pkg/reflect/all_test.go
src/pkg/reflect/value.go

index 16b5ef6e96e4dba4f7abff9d5ac5aeb625e704c9..dc01890945c464d50b72b4bf1c2423a39d8a7787 100644 (file)
@@ -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) {
index 56a5d69d867142214abdf3d189229827b2a348ca..dd677b4ea8150f111fb55858ca14335bf21c1ba8 100644 (file)
@@ -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")
        }