go/token: line adjustment is incorrect for comments when imports are present #51371
Labels
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://go.dev/play/p/3FdwLDhsXfD
What did you expect to see?
Consistent line adjustment independent of the number of imports.
What did you see instead?
Broken line adjustment if imports exist.
An empty
import ()
also causes this breakage.Additional note, this appears only to affect comments.
Cause
This appears to be at least in part due to a discordance between how an
*ast.CommentGroup
's position is calculated and how a//line
directive works. When a line directive is part of a comment group, its pos is used to define the position of the comment group, but it refers to the next line. So changing thePos
method like sofixes the issue. This doesn't explain why imports affect the behaviour and I don't believe that it's a sensible fix.
It also raises a question about why the issue is also present when there is a blank line separating the directive from the comment group, when this is the case, the separated lines are still joined into the comment group. The reason for this is here where a single blank line is allowed between comment lines, but I don't understand why separated comments blocks should end up in the same comment group.
The text was updated successfully, but these errors were encountered: