reflect: avoid panic in reflect.Kind.String for negative Kind

Kind(-1).String() used to panic; let's not.

Change-Id: I1dfc0e3298beb37d77713d8327579bbde90dd156
Reviewed-on: https://go-review.googlesource.com/c/go/+/393015
Trust: Russ Cox <rsc@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Russ Cox 2022-03-15 13:36:10 -04:00
parent db3045b4be
commit 0d71234ee4
2 changed files with 11 additions and 2 deletions

View File

@ -7807,3 +7807,12 @@ func TestIssue50208(t *testing.T) {
t.Errorf("name of type parameter mismatched, want:%s, got:%s", want2, got)
}
}
func TestNegativeKindString(t *testing.T) {
x := -1
s := Kind(x).String()
want := "kind-1"
if s != want {
t.Fatalf("Kind(-1).String() = %q, want %q", s, want)
}
}

View File

@ -632,8 +632,8 @@ const (
// String returns the name of k.
func (k Kind) String() string {
if int(k) < len(kindNames) {
return kindNames[k]
if uint(k) < uint(len(kindNames)) {
return kindNames[uint(k)]
}
return "kind" + strconv.Itoa(int(k))
}