Get Rust to infer trait impl from where bound?

I'm trying to write code like this:

trait Trait {
    fn method(&self);
}

struct Struct<T>(T);

impl<T: Trait> Trait for Struct<T> {
    fn method(&self) {
        self.0.method();
    }
}

trait OtherTrait {
    fn other_method(&self) where Self: Trait;
}

impl<T> OtherTrait for Struct<T> {
    fn other_method(&self) where Self: Trait {
        self.0.method()
    }
}

The last line (self.0.method()) fails to compile:

error[E0599]: no method named `method` found for type parameter `T` in the current scope
  --> src/lib.rs:19:16
   |
17 | impl<T> OtherTrait for Struct<T> {
   |      - method `method` not found for this type parameter
18 |     fn other_method(&self) where Self: Trait {
19 |         self.0.method()
   |                ^^^^^^ method not found in `T`
   |
   = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `method`, perhaps you need to restrict type parameter `T` with it:
   |
17 | impl<T: Trait> OtherTrait for Struct<T> {
   |       +++++++

We know that, this context, T: Trait. In particular, Struct<T>: Trait, and the only way that can happen is for T: Trait thanks to the bound on impl<T: Trait> Trait for Struct<T>. Is there any way to get Rust to realize this?

There's not a way to get Rust to infer the bound due to only being one implementation.

It can also happen if you add an implementation (assuming you are able to, e.g. that the trait and type are both local like in the reduction).

// For example
//                    vv or some other type not covered by the blanket impl
impl Trait for Struct<()> {
    fn method(&self) {}
}
2 Likes

Yeah, I suppose that'd be a pretty big stability hazard as you describe. Oh well.