From a4864094f0e5eb2369831f5e5b734b6398837712 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Daniel=20Mart=C3=AD?= Date: Wed, 10 May 2017 13:10:46 +0200 Subject: [PATCH] reflect: fix String of new array types MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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í Reviewed-by: Brad Fitzpatrick Reviewed-by: David Crawshaw TryBot-Result: Gobot Gobot --- src/reflect/all_test.go | 3 ++- src/reflect/type.go | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go index 1ec4f7954c..b3b82f8b2a 100644 --- a/src/reflect/all_test.go +++ b/src/reflect/all_test.go @@ -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 { diff --git a/src/reflect/type.go b/src/reflect/type.go index 368b27ba51..637392f4e7 100644 --- a/src/reflect/type.go +++ b/src/reflect/type.go @@ -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 { -- 2.48.1