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/ast: TypeSpec.Doc is nil? Have to grab documentation at GenDecl #27477

Closed
xibz opened this issue Sep 3, 2018 · 3 comments
Closed

go/ast: TypeSpec.Doc is nil? Have to grab documentation at GenDecl #27477

xibz opened this issue Sep 3, 2018 · 3 comments
Labels
Documentation FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@xibz
Copy link

xibz commented Sep 3, 2018

What version of Go are you using (go version)?

1.11

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

darwin

Question

Noticed in the AST for type instantiated structures, that if I go to the Doc field in ast.TypeSpec the value is nil. I have to visit the ast.GenDecl node instead to grab doc fields.

Is this intended?
If so, why does ast.TypeSpec have a Doc field? Should Doc along with Comment be deprecated, as in mentioned in the docs, since comments/documentation really do not have anything to do with the type specification?

This can reproduce by parsing the code sample below

package foo

// documentation goes here
type Foo struct{}

func main() {
    f := Foo{}
}

expectation

ast.TypeSpec contain non-nil value for Doc when documentation is provided.

@xibz xibz changed the title TypeSpec.Doc is nil? Have to grab documentation at GenDecl go/ast: TypeSpec.Doc is nil? Have to grab documentation at GenDecl Sep 3, 2018
@ianlancetaylor
Copy link
Contributor

CC @griesemer

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 3, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.12 milestone Sep 3, 2018
@griesemer
Copy link
Contributor

This is working as intended. A type declaration of the form

// comment
type T struct{}

is simply a shortcut for

// comment
type (
   T struct{}
)

since the comment is before the type keyword. If you want the the comment in TypeSpec.Doc field, then you need to write:

type (
   // comment
   T struct {}
)

There is really not much choice here: The AST needs to be able to represent both these cases (for gofmt), so it needs to be able to attach comments both with the grouped type declaration (GenDecl) and each individual type declaration (TypeSpec). For instance, in:

// comment1
type (
   // comment2
   T struct{}
)

there needs to be space for both comments.

Closing.

@xibz
Copy link
Author

xibz commented Sep 5, 2018

Thanks for the clarification @griesemer. That makes a lot of sense. I did not know you could shorthand structure definitions like that. Cheers :)

@golang golang locked and limited conversation to collaborators Sep 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants