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: Unexpected formatting with one line functions/methods. #19544

Closed
nsf opened this issue Mar 14, 2017 · 4 comments
Closed

cmd/gofmt: Unexpected formatting with one line functions/methods. #19544

nsf opened this issue Mar 14, 2017 · 4 comments

Comments

@nsf
Copy link

nsf commented Mar 14, 2017

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

go version go1.8 linux/amd64

What operating system and processor architecture are you using (go env)?

Doesn't matter.

What did you do?

gofmt the following code:

package main

func OneLineFunc() {}
func FuncWithVeryLongName() { // comment
}

func main() {
}

What did you expect to see?

package main

func OneLineFunc() {}
func FuncWithVeryLongName() { // comment
}

func main() {
}

What did you see instead?

package main

func OneLineFunc()            {}
func FuncWithVeryLongName() { // comment
}

func main() {
}

Discovered this case by accident. It looks a bit unexpected to me. Whether it can be considered as a bug or not is obviously up to you. I don't write code like that, so it's not a critical issue for me, but I thought it's worth reporting.

In case you didn't notice the problem. Braces of the one-line function are weirdly aligned with the comment on the next line. Or if the first function is longer than the second one, comment section will be weirdly aligned with braces.

@bradfitz bradfitz changed the title gofmt: Unexpected formatting with one line functions/methods. cmd/gofmt: Unexpected formatting with one line functions/methods. Mar 14, 2017
@griesemer
Copy link
Contributor

I agree this is a bit odd. Not urgent but we should probably address this. Thanks for the report.

@griesemer griesemer added this to the Go1.9Maybe milestone Mar 14, 2017
@fraenkel
Copy link
Contributor

Seems to be caused by

https://github.com/golang/go/blob/master/src/go/printer/nodes.go#L1626
p.funcBody(p.distanceFrom(d.Pos()), vtab, d.Body)

If the vtab is replaced with blank, all is well. But was there a reason?

@griesemer
Copy link
Contributor

The vtab controls the column layout. We want one here because if we have multiple one-line functions w/o empty lines between them, we want the function bodies to align (at least that's what they are doing now).

The assumption was that there would be an empty line before and after any multi-line function, and then the issue you're seeing doesn't occur. (That's also the work-around in this case).

@gopherbot
Copy link

CL https://golang.org/cl/46002 mentions this issue.

@golang golang locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants