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

Clarify precedence with ^ in chained comparison #59828

Open
varkor opened this issue Apr 9, 2019 · 7 comments
Open

Clarify precedence with ^ in chained comparison #59828

varkor opened this issue Apr 9, 2019 · 7 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@varkor
Copy link
Member

varkor commented Apr 9, 2019

fn foo(x: i32, y: i32, z: i32) -> bool {
    x < y ^ y < z // error: chained comparison operators require parentheses
}

Actual output:

error: chained comparison operators require parentheses
 --> src/lib.rs:2:7
  |
2 |     x < y ^ y < z
  |       ^^^^^^^^^^^
  |
  = help: use `::<...>` instead of `<...>` if you meant to specify type arguments
  = help: or use `(...)` if you meant to specify fn arguments

error[E0308]: mismatched types
 --> src/lib.rs:2:17
  |
2 |     x < y ^ y < z
  |                 ^ expected bool, found i32

error: aborting due to 2 previous errors

It would be helpful to clarify that ^ has a higher precedence than < and suggest that the user parenthesises (x < y) ^ (y < z), which would result in the correct expected type.

@varkor varkor added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. labels Apr 9, 2019
@estebank estebank added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 21, 2020
@estebank
Copy link
Contributor

Current output:

error: comparison operators cannot be chained
 --> src/lib.rs:2:7
  |
2 |     x < y ^ y < z // error: chained comparison operators require parentheses
  |       ^^^^^^^^^
  |
  = help: use `::<...>` instead of `<...>` to specify type arguments
  = help: or use `(...)` if you meant to specify fn arguments
help: split the comparison into two...
  |
2 |     x < y ^ y && y ^ y < z // error: chained comparison operators require parentheses
  |     ^^^^^^^^^^^^^^^^^^^^
help: ...or parenthesize one of the comparisons
  |
2 |     (x < y ^ y) < z // error: chained comparison operators require parentheses
  |     ^^^^^^^^^^^^^

@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@pierwill
Copy link
Member

@rustbot claim

@pierwill
Copy link
Member

pierwill commented Oct 20, 2021

Current output:

   Compiling playground v0.0.1 (/playground)
error: comparison operators cannot be chained
 --> src/lib.rs:2:7
  |
2 |     x < y ^ y < z
  |       ^       ^
  |
  = help: use `::<...>` instead of `<...>` to specify type or const arguments
  = help: or use `(...)` if you meant to specify fn arguments
help: split the comparison into two
  |
2 |     x < y ^ y && y ^ y < z
  |               ++++++++

error: could not compile `playground` due to previous error

We've lost the nice parentheses suggestion. 🤔

@pierwill
Copy link
Member

pierwill commented Oct 21, 2021

Are there any other operators in place of ^ that would make x < y ^ y < z valid if the suggestion were to parenthesize both outer pairs, like this (x < y) ^ (y < z)?

If not, I think x < y ^ y < z could be added as its own case in attempt_chained_comparison_suggestion. Unless I'm misunderstanding the code, or there's something general I'm missing.

@estebank
Copy link
Contributor

I guess | and & would be the only other ones.

@pierwill
Copy link
Member

pierwill commented Nov 3, 2021

@rustbot release-assignment

@estebank estebank removed D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants