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

unhelpful diagnostic: trying to compare f64 and &f64 for equality gives E0277 but E0308 would be better #45109

Closed
pm215 opened this issue Oct 8, 2017 · 3 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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pm215
Copy link

pm215 commented Oct 8, 2017

If I try to compare an f64 with an &f64:

        let bookcount = user.countmap.get("Book").unwrap();
        assert!(bookcount < 91.0);

(where user.countmap is a HashMap<String, f64>) then rust tells me

error[E0308]: mismatched types
   --> src/main.rs:134:29
    |
134 |         assert!(bookcount < 91.0);
    |                             ^^^^ expected &f64, found floating-point variable
    |
    = note: expected type `&f64`
               found type `{float}`
    = help: try with `&91.0`

helpfully pointing me to the fact I need to add an & to the constant.

However if I do the same thing for equality comparison:

assert(bookcount == 91.0);

the error is much more obscure:

error[E0277]: the trait bound `&f64: std::cmp::PartialEq<{float}>` is not satisfied
   --> src/main.rs:134:27
    |
134 |         assert!(bookcount == 91.0);
    |                           ^^ can't compare `&f64` with `{float}`
    |
    = help: the trait `std::cmp::PartialEq<{float}>` is not implemented for `&f64`

It would be helpful if rust could produce E0308 for this mistake in all kinds of comparison.

I'm using this version of rust:

mnementh$ rustc --version --verbose
rustc 1.20.0 (f3d6973f4 2017-08-27)
binary: rustc
commit-hash: f3d6973f41a7d1fb83029c9c0ceaf0f5d4fd7208
commit-date: 2017-08-27
host: x86_64-unknown-linux-gnu
release: 1.20.0
LLVM version: 4.0
@TimNN TimNN added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Oct 10, 2017
@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 26, 2019
@estebank
Copy link
Contributor

Closing as duplicate of #45109.

@pm215
Copy link
Author

pm215 commented Sep 26, 2019

Am I confused, or did you just close this bug as a duplicate of itself? I'm guessing you either meant to close some other bug, or to mark this a dup of some other bug...

@estebank
Copy link
Contributor

Indeed I meant to reference #40660.

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. 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

3 participants