[go] go/doc: link to struct fields in the same package

1 view
Skip to first unread message

Sean Liao (Gerrit)

unread,
Dec 14, 2025, 2:04:36 PM (5 days ago) Dec 14
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Sean Liao has uploaded the change for review

Commit message

go/doc: link to struct fields in the same package

This collects up any struct direct field references
for linking. It does not attempt to do any validation
of embedded field conflicts as in CL 510315.

For #61394
Change-Id: I57dc0a0e8a71ce0bcb4e6c0963f459f76a6a6964

Change diff

diff --git a/src/go/doc/comment_test.go b/src/go/doc/comment_test.go
index 0e7de3e..2569e73 100644
--- a/src/go/doc/comment_test.go
+++ b/src/go/doc/comment_test.go
@@ -24,12 +24,12 @@
pkg := New(pkgs["pkgdoc"], "testdata/pkgdoc", 0)

var (
- 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. [I.F] is an interface method and [I.V] is a broken link.\n"
- 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. <a href="#I.F">I.F</a> is an interface method and [I.V] is a broken link.` + "\n"
- 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. [I.F] is an interface method and [I.V] is a broken link.\n"
- 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. [I.F](#I.F) is an interface method and \\[I.V] is a broken link.\n"
- 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. I.F is an interface\nmethod and [I.V] is a broken link.\n"
- 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[I.F] is an interface method and [I.V] is a broken link.\n"
+ 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.X] is a field, [G.M1] and [G.M2] are generic methods. [I.F] is an interface method and [I.V] is a broken link.\n"
+ 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.X">G.X</a> is a field, <a href="#G.M1">G.M1</a> and <a href="#G.M2">G.M2</a> are generic methods. <a href="#I.F">I.F</a> is an interface method and [I.V] is a broken link.` + "\n"
+ 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.X] is a field, [G.M1] and [G.M2] are generic methods. [I.F] is an interface method and [I.V] is a broken link.\n"
+ 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.X](#G.X) is a field, [G.M1](#G.M1) and [G.M2](#G.M2) are generic methods. [I.F](#I.F) is an interface method and \\[I.V] is a broken link.\n"
+ 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.X is a field, G.M1 and G.M2 are generic methods.\nI.F is an interface method and [I.V] is a broken link.\n"
+ 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.X] is a field, [G.M1] and [G.M2]\nare generic methods. [I.F] is an interface method and [I.V] is a broken link.\n"
wantSynopsis = "T and U are types, and T.M is a method, but [V] is a broken link."
wantOldSynopsis = "[T] and [U] are types, and [T.M] is a method, but [V] is a broken link."
)
diff --git a/src/go/doc/doc.go b/src/go/doc/doc.go
index 0c23f1a..8c78689 100644
--- a/src/go/doc/doc.go
+++ b/src/go/doc/doc.go
@@ -168,6 +168,7 @@
p.collectFuncs(t.Funcs)
p.collectFuncs(t.Methods)
p.collectInterfaceMethods(t)
+ p.collectStructFields(t)
}
}

@@ -212,6 +213,24 @@
}
}

+func (p *Package) collectStructFields(t *Type) {
+ for _, s := range t.Decl.Specs {
+ spec, ok := s.(*ast.TypeSpec)
+ if !ok {
+ continue
+ }
+ list, isStruct := fields(spec.Type)
+ if !isStruct {
+ continue
+ }
+ for _, field := range list {
+ for _, name := range field.Names {
+ p.syms[t.Name+"."+name.Name] = true
+ }
+ }
+ }
+}
+
// NewFromFiles computes documentation for a package.
//
// The package is specified by a list of *ast.Files and corresponding
diff --git a/src/go/doc/testdata/pkgdoc/doc.go b/src/go/doc/testdata/pkgdoc/doc.go
index d542dc2..24e127c 100644
--- a/src/go/doc/testdata/pkgdoc/doc.go
+++ b/src/go/doc/testdata/pkgdoc/doc.go
@@ -18,7 +18,7 @@
var _ = rand.Int
var _ = crand.Reader

-type G[T any] struct{ x T }
+type G[T any] struct{ X T }

func (g G[T]) M1() {}
func (g *G[T]) M2() {}

Change information

Files:
  • M src/go/doc/comment_test.go
  • M src/go/doc/doc.go
  • M src/go/doc/testdata/pkgdoc/doc.go
