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: Slice access not being formatted to reflect operator precedence #60594

Open
richgrov opened this issue Jun 4, 2023 · 2 comments
Open
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@richgrov
Copy link

richgrov commented Jun 4, 2023

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

$ go version
go version go1.20.4 linux/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="~/.cache/go-build"
GOENV="~/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="~/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="~/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="~/Desktop/oneworld/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build41908708=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://go.dev/play/p/7EnkFFdEfKt

gofmt (or clicking the "Format" button on the link) will insert spaces between operators to indicate their precedence (line 7). However, this does not apply to a slice accessor (line 10).

This can make code hard to read in more complex operations, such as converting cartesian coordinates to an index (example below.)

What did you expect to see?

func (arr *Array3d) getByte(x, y, z int) byte {
  return arr.data[x*arr.width*arr.height + z*arr.height + y)
}

What did you see instead?

func (arr *Array3d) getByte(x, y, z int) byte {
  return arr.data[x*arr.width*arr.height+z*arr.height+y]
}
@dr2chase
Copy link
Contributor

dr2chase commented Jun 5, 2023

@griesemer @mvdan

@dr2chase dr2chase added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 5, 2023
@griesemer
Copy link
Contributor

The precedence emphasis depends on nesting level. It's not just slices, it also happens in array indexing, and I think a few other cases. I agree it's not very consistent and has been brought up before. It's also trivial to change.

But changing this would cause a huge amount of churn millions of .go files so we have refrained from doing so in the past.
Putting on hold for now.

cc: @rsc who originally wrote the emphasis algorithm.

@griesemer griesemer added NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jun 5, 2023
@griesemer griesemer added this to the Backlog milestone Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

3 participants