aboutsummaryrefslogtreecommitdiff
path: root/test/typeparam
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2022-08-18 15:16:23 -0700
committerMatthew Dempsky <mdempsky@google.com>2022-08-23 18:13:48 +0000
commitaa6a7fa775d8f38225ad74a622187bbe891eaf1c (patch)
tree9e161c751f4e112e3d15b6d4cb3acf2776b1686d /test/typeparam
parent72a76ca1f9c195ed39e929cf768d5df5421eada1 (diff)
downloadgo-aa6a7fa775d8f38225ad74a622187bbe891eaf1c.tar.gz
go-aa6a7fa775d8f38225ad74a622187bbe891eaf1c.zip
cmd/compile: fix reflect naming of local generic types
To disambiguate local types, we append a "·N" suffix to their name and then trim it off again when producing their runtime type descriptors. However, if a local type is generic, then we were further appending the type arguments after this suffix, and the code in types/fmt.go responsible for trimming didn't know to handle this. We could extend the types/fmt.go code to look for the "·N" suffix elsewhere in the type name, but this is risky because it could legitimately (albeit unlikely) appear in struct field tags. Instead, the most robust solution is to just change the mangling logic to keep the "·N" suffix at the end, where types/fmt.go can easily and reliably trim it. Note: the "·N" suffix is still visible within the type arguments list (e.g., the "·3" suffixes in nested.out), because we currently use the link strings in the type arguments list. Fixes #54456. Change-Id: Ie9beaf7e5330982f539bff57b8d48868a3674a37 Reviewed-on: https://go-review.googlesource.com/c/go/+/424901 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Than McIntosh <thanm@google.com>
Diffstat (limited to 'test/typeparam')
-rw-r--r--test/typeparam/issue54456.go37
-rw-r--r--test/typeparam/nested.out8
2 files changed, 41 insertions, 4 deletions
diff --git a/test/typeparam/issue54456.go b/test/typeparam/issue54456.go
new file mode 100644
index 0000000000..8342163e51
--- /dev/null
+++ b/test/typeparam/issue54456.go
@@ -0,0 +1,37 @@
+// run
+
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// The Go 1.18 frontend failed to disambiguate instantiations of
+// different, locally defined generic types with the same name.
+//
+// The unified frontend also exposed the scope-disambiguation mangling
+// to end users in reflect data.
+
+package main
+
+import (
+ "reflect"
+)
+
+func one() any { type T[_ any] int; return T[int](0) }
+func two() any { type T[_ any] int; return T[int](0) }
+
+func main() {
+ p, q := one(), two()
+
+ // p and q have different dynamic types; this comparison should
+ // evaluate false.
+ if p == q {
+ panic("bad type identity")
+ }
+
+ for _, x := range []any{p, q} {
+ // The names here should not contain "·1" or "·2".
+ if name := reflect.TypeOf(x).String(); name != "main.T[int]" {
+ panic(name)
+ }
+ }
+}
diff --git a/test/typeparam/nested.out b/test/typeparam/nested.out
index 37cb762e32..0836d9b0dc 100644
--- a/test/typeparam/nested.out
+++ b/test/typeparam/nested.out
@@ -1,4 +1,4 @@
-0,3: main.T·2[int;int]
-4,7: main.T·2[int;main.U·3[int;int]]
-22,23: main.T·2[main.Int;main.Int]
-26,27: main.T·2[main.Int;main.U·3[main.Int;main.Int]]
+0,3: main.T[int;int]
+4,7: main.T[int;main.U[int;int]·3]
+22,23: main.T[main.Int;main.Int]
+26,27: main.T[main.Int;main.U[main.Int;main.Int]·3]