Change size: S
Delta: 3 files changed, 26 insertions(+), 7 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I57dc0a0e8a71ce0bcb4e6c0963f459f76a6a6964
Gerrit-Change-Number: 729980
Gerrit-PatchSet: 1
Gerrit-Owner: Sean Liao <se...@liao.dev>
unsatisfied_requirement
satisfied_requirement
open
diffy

Sean Liao (Gerrit)

unread,
Dec 14, 2025, 2:05:03 PM (5 days ago) Dec 14
to goph...@pubsubhelper.golang.org, Alan Donovan, golang-co...@googlegroups.com
Attention needed from Alan Donovan

Sean Liao voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Alan Donovan
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I57dc0a0e8a71ce0bcb4e6c0963f459f76a6a6964
Gerrit-Change-Number: 729980
Gerrit-PatchSet: 1
Gerrit-Owner: Sean Liao <se...@liao.dev>
Gerrit-Reviewer: Alan Donovan <adon...@google.com>
Gerrit-Reviewer: Sean Liao <se...@liao.dev>
Gerrit-Attention: Alan Donovan <adon...@google.com>
Gerrit-Comment-Date: Sun, 14 Dec 2025 19:04:56 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Michael Matloob (Gerrit)

unread,
10:55 AM (3 hours ago) 10:55 AM
to goph...@pubsubhelper.golang.org, Go LUCI, Alan Donovan, golang-co...@googlegroups.com
Attention needed from Alan Donovan and Sean Liao

Michael Matloob voted Code-Review+1

Code-Review+1
Open in Gerrit

Related details

Attention is currently required from:
  • Alan Donovan
  • Sean Liao
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I57dc0a0e8a71ce0bcb4e6c0963f459f76a6a6964
    Gerrit-Change-Number: 729980
    Gerrit-PatchSet: 1
    Gerrit-Owner: Sean Liao <se...@liao.dev>
    Gerrit-Reviewer: Alan Donovan <adon...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@google.com>
    Gerrit-Reviewer: Sean Liao <se...@liao.dev>
    Gerrit-Attention: Sean Liao <se...@liao.dev>
    Gerrit-Attention: Alan Donovan <adon...@google.com>
    Gerrit-Comment-Date: Fri, 19 Dec 2025 15:55:18 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    Michael Matloob (Gerrit)

    unread,
    10:55 AM (3 hours ago) 10:55 AM
    to goph...@pubsubhelper.golang.org, Michael Matloob, Go LUCI, Alan Donovan, golang-co...@googlegroups.com
    Attention needed from Alan Donovan and Sean Liao

    Michael Matloob voted Code-Review+2

    Code-Review+2
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Alan Donovan
    • Sean Liao
    Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I57dc0a0e8a71ce0bcb4e6c0963f459f76a6a6964
    Gerrit-Change-Number: 729980
    Gerrit-PatchSet: 1
    Gerrit-Owner: Sean Liao <se...@liao.dev>
    Gerrit-Reviewer: Alan Donovan <adon...@google.com>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-Reviewer: Michael Matloob <mat...@google.com>
    Gerrit-Reviewer: Sean Liao <se...@liao.dev>
    Gerrit-Attention: Sean Liao <se...@liao.dev>
    Gerrit-Attention: Alan Donovan <adon...@google.com>
    Gerrit-Comment-Date: Fri, 19 Dec 2025 15:55:50 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Michael Knyszek (Gerrit)

    unread,
    12:11 PM (1 hour ago) 12:11 PM
    to goph...@pubsubhelper.golang.org, Michael Matloob, Michael Matloob, Go LUCI, Alan Donovan, golang-co...@googlegroups.com
    Attention needed from Alan Donovan and Sean Liao

    Michael Knyszek voted Code-Review+1

    Code-Review+1
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Alan Donovan
    • Sean Liao
    Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement satisfiedReview-Enforcement
      • requirement satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I57dc0a0e8a71ce0bcb4e6c0963f459f76a6a6964
      Gerrit-Change-Number: 729980
      Gerrit-PatchSet: 1
      Gerrit-Owner: Sean Liao <se...@liao.dev>
      Gerrit-Reviewer: Alan Donovan <adon...@google.com>
      Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
      Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Matloob <mat...@google.com>
      Gerrit-Reviewer: Sean Liao <se...@liao.dev>
      Gerrit-Attention: Sean Liao <se...@liao.dev>
      Gerrit-Attention: Alan Donovan <adon...@google.com>
      Gerrit-Comment-Date: Fri, 19 Dec 2025 17:11:27 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages