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: inconsistent formatting of empty methods #28082

Open
neild opened this Issue Oct 8, 2018 · 3 comments

Comments

Projects
None yet
4 participants
@neild
Contributor

neild commented Oct 8, 2018

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

go version go1.11rc2 linux/amd64

Does this issue reproduce with the latest release?

yes

What did you do?

Format https://play.golang.org/p/VW-uvEb5Mro

package main

func main(                                                                                                      ) {}

What did you expect to see?

https://play.golang.org/p/34L1Aw0cZ1D

package main

func main() {}

What did you see instead?

package main

func main() {
}

Looks like gofmt is deciding whether to turn {} into {\n} based on the original length of the line, rather than the post-formatting length. This is harmless, but surprising.

@agnivade

This comment has been minimized.

Show comment
Hide comment
@agnivade
Member

agnivade commented Oct 9, 2018

@agnivade agnivade added this to the Unplanned milestone Oct 9, 2018

@dmitshur

This comment has been minimized.

Show comment
Hide comment
@dmitshur

dmitshur Oct 9, 2018

Member

An interesting behavior.

For anyone confused why it formats instead of printing an error like main.go:3:12: expected type, found 'EOF', scroll to the right. The ) {} characters are just off-screen.

Member

dmitshur commented Oct 9, 2018

An interesting behavior.

For anyone confused why it formats instead of printing an error like main.go:3:12: expected type, found 'EOF', scroll to the right. The ) {} characters are just off-screen.

@griesemer

This comment has been minimized.

Show comment
Hide comment
@griesemer

griesemer Oct 9, 2018

Contributor

This patch solves this specific issue:

diff --git a/src/go/printer/nodes.go b/src/go/printer/nodes.go
index 1de7cd81b2..38b50f2596 100644
--- a/src/go/printer/nodes.go
+++ b/src/go/printer/nodes.go
@@ -1693,7 +1693,9 @@ func (p *printer) funcBody(headerSize int, sep whiteSpace, b *ast.BlockStmt) {
 func (p *printer) distanceFrom(from token.Pos) int {
        if from.IsValid() && p.pos.IsValid() {
                if f := p.posFor(from); f.Line == p.pos.Line {
-                       return p.pos.Column - f.Column
+                       // Use actual (formatted, i.e., p.out) column information
+                       // to get an accurate column distance. See issue #28082.
+                       return p.out.Column - f.Column
                }
        }
        return infinity

But it leads to cmd/gofmt test failures. Haven't investigated. Not urgent. If somebody wants to analyze this a bit more, please go ahead and keep me in the loop, thanks.

Contributor

griesemer commented Oct 9, 2018

This patch solves this specific issue:

diff --git a/src/go/printer/nodes.go b/src/go/printer/nodes.go
index 1de7cd81b2..38b50f2596 100644
--- a/src/go/printer/nodes.go
+++ b/src/go/printer/nodes.go
@@ -1693,7 +1693,9 @@ func (p *printer) funcBody(headerSize int, sep whiteSpace, b *ast.BlockStmt) {
 func (p *printer) distanceFrom(from token.Pos) int {
        if from.IsValid() && p.pos.IsValid() {
                if f := p.posFor(from); f.Line == p.pos.Line {
-                       return p.pos.Column - f.Column
+                       // Use actual (formatted, i.e., p.out) column information
+                       // to get an accurate column distance. See issue #28082.
+                       return p.out.Column - f.Column
                }
        }
        return infinity

But it leads to cmd/gofmt test failures. Haven't investigated. Not urgent. If somebody wants to analyze this a bit more, please go ahead and keep me in the loop, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment