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

rustc suggests wrapping receivers even if it wouldn't immediately help method resolution #94218

Closed
nagisa opened this issue Feb 21, 2022 · 1 comment · Fixed by #94237
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@nagisa
Copy link
Member

nagisa commented Feb 21, 2022

mod banana {
    pub struct Chaenomeles;
    
    pub trait Apple {
        fn pick(&self) {}
    }
    impl Apple for Chaenomeles {}

    pub trait Peach {
        fn pick(&self, a: &mut ()) {}
    }
    impl<Mango: Peach> Peach for Box<Mango> {}
    impl Peach for Chaenomeles {}
}

fn main() {
    banana::Chaenomeles.pick()
}

playground

Produces the output as such:

error[[E0599]](https://doc.rust-lang.org/stable/error-index.html#E0599): no method named `pick` found for struct `Chaenomeles` in the current scope
  [--> src/main.rs:17:25
](https://play.rust-lang.org/#)   |
2  |     pub struct Chaenomeles;
   |     ----------------------- method `pick` not found for this
...
10 |         fn pick(&self, a: &mut ()) {}
   |            ---- the method is available for `Box<Chaenomeles>` here
...
17 |     banana::Chaenomeles.pick()
   |                         ^^^^ method not found in `Chaenomeles`
   |
   = help: items from traits can only be used if the trait is in scope
help: consider wrapping the receiver expression with the appropriate type
   |
17 |     Box::new(banana::Chaenomeles).pick()
   |     +++++++++                   +
help: the following traits are implemented but not in scope; perhaps add a `use` for one of them:
   |
1  | use crate::banana::Apple;
   |
1  | use crate::banana::Peach;
   |

Note that the message suggests wrapping the receiver expression in a Box::new even though it won't really help much unless the traits are imported. And if the trait was imported in the first place, the receiver type would've been fine either way.

@nagisa nagisa added the A-diagnostics Area: Messages for errors, warnings, and lints label Feb 21, 2022
@compiler-errors
Copy link
Member

@rustbot claim

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants