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

cmd/gofmt: rewrite 'a -> a' transforms 'default:' into 'case :' #33103

Closed
rillig opened this issue Jul 14, 2019 · 5 comments
Closed

cmd/gofmt: rewrite 'a -> a' transforms 'default:' into 'case :' #33103

rillig opened this issue Jul 14, 2019 · 5 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@rillig
Copy link
Contributor

rillig commented Jul 14, 2019

What version of Go are you using (go version)?

go version go1.13beta1 windows/amd64

Does this issue reproduce with the latest release?

yes

What did you do?

package main

func main() {
	func() {
		switch {
		default: // This is changed to `case :`.
		}
	}()
}
gofmt -r 'a->a' -d .

What did you expect to see?

After rewriting, the code still compiles.

What did you see instead?

The default: is rewritten to case :, which is syntactically invalid.
This only happens inside a function literal, but not in main itself.

@dmitshur
Copy link
Contributor

Thanks for the report.

I checked, this affects Go 1.12.7 as well as Go 1.10.5, so it's not a new regression in Go 1.13 beta 1.

Given this and the other 2 issues you reported (#33104, #33105), it seems there are some bugs in the rewrite rules when doing an identity replacement (a -> a). This issue doesn't come up when doing something else like (a) -> a, but that's because default: doesn't match (a).

@rillig Have you seen this issue come up in more practical replace rules other than the no-op a -> a replacement, or are you reporting this just because it's a valid problem you discovered even though it's unlikely to come up in real-world usage?

I checked https://goissues.org/cmd/gofmt and it doesn't seem that this was reported in the past, so thanks again.

/cc @griesemer

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 15, 2019
@dmitshur dmitshur added this to the Go1.14 milestone Jul 15, 2019
@rillig
Copy link
Contributor Author

rillig commented Jul 15, 2019

@dmitshur The reason I tried out the a->a transformation was that gofmt is known to sometimes discard comments and add additional empty lines, which makes it an unreliable tool that needs manual inspection afterwards. I experienced this myself when I did a large code transformation.

To make sure that gofmt doesn't damage my code, my idea was to first try the transformation by essentially doing nothing, before actually transforming something. So instead of running gofmt -r 'call(a, b, c) -> call(b)' directly, I would first run gofmt -r 'call(a, b, c) -> call(a, b, c)' to see whether anything is damaged.

The simplest such call is gofmt -r 'a->a', therefore I used that for playing around with gofmt. And since that command found so many inconsistencies, I thought I'd better report them immediately instead of letting gofmt break a whole lot of real life code.

@gopherbot
Copy link

Change https://golang.org/cl/187879 mentions this issue: cmd/gofmt: fix a->a rewrite rule for switch with default only

@gopherbot
Copy link

Change https://golang.org/cl/187880 mentions this issue: cmd/gofmt: fix a->a rewrite rule for switch with default only

@gopherbot
Copy link

Change https://golang.org/cl/187917 mentions this issue: cmd/gofmt: don't turn nil slices into empty slices during rewriting

@golang golang locked and limited conversation to collaborators Sep 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants