go/printer: Invalid reformatting of slice type with a /* comment. #67868
Labels
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
Go version
devel go1.23-7492290887
Output of
go env
in your module/workspace:What did you do?
https://go.dev/play/p/ndVJVdo9nIW
Click format.
A realistic example could be formatting
var s [/*<10*/]byte
What did you see happen?
Formatting code with a "/*" comment and line break inside "[" "]" of a slice either fails or produces invalid code.
Clicking format fails with "24:16: expected type, found newline (and 1 more errors)".
Having multiple comments moves them 2 lines down (no error).
Line comments and multi-line comments are moved to the next position after "]" where line breaks don't insert a ";".
Setting a fake position to the same line as the element type before writing "]" changes the behavior. "go/printer/nodes.go:1049"
ast.ArrayType lacks position information about "]".
The closing "]" must be on the same line as the array length ends. On a slice type this implicit positioning spans multiple lines and the chosen line affects ;'s.
What did you expect to see?
Comments not moving across tokens when formatted.
I would also like the ast types to have positions for more tokens. Guessing the position requires different logic for different types: last line for slice "]", first line for map "]", first line for type assert ".", unknown for <- chan "chan".
The text was updated successfully, but these errors were encountered: