]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: fix IsValid vs Kind mismatch after Elem of nil interface
authorRuss Cox <rsc@golang.org>
Wed, 1 Oct 2014 20:51:32 +0000 (16:51 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 1 Oct 2014 20:51:32 +0000 (16:51 -0400)
LGTM=r
R=r
CC=golang-codereviews
https://golang.org/cl/151960044

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

index b72c4b176d5ee805906cfaf0b25a220a10e7d6a7..d17ef5c5e9a8a314d89c3f4f5b7f6350cbad0dd4 100644 (file)
@@ -3939,3 +3939,17 @@ func TestValueString(t *testing.T) {
                t.Errorf("ValueOf(Impl{}).Method(0).String() = %q, want %q", method.String(), "<func() Value>")
        }
 }
+
+func TestInvalid(t *testing.T) {
+       // Used to have inconsistency between IsValid() and Kind() != Invalid.
+       type T struct{ v interface{} }
+
+       v := ValueOf(T{}).Field(0)
+       if v.IsValid() != true || v.Kind() != Interface {
+               t.Errorf("field: IsValid=%v, Kind=%v, want true, Interface", v.IsValid(), v.Kind())
+       }
+       v = v.Elem()
+       if v.IsValid() != false || v.Kind() != Invalid {
+               t.Errorf("field elem: IsValid=%v, Kind=%v, want false, Invalid", v.IsValid(), v.Kind())
+       }
+}
index 12d423f3c3e73a4d6cc9bb90f0414af0de0ce838..9c65ee27033ceaf3d355e7adc96db4c5dbd90af8 100644 (file)
@@ -791,7 +791,9 @@ func (v Value) Elem() Value {
                        })(v.ptr))
                }
                x := unpackEface(eface)
-               x.flag |= v.flag & flagRO
+               if x.flag != 0 {
+                       x.flag |= v.flag & flagRO
+               }
                return x
        case Ptr:
                ptr := v.ptr