Skip to content

Rust Reference documents operator associativity incorrectly #34386

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

Closed
jameysharp opened this issue Jun 20, 2016 · 6 comments
Closed

Rust Reference documents operator associativity incorrectly #34386

jameysharp opened this issue Jun 20, 2016 · 6 comments
Labels
T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@jameysharp
Copy link
Contributor

In section 7.2.12.8 "Operator precedence", the Rust reference says, "Operators at the same precedence level are evaluated left-to-right." I assume this is intended to mean that the binary operators are all left-associative.

However, the comparison operators (== != < > <= >=) are apparently non-associative, requiring explicit parentheses if you want to apply one to the result of another, as in (2 < 3) == true. (That's a silly example, but I have in the past found utility in expressions like (a < b) != (c < d).)

I think the implemented behavior is both reasonable and a good idea, so this is just a documentation bug, but I'm not really sure how to fix the documentation to be clear.

@steveklabnik
Copy link
Member

/cc @rust-lang/lang , is this intentional? if so, I will fix the docs

@eddyb
Copy link
Member

eddyb commented Jun 20, 2016

Yes, it is. I believe it has to do with how a < b < c, even if it parsed, wouldn't be a < b && b < c like in some other languages (Python?) and would produce confusing type error messages in most cases.

@durka
Copy link
Contributor

durka commented Jun 21, 2016

It actually does parse, even after the chained comparison error, and produces the expected type error :)

@Aatch
Copy link
Contributor

Aatch commented Jun 22, 2016

@steveklabnik it's complicated, as the parser does technically parse a == b == c == d as ((a == b) == c) == d (I think, this is a based on the error messages I got). I think the fact that chained comparisons throws an error means we should probably document them as non-associative though.

Chances are that section was written before the chained comparisons error was implemented.

@durka
Copy link
Contributor

durka commented Jun 22, 2016

And those error messages should be suppressed.

@steveklabnik steveklabnik added T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed A-lang labels Mar 24, 2017
@Mark-Simulacrum
Copy link
Member

This appears to be a documentation issue, closing in favor of an issue on the reference: https://github.com/rust-lang-nursery/reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants