]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/api: use placeholder names for type parameters
authorRobert Findley <rfindley@google.com>
Tue, 12 Oct 2021 15:54:09 +0000 (11:54 -0400)
committerRobert Findley <rfindley@google.com>
Tue, 12 Oct 2021 22:26:55 +0000 (22:26 +0000)
Changing type parameter names is not a breaking API change, so we should
not include these names in the output of cmd/api. Instead print a
placeholder '$<index>' wherever type parameters are referenced.

This is valid for cmd/api as there is at most one type parameter list in
scope for any exported declaration. If we ever support method type
parameters, we'll need to revisit this syntax.

Change-Id: I7e677b1dab6ffeb0b79afefdb8d2580bef93891c
Reviewed-on: https://go-review.googlesource.com/c/go/+/355389
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/api/goapi.go
src/cmd/api/testdata/src/pkg/p4/golden.txt
src/cmd/api/testdata/src/pkg/p4/p4.go

index eca113a6388517c356ee3a434993829bb295fef3..0c61b1b489747e70e630f787f64cf5d90aca0723 100644 (file)
@@ -838,7 +838,8 @@ func (w *Walker) writeType(buf *bytes.Buffer, typ types.Type) {
                buf.WriteString(typ.Obj().Name())
 
        case *types.TypeParam:
-               buf.WriteString(typ.Obj().Name())
+               // Type parameter names may change, so use a placeholder instead.
+               fmt.Fprintf(buf, "$%d", typ.Index())
 
        default:
                panic(fmt.Sprintf("unknown type %T", typ))
@@ -870,7 +871,7 @@ func (w *Walker) writeTypeParams(buf *bytes.Buffer, tparams *types.TypeParamList
                        buf.WriteString(", ")
                }
                tp := tparams.At(i)
-               buf.WriteString(tp.Obj().Name())
+               w.writeType(buf, tp)
                if withConstraints {
                        buf.WriteByte(' ')
                        w.writeType(buf, tp.Constraint())
index d5f282be8e8f4fbbf49c987f2fe902b6a2ac039e..7997ab447123e83a5de90a695a7fa271bcb3d0a0 100644 (file)
@@ -1,4 +1,5 @@
-pkg p4, func NewPair[T1 interface{ M }, T2 interface{ ~int }](T1, T2) Pair
-pkg p4, method (Pair[_, X2]) Second() X2
-pkg p4, method (Pair[X1, _]) First() X1
-pkg p4, type Pair[T1 interface{ M }, T2 interface{ ~int }] struct
+pkg p4, func NewPair[$0 interface{ M }, $1 interface{ ~int }]($0, $1) Pair
+pkg p4, method (Pair[$0, $1]) Second() $1
+pkg p4, method (Pair[$0, $1]) First() $0
+pkg p4, type Pair[$0 interface{ M }, $1 interface{ ~int }] struct
+pkg p4, func Clone[$0 interface{ ~[]$1 }, $1 interface{}]($0) $0
index 187339b169bb4980d196032b899c00cfe0419579..462a75be1ad52b8d6b455853480205b5b673d0a3 100644 (file)
@@ -20,3 +20,7 @@ func (p Pair[X1, _]) First() X1 {
 func (p Pair[_, X2]) Second() X2 {
        return p.f2
 }
+
+func Clone[S ~[]T, T any](s S) S {
+       return append(S(nil), s...)
+}