1
0
mirror of https://github.com/golang/go synced 2024-11-19 00:54:42 -07:00

reflect: give type hints in error messages

Currently, if you call various reflect methods you might get a panic with a
message like, "reflect: Field of non-struct type". Sometimes it's easy to
grok what's going on, but other times you need to laboriously go perform
reflect.ValueOf(myType).Kind().

This CL just adds that detail to the error message, saving debuggers the
extra step and making the error message more clear.

Change-Id: I7e0c211a3001e6b217b828cbcf50518080b5cb1e
Reviewed-on: https://go-review.googlesource.com/c/go/+/183097
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Jean de Klerk 2019-06-19 18:02:09 -06:00 committed by Ian Lance Taylor
parent 3c6eaa7c0d
commit 09824ccfe5

View File

@ -878,7 +878,7 @@ func (t *rtype) Name() string {
func (t *rtype) ChanDir() ChanDir {
if t.Kind() != Chan {
panic("reflect: ChanDir of non-chan type")
panic("reflect: ChanDir of non-chan type " + t.String())
}
tt := (*chanType)(unsafe.Pointer(t))
return ChanDir(tt.dir)
@ -886,7 +886,7 @@ func (t *rtype) ChanDir() ChanDir {
func (t *rtype) IsVariadic() bool {
if t.Kind() != Func {
panic("reflect: IsVariadic of non-func type")
panic("reflect: IsVariadic of non-func type " + t.String())
}
tt := (*funcType)(unsafe.Pointer(t))
return tt.outCount&(1<<15) != 0
@ -910,12 +910,12 @@ func (t *rtype) Elem() Type {
tt := (*sliceType)(unsafe.Pointer(t))
return toType(tt.elem)
}
panic("reflect: Elem of invalid type")
panic("reflect: Elem of invalid type " + t.String())
}
func (t *rtype) Field(i int) StructField {
if t.Kind() != Struct {
panic("reflect: Field of non-struct type")
panic("reflect: Field of non-struct type " + t.String())
}
tt := (*structType)(unsafe.Pointer(t))
return tt.Field(i)
@ -923,7 +923,7 @@ func (t *rtype) Field(i int) StructField {
func (t *rtype) FieldByIndex(index []int) StructField {
if t.Kind() != Struct {
panic("reflect: FieldByIndex of non-struct type")
panic("reflect: FieldByIndex of non-struct type " + t.String())
}
tt := (*structType)(unsafe.Pointer(t))
return tt.FieldByIndex(index)
@ -931,7 +931,7 @@ func (t *rtype) FieldByIndex(index []int) StructField {
func (t *rtype) FieldByName(name string) (StructField, bool) {
if t.Kind() != Struct {
panic("reflect: FieldByName of non-struct type")
panic("reflect: FieldByName of non-struct type " + t.String())
}
tt := (*structType)(unsafe.Pointer(t))
return tt.FieldByName(name)
@ -939,7 +939,7 @@ func (t *rtype) FieldByName(name string) (StructField, bool) {
func (t *rtype) FieldByNameFunc(match func(string) bool) (StructField, bool) {
if t.Kind() != Struct {
panic("reflect: FieldByNameFunc of non-struct type")
panic("reflect: FieldByNameFunc of non-struct type " + t.String())
}
tt := (*structType)(unsafe.Pointer(t))
return tt.FieldByNameFunc(match)
@ -947,7 +947,7 @@ func (t *rtype) FieldByNameFunc(match func(string) bool) (StructField, bool) {
func (t *rtype) In(i int) Type {
if t.Kind() != Func {
panic("reflect: In of non-func type")
panic("reflect: In of non-func type " + t.String())
}
tt := (*funcType)(unsafe.Pointer(t))
return toType(tt.in()[i])
@ -955,7 +955,7 @@ func (t *rtype) In(i int) Type {
func (t *rtype) Key() Type {
if t.Kind() != Map {
panic("reflect: Key of non-map type")
panic("reflect: Key of non-map type " + t.String())
}
tt := (*mapType)(unsafe.Pointer(t))
return toType(tt.key)
@ -963,7 +963,7 @@ func (t *rtype) Key() Type {
func (t *rtype) Len() int {
if t.Kind() != Array {
panic("reflect: Len of non-array type")
panic("reflect: Len of non-array type " + t.String())
}
tt := (*arrayType)(unsafe.Pointer(t))
return int(tt.len)
@ -971,7 +971,7 @@ func (t *rtype) Len() int {
func (t *rtype) NumField() int {
if t.Kind() != Struct {
panic("reflect: NumField of non-struct type")
panic("reflect: NumField of non-struct type " + t.String())
}
tt := (*structType)(unsafe.Pointer(t))
return len(tt.fields)
@ -979,7 +979,7 @@ func (t *rtype) NumField() int {
func (t *rtype) NumIn() int {
if t.Kind() != Func {
panic("reflect: NumIn of non-func type")
panic("reflect: NumIn of non-func type " + t.String())
}
tt := (*funcType)(unsafe.Pointer(t))
return int(tt.inCount)
@ -987,7 +987,7 @@ func (t *rtype) NumIn() int {
func (t *rtype) NumOut() int {
if t.Kind() != Func {
panic("reflect: NumOut of non-func type")
panic("reflect: NumOut of non-func type " + t.String())
}
tt := (*funcType)(unsafe.Pointer(t))
return len(tt.out())
@ -995,7 +995,7 @@ func (t *rtype) NumOut() int {
func (t *rtype) Out(i int) Type {
if t.Kind() != Func {
panic("reflect: Out of non-func type")
panic("reflect: Out of non-func type " + t.String())
}
tt := (*funcType)(unsafe.Pointer(t))
return toType(tt.out()[i])
@ -2986,7 +2986,7 @@ var layoutCache sync.Map // map[layoutKey]layoutType
// the name for possible debugging use.
func funcLayout(t *funcType, rcvr *rtype) (frametype *rtype, argSize, retOffset uintptr, stk *bitVector, framePool *sync.Pool) {
if t.Kind() != Func {
panic("reflect: funcLayout of non-func type")
panic("reflect: funcLayout of non-func type " + t.String())
}
if rcvr != nil && rcvr.Kind() == Interface {
panic("reflect: funcLayout with interface receiver " + rcvr.String())