]> Cypherpunks.ru repositories - gostls13.git/commitdiff
reflect: fix name of type parameter
authorzhouguangyuan <zhouguangyuan.xian@gmail.com>
Thu, 16 Dec 2021 14:18:26 +0000 (22:18 +0800)
committerKeith Randall <khr@golang.org>
Thu, 16 Dec 2021 15:44:40 +0000 (15:44 +0000)
Fixes #50208

Change-Id: Ib0aff56341adb98ff6831c5badd1603ebf002b79
Reviewed-on: https://go-review.googlesource.com/c/go/+/372774
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Trust: Cherry Mui <cherryyz@google.com>

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

index 9c8434c22c953e41993531d6777f5895ca99bc46..866f38e68745d318ae60fae3f63f252a4f969650 100644 (file)
@@ -7768,3 +7768,17 @@ func TestMethodCallValueCodePtr(t *testing.T) {
                t.Errorf("methodValueCall code pointer mismatched, want: %v, got: %v", want, got)
        }
 }
+
+type A struct{}
+type B[T any] struct{}
+
+func TestIssue50208(t *testing.T) {
+       want1 := "B[reflect_test.A]"
+       if got := TypeOf(new(B[A])).Elem().Name(); got != want1 {
+               t.Errorf("name of type parameter mismatched, want:%s, got:%s", want1, got)
+       }
+       want2 := "B[reflect_test.B[reflect_test.A]]"
+       if got := TypeOf(new(B[B[A]])).Elem().Name(); got != want2 {
+               t.Errorf("name of type parameter mismatched, want:%s, got:%s", want2, got)
+       }
+}
index 6217291a3f2aed0ef46d57084c31eeda6ba61509..4e03dc3382ef1e28c873e3943ef2d8951786f135 100644 (file)
@@ -915,7 +915,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:]