Skip to content

Commit a11cd6f

Browse files
committed
go/doc: recognize methods on generic types as Funcs
When writing markdown for godoc, we can reference a method M of a type T as [T.M]. This doesn't currently work for methods on generic types because the declaration of the type parameter gets in the way. (You'd have to write [T[P].M] and that doesn't parse, and even if it did you'd have to spell "P" correctly.) Get rid of the type parameter when building the list of Funcs so [T.M] works in godoc if T is generic. Change-Id: I8ef5264124a944967df3ce20ddd40a2447ff4187 Reviewed-on: https://go-review.googlesource.com/c/go/+/449236 Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent c3d444d commit a11cd6f

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

src/go/doc/comment_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ func TestComment(t *testing.T) {
2424
pkg := New(pkgs["pkgdoc"], "testdata/pkgdoc", 0)
2525

2626
var (
27-
input = "[T] and [U] are types, and [T.M] is a method, but [V] is a broken link. [rand.Int] and [crand.Reader] are things.\n"
28-
wantHTML = `<p><a href="#T">T</a> and <a href="#U">U</a> are types, and <a href="#T.M">T.M</a> is a method, but [V] is a broken link. <a href="/math/rand#Int">rand.Int</a> and <a href="/crypto/rand#Reader">crand.Reader</a> are things.` + "\n"
29-
wantOldHTML = "<p>[T] and [U] are <i>types</i>, and [T.M] is a method, but [V] is a broken link. [rand.Int] and [crand.Reader] are things.\n"
30-
wantMarkdown = "[T](#T) and [U](#U) are types, and [T.M](#T.M) is a method, but \\[V] is a broken link. [rand.Int](/math/rand#Int) and [crand.Reader](/crypto/rand#Reader) are things.\n"
31-
wantText = "T and U are types, and T.M is a method, but [V] is a broken link. rand.Int and\ncrand.Reader are things.\n"
32-
wantOldText = "[T] and [U] are types, and [T.M] is a method, but [V] is a broken link.\n[rand.Int] and [crand.Reader] are things.\n"
27+
input = "[T] and [U] are types, and [T.M] is a method, but [V] is a broken link. [rand.Int] and [crand.Reader] are things. [G.M1] and [G.M2] are generic methods.\n"
28+
wantHTML = `<p><a href="#T">T</a> and <a href="#U">U</a> are types, and <a href="#T.M">T.M</a> is a method, but [V] is a broken link. <a href="/math/rand#Int">rand.Int</a> and <a href="/crypto/rand#Reader">crand.Reader</a> are things. <a href="#G.M1">G.M1</a> and <a href="#G.M2">G.M2</a> are generic methods.` + "\n"
29+
wantOldHTML = "<p>[T] and [U] are <i>types</i>, and [T.M] is a method, but [V] is a broken link. [rand.Int] and [crand.Reader] are things. [G.M1] and [G.M2] are generic methods.\n"
30+
wantMarkdown = "[T](#T) and [U](#U) are types, and [T.M](#T.M) is a method, but \\[V] is a broken link. [rand.Int](/math/rand#Int) and [crand.Reader](/crypto/rand#Reader) are things. [G.M1](#G.M1) and [G.M2](#G.M2) are generic methods.\n"
31+
wantText = "T and U are types, and T.M is a method, but [V] is a broken link. rand.Int and\ncrand.Reader are things. G.M1 and G.M2 are generic methods.\n"
32+
wantOldText = "[T] and [U] are types, and [T.M] is a method, but [V] is a broken link.\n[rand.Int] and [crand.Reader] are things. [G.M1] and [G.M2] are generic methods.\n"
3333
wantSynopsis = "T and U are types, and T.M is a method, but [V] is a broken link."
3434
wantOldSynopsis = "[T] and [U] are types, and [T.M] is a method, but [V] is a broken link."
3535
)

src/go/doc/doc.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type Func struct {
7676

7777
// methods
7878
// (for functions, these fields have the respective zero value)
79-
Recv string // actual receiver "T" or "*T"
79+
Recv string // actual receiver "T" or "*T" possibly followed by type parameters [P1, ..., Pn]
8080
Orig string // original receiver "T" or "*T"
8181
Level int // embedding level; 0 means not embedded
8282

@@ -173,7 +173,11 @@ func (p *Package) collectTypes(types []*Type) {
173173
func (p *Package) collectFuncs(funcs []*Func) {
174174
for _, f := range funcs {
175175
if f.Recv != "" {
176-
p.syms[strings.TrimPrefix(f.Recv, "*")+"."+f.Name] = true
176+
r := strings.TrimPrefix(f.Recv, "*")
177+
if i := strings.IndexByte(r, '['); i >= 0 {
178+
r = r[:i] // remove type parameters
179+
}
180+
p.syms[r+"."+f.Name] = true
177181
} else {
178182
p.syms[f.Name] = true
179183
}

src/go/doc/testdata/pkgdoc/doc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ func (T) M() {}
1717

1818
var _ = rand.Int
1919
var _ = crand.Reader
20+
21+
type G[T any] struct{ x T }
22+
23+
func (g G[T]) M1() {}
24+
func (g *G[T]) M2() {}

0 commit comments

Comments
 (0)