Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
go/printer: AST rewriting and then formatting results in mangled godocs #11775
In golang.org/cl/12373, I wrote a cmd/fix rule to rewrite "types.Typ[x]" into "x.Basic()". However, the naive approach causes
to get formatted as
I.e., the godoc for g is moved up into the arguments section for the .Basic() invocation.
As a workaround, I found setting Rparen seems to prevent the issue. I wouldn't expect that to be necessary though.
This is a well-known problem: comments are placed based on their original source code positions and thus don't "flow" with the nodes they seem to be attached to (they are not attached). You can use an ast.CommentSet to work around this issue.
Unfortunately this is "working as intended" - and also one of the major reasons why the go/ast could use a major overhaul.