diff --git a/src/runtime/debug/heapdump_test.go b/src/runtime/debug/heapdump_test.go index 768934d05db..ee6b054b117 100644 --- a/src/runtime/debug/heapdump_test.go +++ b/src/runtime/debug/heapdump_test.go @@ -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() +} diff --git a/src/runtime/type.go b/src/runtime/type.go index 44f36a85cad..b650d6d7959 100644 --- a/src/runtime/type.go +++ b/src/runtime/type.go @@ -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:]