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

Suggest derefence operator when mismatched types related to reference #39942

Closed
frewsxcv opened this issue Feb 18, 2017 · 4 comments
Closed

Suggest derefence operator when mismatched types related to reference #39942

frewsxcv opened this issue Feb 18, 2017 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

@frewsxcv
Copy link
Member

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

@frewsxcv frewsxcv added the A-diagnostics Area: Messages for errors, warnings, and lints label Feb 18, 2017
@frewsxcv
Copy link
Member Author

cc @oconnor663

@oconnor663
Copy link
Contributor

This is especially tricky when you're dealing with iterators or other container methods that add extra &'s onto your types, and you've made an &&T without realizing it. The error message doesn't include an && anywhere (I think), and it becomes very hard to figure out what's wrong.

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
bors added a commit that referenced this issue Apr 10, 2017
Explicit help message for binop type mismatch

When trying to do `1 + Some(2)`, or some other binary operation on two
types different types without an appropriate trait implementation, provide
an explicit help message:

```rust
help: `{integer} + std::option::Option<{integer}>` has no implementation
```

Re: #39579, #38564, #37626, #39942, #34698.
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank
Copy link
Contributor

Current output:

error[E0277]: can't compare `&{integer}` with `{integer}`
 --> src/main.rs:6:10
  |
6 |     if a == b {
  |          ^^ no implementation for `&{integer} == {integer}`
  |
  = help: the trait `std::cmp::PartialEq<{integer}>` is not implemented for `&{integer}`

I don't think we'll get to suggesting dereferencing for these cases in the short term.

@estebank
Copy link
Contributor

Closing as duplicate of #45109 and #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 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

5 participants