]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: fix name of type parameter
authorzhouguangyuan <zhouguangyuan.xian@gmail.com>
Sat, 19 Mar 2022 16:07:37 +0000 (00:07 +0800)
committerGopher Robot <gobot@golang.org>
Tue, 12 Apr 2022 00:32:29 +0000 (00:32 +0000)
CL 372774 is for reflect, this CL is for _type in runtime.
Add a test case to ensure the name method of _type can be exercised.

Updates #50208

Change-Id: I26ccf8c5c574dd9e78510cf29eb40ae7c8d449ab
Reviewed-on: https://go-review.googlesource.com/c/go/+/393917
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Run-TryBot: Keith Randall <khr@google.com>
Auto-Submit: Keith Randall <khr@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/runtime/debug/heapdump_test.go
src/runtime/type.go

index 768934d05db38ac92605c2cdebcd6719fd0655d5..ee6b054b117afd3c6459ed1becf3ffee46f112cb 100644 (file)
@@ -67,3 +67,29 @@ func TestWriteHeapDumpFinalizers(t *testing.T) {
        WriteHeapDump(f.Fd())
        println("done dump")
 }
+
+type G[T any] struct{}
+type I interface {
+       M()
+}
+
+//go:noinline
+func (g G[T]) M() {}
+
+var dummy I = G[int]{}
+var dummy2 I = G[G[int]]{}
+
+func TestWriteHeapDumpTypeName(t *testing.T) {
+       if runtime.GOOS == "js" {
+               t.Skipf("WriteHeapDump is not available on %s.", runtime.GOOS)
+       }
+       f, err := os.CreateTemp("", "heapdumptest")
+       if err != nil {
+               t.Fatalf("TempFile failed: %v", err)
+       }
+       defer os.Remove(f.Name())
+       defer f.Close()
+       WriteHeapDump(f.Fd())
+       dummy.M()
+       dummy2.M()
+}
index 44f36a85cadf48d181bfdd6fc179d28e80ffdcee..b650d6d79598195fcd5f13314d086044aa49d80b 100644 (file)
@@ -127,7 +127,14 @@ func (t *_type) 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:]