Skip to content

Suggest derefence operator when mismatched types related to reference #39942

Closed
@frewsxcv

Description

@frewsxcv

Playpen

Code:

fn main() {
    let a = &1;
    let b = 1;
    if a == b {
        println!("equal");
    } else {
        println!("not equal");
    }
}

Error:

error[E0277]: the trait bound `&{integer}: std::cmp::PartialEq<{integer}>` is not satisfied
 --> <anon>:4:8
  |
4 |     if a == b {
  |        ^^^^^^ the trait `std::cmp::PartialEq<{integer}>` is not implemented for `&{integer}`
  |
  = help: the following implementations were found:
  = help:   <&'a A as std::cmp::PartialEq<&'b B>>
  = help:   <&'a mut A as std::cmp::PartialEq<&'b mut B>>
  = help:   <&'a A as std::cmp::PartialEq<&'b mut B>>
  = help:   <&'a mut A as std::cmp::PartialEq<&'b B>>
  = help: and 586 others

error: aborting due to previous error

This error message is somewhat intimidating. Assuming the user knows to add a * or &, which side do they add it to? It might be not too difficult with this example to deduce that, but when dealing with double nested references and other shenanigans, it'd be a lot easier if we just suggested where to put the * and &.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-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