diff options
author | Rob Pike <r@golang.org> | 2012-04-23 12:07:02 +1000 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2012-04-23 12:07:02 +1000 |
commit | 86990145a6647d357776c0196066c8c69b9f877a (patch) | |
tree | de1fd63aef2b5bca9ac72f5f1554ac044036fcf6 | |
parent | 8b5350b8e4b8b23c49f8e8386d4a57848738a867 (diff) | |
download | go-86990145a6647d357776c0196066c8c69b9f877a.tar.gz go-86990145a6647d357776c0196066c8c69b9f877a.zip |
[release-branch.go1] reflect: document and test TypeOf(nil)
««« backport 82aaf0925029
reflect: document and test TypeOf(nil)
Fixes #3549.
R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/6107047
»»»
-rw-r--r-- | src/pkg/reflect/all_test.go | 5 | ||||
-rw-r--r-- | src/pkg/reflect/type.go | 1 |
2 files changed, 6 insertions, 0 deletions
diff --git a/src/pkg/reflect/all_test.go b/src/pkg/reflect/all_test.go index 6bb0613981..e331405635 100644 --- a/src/pkg/reflect/all_test.go +++ b/src/pkg/reflect/all_test.go @@ -638,6 +638,7 @@ var ( var deepEqualTests = []DeepEqualTest{ // Equalities + {nil, nil, true}, {1, 1, true}, {int32(1), int32(1), true}, {0.5, 0.5, true}, @@ -696,6 +697,10 @@ func TestDeepEqual(t *testing.T) { } func TestTypeOf(t *testing.T) { + // Special case for nil + if typ := TypeOf(nil); typ != nil { + t.Errorf("expected nil type for nil value; got %v", typ) + } for _, test := range deepEqualTests { v := ValueOf(test.a) if !v.IsValid() { diff --git a/src/pkg/reflect/type.go b/src/pkg/reflect/type.go index 64550b8f6c..060bde3aff 100644 --- a/src/pkg/reflect/type.go +++ b/src/pkg/reflect/type.go @@ -940,6 +940,7 @@ func toType(p *runtimeType) Type { } // TypeOf returns the reflection Type of the value in the interface{}. +// TypeOf(nil) returns nil. func TypeOf(i interface{}) Type { eface := *(*emptyInterface)(unsafe.Pointer(&i)) return toType(eface.typ) |