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

go/printer: format x&1<<8 as x&1 << 8? #13038

Closed
minux opened this issue Oct 23, 2015 · 4 comments
Closed

go/printer: format x&1<<8 as x&1 << 8? #13038

minux opened this issue Oct 23, 2015 · 4 comments

Comments

@minux
Copy link
Member

minux commented Oct 23, 2015

A recent bug on sqrt (#13013) is caused by missing parenthesis
in condition for for loop:

    for ix&1<<shift == 0 {}

I suspect it might be a common error.
What if we make gofmt formats that to

    for ix&1 << shift == 0 {}

instead?

/cc: @griesemer

@griesemer
Copy link
Contributor

& and << are both multiplicative operators and have the same binding strength. The suggestion implies a different binding for & than for <<. Where do we stop?

/cc: @rsc who wrote the code for the spacing around operators in go/printer.

@rsc
Copy link
Contributor

rsc commented Oct 24, 2015

The expression formatting logic is basically unchanged from day 1 of the public release, and I think we should leave it that way. There are any number of arguments for and against minor changes, but widespread agreement is more important than any single change. As Rob said recently, gofmt particular syntax choices are no one's favorite but gofmt is everyone's favorite.

For this specific case, a goal for the expression formatting was on a particular line, operators with spaces have looser precedence than operators without. If we write this expression as x&1 << 8 that implies something about & vs << that simply isn't true.

By itself, the expression formats as x & 1 << 8. When used inside a larger expression with another precedence, it gets compressed to make clear that the outer expression has different precedence:

x & 1 << 8
x&1<<8 == 0
x&1<<8 | 2   NOT  x&1 << 8 | 2

Note in the last case especially that a special case for & vs << would produce a confusing result with respect to addition-level operators like | or +.

No matter what the formatting is, people will still make the occasional mistake. It's actually working very well. Let's leave it as it is.

@rsc rsc changed the title go/format: format x&1<<8 as x&1 << 8? go/printer: format x&1<<8 as x&1 << 8? Oct 24, 2015
@rsc rsc closed this as completed Oct 24, 2015
@mdempsky
Copy link
Member

@minux What about making cmd/vet warn that "a&b<<c == 0" and "a&b<<c != 0" are probably missing parentheses? They seem very unlikely to occur legitimately.

@minux
Copy link
Member Author

minux commented Oct 24, 2015 via email

@golang golang locked and limited conversation to collaborators Oct 24, 2016
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

5 participants