From 4cdb5665c71cab400c38adcaf3297418cabf44d9 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 1 Oct 2014 16:51:32 -0400 Subject: reflect: fix IsValid vs Kind mismatch after Elem of nil interface LGTM=r R=r CC=golang-codereviews https://codereview.appspot.com/151960044 --- src/reflect/all_test.go | 14 ++++++++++++++ src/reflect/value.go | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go index b72c4b176..d17ef5c5e 100644 --- a/src/reflect/all_test.go +++ b/src/reflect/all_test.go @@ -3939,3 +3939,17 @@ func TestValueString(t *testing.T) { t.Errorf("ValueOf(Impl{}).Method(0).String() = %q, want %q", method.String(), "") } } + +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()) + } +} diff --git a/src/reflect/value.go b/src/reflect/value.go index 12d423f3c..9c65ee270 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -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 -- cgit v1.2.1