]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: fix String of new array types
authorDaniel Martí <mvdan@mvdan.cc>
Wed, 10 May 2017 11:10:46 +0000 (13:10 +0200)
committerDaniel Martí <mvdan@mvdan.cc>
Wed, 10 May 2017 11:42:46 +0000 (11:42 +0000)
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>

src/reflect/all_test.go
src/reflect/type.go

index 1ec4f7954cc43725ac9eba4c6e60ac789c904a7e..b3b82f8b2ae28b291b4b76ce693a07a410cefc25 100644 (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 {
index 368b27ba518595675a5bde16488985367d8ae7df..637392f4e7688ebbc273726a806b8f4e477d7d79 100644 (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 {