go/ast: no way to distinguish some For statements, like for ;; {}
and for {}
#44257
Comments
for ;; {}
and for {}
cc @griesemer |
Marked for Go 1.17 but only so it's on the radar if we decide to address this. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?
Parse
a
andb
: they produce the same AST. There is no way to distinguish them.Same for the cond-only for loops, we can't distinguish them:
With
SliceExpr
, we have a usefulSlice3
flag that helps to distinguish ambiguous cases.Maybe we can have something similar for the
ForStmt
? It can also be solved by adding semicolon positions to theForStmt
.I know that Go AST is (ahem) AST, not a parse tree, so some information can be lost there, but it seems like this would be useful for tools that analyze the Go source code style.
In particular, gogrep tool could benefit from that.
(It also looks like
go/printer
has the same difficulties, this is why we get identical printed forms above.)I'm also aware that after
gofmt
botha
andb
become identical; it's not always desirable to reformat the code being processed.Another gogrep-related thing.
for $*_ {}
could read as "any for loop" whilefor ; $*_; {}
should probably match only non-range loops. Right now it's hard to produce a different pattern for these as both of them generate identical AST.What did you expect to see?
A way to distinguish between 2 syntax forms.
What did you see instead?
No way to distinguish between 2 syntax forms.
The text was updated successfully, but these errors were encountered: