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/format: formatting an empty var decl produces invalid syntax #63566

Closed
adonovan opened this issue Oct 16, 2023 · 3 comments
Closed

go/format: formatting an empty var decl produces invalid syntax #63566

adonovan opened this issue Oct 16, 2023 · 3 comments
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@adonovan
Copy link
Member

When the formatter is given a var decl with no specs, it produces the output "var", which cannot be parsed as a decl:

format.Node(os.Stdout, token.NewFileSet(), &ast.GenDecl{Tok: token.VAR}) // "var", sans parens

The output should be var ().

@cagedmantis cagedmantis added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 16, 2023
@cagedmantis cagedmantis added this to the Backlog milestone Oct 16, 2023
@mauri870
Copy link
Member

@adonovan I noticed the same happens with token.CONST and token.IMPORT, should these follow the same convention you suggested for var?

@adonovan
Copy link
Member Author

Yes, the logic is probably common to all of them.

mauri870 added a commit to mauri870/go that referenced this issue Oct 17, 2023
The current output for empty declarations such as var, const, import
results in "var", "const", "import" respectively. These are not valid
syntax and the parser will promptly reject them as invalid syntax.

This CL updates this behavior by adding "()" to the output of empty
decls so the syntax becomes valid, e.g "var ()" instead of "var".

Fixes golang#63566
@gopherbot
Copy link

Change https://go.dev/cl/535995 mentions this issue: go/printer: fix invalid output for empty decls

yunginnanet pushed a commit to yunginnanet/go that referenced this issue Oct 20, 2023
The current output for empty declarations such as var, const, import
results in "var", "const", "import" respectively. These are not valid
and the parser will promptly reject them as invalid syntax.

This CL updates this behavior by adding "()" to the output of empty
decls so the syntax becomes valid, e.g "var ()" instead of "var".

Fixes golang#63566

Change-Id: I571b182d9ccf71b159360c8de003ad55d0ff3443
GitHub-Last-Rev: 2720419
GitHub-Pull-Request: golang#63593
Reviewed-on: https://go-review.googlesource.com/c/go/+/535995
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Mauri de Souza Meneguzzo <mauri870@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants