1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:44:45 -07:00

reflect: fix String of new array types

When constructing a new type for an array type in ArrayOf, we don't
reset tflag to 0. All the other methods in the package, such as SliceOf,
do this already. This results in the new array type having weird issues
when being printed, such as having tflagExtraStar set when it shouldn't.

That flag removes the first char to get rid of '*', but when used
incorrectly in this case it eats the '[' character leading to broken
strings like "3]int".

This was fixed in 56752eb2 for issue #16722, but ArrayOf was missed.

Also make the XM test struct have a non-zero size as that leads to a
division by zero panic in ArrayOf.

Fixes #20311.

Change-Id: I18f1027fdbe9f71767201e7424269c3ceeb23eb5
Reviewed-on: https://go-review.googlesource.com/43130
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Daniel Martí 2017-05-10 13:10:46 +02:00
parent 266a3b66ca
commit a4864094f0
2 changed files with 3 additions and 1 deletions

View File

@ -5861,7 +5861,7 @@ func TestTypeOfTypeOf(t *testing.T) {
check("SliceOf", SliceOf(TypeOf(T{})))
}
type XM struct{}
type XM struct{ _ bool }
func (*XM) String() string { return "" }
@ -6015,6 +6015,7 @@ func TestTypeStrings(t *testing.T) {
{TypeOf(new(XM)).Method(0).Type, "func(*reflect_test.XM) string"},
{ChanOf(3, TypeOf(XM{})), "chan reflect_test.XM"},
{MapOf(TypeOf(int(0)), TypeOf(XM{})), "map[int]reflect_test.XM"},
{ArrayOf(3, TypeOf(XM{})), "[3]reflect_test.XM"},
}
for i, test := range stringTests {

View File

@ -2805,6 +2805,7 @@ func ArrayOf(count int, elem Type) Type {
var iarray interface{} = [1]unsafe.Pointer{}
prototype := *(**arrayType)(unsafe.Pointer(&iarray))
array := *prototype
array.tflag = 0
array.str = resolveReflectName(newName(s, "", "", false))
array.hash = fnv1(typ.hash, '[')
for n := uint32(count); n > 0; n >>= 8 {