From 47efdcbf4c40a718ba6e1c28b8bb7b73e729f2b0 Mon Sep 17 00:00:00 2001 From: zhouguangyuan Date: Sun, 20 Mar 2022 02:10:17 +0800 Subject: [PATCH] internal/reflectlite: fix name of type parameter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CL 372774 is for reflect, this CL is for internal/reflectlite. Updates #50208 Change-Id: Ib7e8b1bc031feab218d1addd78388fcfe9b675b5 Reviewed-on: https://go-review.googlesource.com/c/go/+/393918 Run-TryBot: Ian Lance Taylor TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor Reviewed-by: Daniel Martí Trust: Daniel Martí --- src/internal/reflectlite/all_test.go | 5 +++++ src/internal/reflectlite/type.go | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/internal/reflectlite/all_test.go b/src/internal/reflectlite/all_test.go index ea750831efb..eb6aaa9c285 100644 --- a/src/internal/reflectlite/all_test.go +++ b/src/internal/reflectlite/all_test.go @@ -958,6 +958,9 @@ type nameTest struct { want string } +type A struct{} +type B[T any] struct{} + var nameTests = []nameTest{ {(*int32)(nil), "int32"}, {(*D1)(nil), "D1"}, @@ -971,6 +974,8 @@ var nameTests = []nameTest{ F() })(nil), ""}, {(*TheNameOfThisTypeIsExactly255BytesLongSoWhenTheCompilerPrependsTheReflectTestPackageNameAndExtraStarTheLinkerRuntimeAndReflectPackagesWillHaveToCorrectlyDecodeTheSecondLengthByte0123456789_0123456789_0123456789_0123456789_0123456789_012345678)(nil), "TheNameOfThisTypeIsExactly255BytesLongSoWhenTheCompilerPrependsTheReflectTestPackageNameAndExtraStarTheLinkerRuntimeAndReflectPackagesWillHaveToCorrectlyDecodeTheSecondLengthByte0123456789_0123456789_0123456789_0123456789_0123456789_012345678"}, + {(*B[A])(nil), "B[reflectlite_test.A]"}, + {(*B[B[A]])(nil), "B[reflectlite_test.B[reflectlite_test.A]]"}, } func TestNames(t *testing.T) { diff --git a/src/internal/reflectlite/type.go b/src/internal/reflectlite/type.go index 8f649600d21..34677b400ec 100644 --- a/src/internal/reflectlite/type.go +++ b/src/internal/reflectlite/type.go @@ -577,7 +577,14 @@ func (t *rtype) Name() string { } s := t.String() i := len(s) - 1 - for i >= 0 && s[i] != '.' { + sqBrackets := 0 + for i >= 0 && (s[i] != '.' || sqBrackets != 0) { + switch s[i] { + case ']': + sqBrackets++ + case '[': + sqBrackets-- + } i-- } return s[i+1:]