Skip to content

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

Closed
@pm215

Description

@pm215

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions