Skip to content

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

Closed
@nagisa

Description

@nagisa
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.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions