From: Robert Findley Date: Tue, 12 Oct 2021 15:54:09 +0000 (-0400) Subject: cmd/api: use placeholder names for type parameters X-Git-Tag: go1.18beta1~926 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=78d01be00b68d1f5c3f5eb493f053ba97adc92df;p=gostls13.git cmd/api: use placeholder names for type parameters 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 '$' 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 Run-TryBot: Robert Findley TryBot-Result: Go Bot Reviewed-by: Robert Griesemer --- diff --git a/src/cmd/api/goapi.go b/src/cmd/api/goapi.go index eca113a638..0c61b1b489 100644 --- a/src/cmd/api/goapi.go +++ b/src/cmd/api/goapi.go @@ -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()) diff --git a/src/cmd/api/testdata/src/pkg/p4/golden.txt b/src/cmd/api/testdata/src/pkg/p4/golden.txt index d5f282be8e..7997ab4471 100644 --- a/src/cmd/api/testdata/src/pkg/p4/golden.txt +++ b/src/cmd/api/testdata/src/pkg/p4/golden.txt @@ -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 diff --git a/src/cmd/api/testdata/src/pkg/p4/p4.go b/src/cmd/api/testdata/src/pkg/p4/p4.go index 187339b169..462a75be1a 100644 --- a/src/cmd/api/testdata/src/pkg/p4/p4.go +++ b/src/cmd/api/testdata/src/pkg/p4/p4.go @@ -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...) +}