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 indentation in chained calls #41889

Open
cbandy opened this issue Oct 9, 2020 · 3 comments
Open

cmd/gofmt: unexpected indentation in chained calls #41889

cbandy opened this issue Oct 9, 2020 · 3 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@cbandy
Copy link

cbandy commented Oct 9, 2020

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

$ go version
go version go1.15.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/cbandy/Library/Caches/go-build"
GOENV="/Users/cbandy/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/cbandy/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/cbandy/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/cbandy/.local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/cbandy/.local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/bw/_dxdzsmx6h1ffcgp6rvfh55c0000gn/T/go-build600613638=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Ran gofmt -s -w on the following code: https://play.golang.org/p/CXEb-91Y6Mm

package main

func f(a interface{}) func(b interface{}) {
	return func(interface{}) {}
}

type s struct{}

func (s) m(a interface{}) func(b interface{}) {
	return func(interface{}) {}
}

func main() {
	f("a")("b")
	f("a")(
		"b",
	)
	f("a")([]string{
		"b",
	})
	f(
		"a",
	)(
		"b",
	)

	s{}.
		m("a")("b")
	s{}.
		m("a")(
			"b",
		)
	s{}.
		m(
			"a",
		)(
			"b",
		)
	s{}.
		m("a")([]string{
			"b",
		})
}

What did you expect to see?

(I expected no changes to the indentation because) I expected closing brackets to be indented the same amount as the line of their matching open brackets.

What did you see instead?

On some of the chained method calls, the closing brackets are indented one level less. Starting on L29, the output is:

	s{}.
		m("a")(
		"b",
	)
	s{}.
		m(
			"a",
		)(
		"b",
	)
	s{}.
		m("a")([]string{
		"b",
	})
--- main.go.orig
+++ main.go
@@ -28,16 +28,16 @@
 		m("a")("b")
 	s{}.
 		m("a")(
-			"b",
-		)
+		"b",
+	)
 	s{}.
 		m(
 			"a",
 		)(
-			"b",
-		)
+		"b",
+	)
 	s{}.
 		m("a")([]string{
-			"b",
-		})
+		"b",
+	})
 }
@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 13, 2020
@dmitshur dmitshur changed the title cmd/gofmt: Unexpected indentation in chained calls cmd/gofmt: unexpected indentation in chained calls Oct 13, 2020
@dmitshur dmitshur added this to the Backlog milestone Oct 13, 2020
@dmitshur
Copy link
Contributor

CC @griesemer.

@cbandy
Copy link
Author

cbandy commented Oct 13, 2020

Sorry for the contrived example above. For context, I encountered this while using a type that has ... accumulating/builder-style methods: https://play.golang.org/p/GVl1MdZEeh3

	result, err := pkg.NewResolver().
		Add("x", "y")(1, 2).
		Add("z")(3).
		Add("n")([]string{
			"q",
			"r",
		}).
		Resolve()

gofmt indents as described:

 		Add("x", "y")(1, 2).
 		Add("z")(3).
 		Add("n")([]string{
-			"q",
-			"r",
-		}).
+		"q",
+		"r",
+	}).
 		Resolve()
 }

@griesemer
Copy link
Contributor

Thanks. I probably won't be able to get to this anytime soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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