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 dereferencing on E0277 if appropriate #67571

Closed
estebank opened this issue Dec 23, 2019 · 3 comments
Closed

Suggest dereferencing on E0277 if appropriate #67571

estebank opened this issue Dec 23, 2019 · 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. 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

@estebank
Copy link
Contributor

error[E0277]: the trait bound `syntax_pos::MultiSpan: std::convert::From<&syntax_pos::span_encoding::Span>` is not satisfied
   --> src/librustc_typeck/astconv.rs:726:42
    |
726 | ...                   tcx.sess.delay_span_bug(visitor.0.iter().next().unwrap(), "asdf");
    |                                ^^^^^^^^^^^^^^ the trait `std::convert::From<&syntax_pos::span_encoding::Span>` is not implemented for `syntax_pos::MultiSpan`
    |
    = help: the following implementations were found:
              <syntax_pos::MultiSpan as std::convert::From<std::vec::Vec<syntax_pos::span_encoding::Span>>>
              <syntax_pos::MultiSpan as std::convert::From<syntax_pos::span_encoding::Span>>
    = note: required because of the requirements on the impl of `std::convert::Into<syntax_pos::MultiSpan>` for `&syntax_pos::span_encoding::Span`

Suggest *foo when <&foo as &Foo>, Into<Bar> is expected and Bar: From<Foo> is implemented.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. labels Dec 23, 2019
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Dec 24, 2019
@drmason13
Copy link

I would have benefited from this, related forum post: https://users.rust-lang.org/t/methods-that-take-references-to-type-parameter/36088

When writing a method taking &T and comparing to a field T the compiler complains

error[E0277]: can't compare `&T` with `T`
...
help: the trait `std::cmp::PartialEq<T>` is not implemented for `&T`

The suggestion from rustc is:

   = help: consider adding a `where &T: std::cmp::PartialEq<T>` bound

But fellow rustaceans suggested:

do data == &self.data because if you have T: PartialEq<T>, then you will get &T: PartialEq<&T> for free

If the compiler could suggest the latter instead it would be a great help :)

@kornelski
Copy link
Contributor

Referencing would help too:

fn main() {
    takes_ref(Default::default());
}
fn takes_ref(_: &i32) {}
2 |     takes_ref(Default::default());
  |               ^^^^^^^^^^^^^^^^^^ the trait `std::default::Default` is not implemented for `&i32`
  |
  = help: the following implementations were found:
            <i32 as std::default::Default>
  = note: required by `std::default::Default::default`

@estebank
Copy link
Contributor Author

estebank commented Nov 17, 2023

Current output:

error[E0382]: borrow of moved value: `bar`
  --> src/main.rs:24:27
   |
21 |     let bar = String::from("XYZ");
   |         --- move occurs because `bar` has type `String`, which does not implement the `Copy` trait
22 |
23 |     if foo.matches(bar) {
   |                    --- value moved here
24 |          println!("{:?}", bar);
   |                           ^^^ value borrowed here after move
   |
note: consider changing this parameter type in method `matches` to borrow instead if owning the value isn't necessary
  --> src/main.rs:14:29
   |
14 |     fn matches(&self, data: T) -> bool {
   |        -------              ^ this parameter takes ownership of the value
   |        |
   |        in this method
   = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider cloning the value if the performance cost is acceptable
   |
23 |     if foo.matches(bar.clone()) {
   |                       ++++++++

The above "solves" the issue if you apply the suggestion, but if you follow the earlier advice you get the original suggestion:

error[E0277]: can't compare `&T` with `T`
  --> src/main.rs:15:14
   |
15 |         data == self.data
   |              ^^ no implementation for `&T == T`
   |
   = help: the trait `PartialEq<T>` is not implemented for `&T`
help: consider extending the `where` clause, but there might be an alternative better way to express this requirement
   |
8  |     where T: PartialEq, &T: PartialEq<T>
   |                       ~~~~~~~~~~~~~~~~~~

I believe that issue falls under #40660, which we should address.

error[E0277]: the trait bound `&i32: Default` is not satisfied
 --> src/main.rs:2:15
  |
2 |     takes_ref(Default::default());
  |               ^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `&i32`
  |
help: consider borrowing here
  |
2 |     takes_ref(&Default::default());
  |               +
error[E0308]: mismatched types
 --> src/main.rs:2:15
  |
2 |     takes_ref(&Default::default());
  |     --------- ^^^^^^^^^^^^^^^^^^^ expected `i32`, found `&_`
  |     |
  |     arguments to this function are incorrect
  |
  = note:   expected type `i32`
          found reference `&_`
note: function defined here
 --> src/main.rs:4:4
  |
4 | fn takes_ref(_: i32) {}
  |    ^^^^^^^^^ ------
help: consider removing the borrow
  |
2 -     takes_ref(&Default::default());
2 +     takes_ref(Default::default());

I believe we can close this ticket.

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