Skip to content

Commit

Permalink
reflect: fix String of new array types
Browse files Browse the repository at this point in the history
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 56752eb for issue golang#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 golang#20311.

Change-Id: I18f1027fdbe9f71767201e7424269c3ceeb23eb5
Reviewed-on: https://go-review.googlesource.com/43130
Run-TryBot: Daniel Martí <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
Reviewed-by: David Crawshaw <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
mvdan committed May 10, 2017
1 parent 266a3b6 commit a486409
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/reflect/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "" }

Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/reflect/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a486409

Please sign in to comment.