Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/doc: Package.Parser should collect up interface methods #62293

Open
dsnet opened this issue Aug 25, 2023 · 5 comments
Open

go/doc: Package.Parser should collect up interface methods #62293

dsnet opened this issue Aug 25, 2023 · 5 comments
Labels
Documentation Issues describing a change to documentation. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Aug 25, 2023

Currently, the methods of a interface are not linkable because Package.Parser does not collect them up.

Thus, if you put a declaration like [Reader.Read] within the io package, the pkgsite is not able to link to the Read method.

It is unclear how to best fix this. Ideally interface methods appear in Type.Methods, but historically they have not.
Adding them in there could break tools that are not expecting their appearance.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/523059 mentions this issue: internal/godoc/dochtml/internal/render: fix linking of interface methods

@joedian joedian added Documentation Issues describing a change to documentation. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Aug 28, 2023
@peczenyj
Copy link

peczenyj commented Jun 28, 2024

Hey guys

I just note that links to public methods in a public interface are not linked correctly via godoc if it refers to the same package. Same for public fields:

notable examples

link to public method in type interface

https://pkg.go.dev/encoding/json@go1.22.4#MarshalerError

A MarshalerError represents an error from calling a [Marshaler.MarshalJSON] or encoding.TextMarshaler.MarshalText method.

// A MarshalerError represents an error from calling a
// [Marshaler.MarshalJSON] or [encoding.TextMarshaler.MarshalText] method.
type MarshalerError struct {

The rendering:

[Marshaler.MarshalJSON] does not work, expected https://pkg.go.dev/encoding/json#Marshaler.MarshalJSON

[encoding.TextMarshaler.MarshalText] => work as expected, became https://pkg.go.dev/encoding#TextMarshaler.MarshalText

Another example: https://pkg.go.dev/io@go1.22.4#WriteString

link to public field in type struct

https://pkg.go.dev/archive/tar@go1.22.4#Reader.Read

// Calling Read on special types like [TypeLink], [TypeSymlink], [TypeChar],
// [TypeBlock], [TypeDir], and [TypeFifo] returns (0, [io.EOF]) regardless of what
// the [Header.Size] claims.
func (tr *Reader) Read(b []byte) (int, error) {

Calling Read on special types like TypeLink, TypeSymlink, TypeChar, TypeBlock, TypeDir, and TypeFifo returns (0, io.EOF) regardless of what the [Header.Size] claims.

The rendering:

[Header.Size] does not work, expected https://pkg.go.dev/archive/tar#Header.Size

I can't find an example, but I think it may work from another package, like the previous issue in the public interface

link to builtin function defined on the current package:

https://pkg.go.dev/builtin@go1.22.4#recover

Prior to Go 1.21, recover would also return nil if panic is called with a nil argument. See [panic] for details.

The rendering:

[panic] does not work, expected https://pkg.go.dev/builtin@go1.22.4#panic


let me ask: this issue #62293 also refer to such errors? or should I open another ticket?

This is kinda annoying BTW

@AlexanderYastrebov
Copy link
Contributor

There is a duplicate #63679

@AlexanderYastrebov
Copy link
Contributor

It is unclear how to best fix this. Ideally interface methods appear in Type.Methods, but historically they have not.
Adding them in there could break tools that are not expecting their appearance.

Alternatively, to fix links it should be enough to add interface methods into p.syms like:

index 4d01ae458b..192554ec2b 100644
--- a/src/go/doc/doc.go
+++ b/src/go/doc/doc.go
@@ -167,6 +167,7 @@ func (p *Package) collectTypes(types []*Type) {
                p.collectValues(t.Vars)
                p.collectFuncs(t.Funcs)
                p.collectFuncs(t.Methods)
+               p.collectInterfaceMethods(t)
        }
 }
 
@@ -184,6 +185,21 @@ func (p *Package) collectFuncs(funcs []*Func) {
        }
 }
 
+func (p *Package) collectInterfaceMethods(t *Type) {
+       if t.Decl != nil && t.Decl.Tok == token.TYPE {
+               for _, s := range t.Decl.Specs {
+                       s := s.(*ast.TypeSpec) // token.TYPE guarantees this
+                       if i, ok := s.Type.(*ast.InterfaceType); ok {
+                               for _, m := range i.Methods.List {
+                                       if len(m.Names) > 0 { // skip embedded interfaces
+                                               p.syms[t.Name+"."+m.Names[0].Name] = true
+                                       }
+                               }
+                       }
+               }
+       }
+}
+

@Juneezee
Copy link
Contributor

Related to #54033

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Issues describing a change to documentation. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

7 participants