You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What kind of rewrite were you actually trying to make here? Encountering a panic is a bug for sure, but as far as I can tell, this rewrite should generally result in an error anyway.
Naively, your code could become something like:
package p.main
var p._ p.int
That's not valid syntax, and it's what's causing the panic: names can't be arbitrary expressions like selectors. Perhaps we could teach gofmt's rewrite about that, so that it would error in those cases.
An alternative is to teach it to not apply the rewrite in nodes where it would not be possible, resulting in:
package main
var _ p.int
That wouldn't lead to invalid syntax, and could arguably be useful for some cases, but it could also make the rewrite feature less consistent. I'm not sure which of the two approaches gofmt -r is designed for. cc @griesemer
mvdan
changed the title
gofmt panic from simple rewrite rule
cmd/gofmt: panic from simple rewrite rule
Apr 5, 2022
Yeah, this is just stripped down example that reproduces the panic. I
don't remember the original context (months ago), but it was a more
complex rewrite rule on the elements of a composite literal. My rewrite
rule was probably wrong there, too, but I never figured out why due to the
panic and corresponding lack of feedback. I only saved off this example
for future investigation/reporting.
The -r rewrite feature of gofmt is here for historic reasons; now we would probably rather have a stand-alone tool for rewriting. For the record, @rsc had the idea and added the code of the rewriter to gofmt.
In this case I'd argue that if the rewrite is not possible because an *ast.Ident cannot be replaced with an ast.Expr, it probably should not crash or error, but just ignore those cases. Clearly a crash is not desirable, and an error is not helpful because there wouldn't be a way to avoid it (at least in this case, though I have not looked into the code).
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?
What did you expect to see?
No panic.
What did you see instead?
The text was updated successfully, but these errors were encountered: