Skip to content

Commit c197250

Browse files
orirawlingsianlancetaylor
authored andcommitted
internal/reflectlite: include Kind in ValueError message
The implementation has been ported from reflect, but to avoid introducing a dependency on strconv, Kind.String() falls back to "invalid" if the Kind is unknown rather than "kind" + strconv.Itoa(int(k)) Fixes golang#39286 Change-Id: I82277242a6c41d0146dabd9d20339fe72d562500 Reviewed-on: https://go-review.googlesource.com/c/go/+/235522 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Trust: Dmitri Shuralyov <[email protected]>
1 parent f8e5540 commit c197250

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

src/internal/reflectlite/type.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,44 @@ const (
384384
kindMask = (1 << 5) - 1
385385
)
386386

387+
// String returns the name of k.
388+
func (k Kind) String() string {
389+
if int(k) < len(kindNames) {
390+
return kindNames[k]
391+
}
392+
return kindNames[0]
393+
}
394+
395+
var kindNames = []string{
396+
Invalid: "invalid",
397+
Bool: "bool",
398+
Int: "int",
399+
Int8: "int8",
400+
Int16: "int16",
401+
Int32: "int32",
402+
Int64: "int64",
403+
Uint: "uint",
404+
Uint8: "uint8",
405+
Uint16: "uint16",
406+
Uint32: "uint32",
407+
Uint64: "uint64",
408+
Uintptr: "uintptr",
409+
Float32: "float32",
410+
Float64: "float64",
411+
Complex64: "complex64",
412+
Complex128: "complex128",
413+
Array: "array",
414+
Chan: "chan",
415+
Func: "func",
416+
Interface: "interface",
417+
Map: "map",
418+
Ptr: "ptr",
419+
Slice: "slice",
420+
String: "string",
421+
Struct: "struct",
422+
UnsafePointer: "unsafe.Pointer",
423+
}
424+
387425
func (t *uncommonType) methods() []method {
388426
if t.mcount == 0 {
389427
return nil

src/internal/reflectlite/value.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ type ValueError struct {
160160
}
161161

162162
func (e *ValueError) Error() string {
163-
return "reflect: call of " + e.Method + " on zero Value"
163+
if e.Kind == 0 {
164+
return "reflect: call of " + e.Method + " on zero Value"
165+
}
166+
return "reflect: call of " + e.Method + " on " + e.Kind.String() + " Value"
164167
}
165168

166169
// methodName returns the name of the calling method,

0 commit comments

Comments
 (0)