Why explicit bound is needed for associated type in the following code?

trait MatrixTrait {
    fn some(&self);
}
trait MutMatrixTrait: MatrixTrait {
    fn other(&self);
}

pub trait MatrixWrapper {
    type Matrix: MatrixTrait;
    fn matrix(&self) -> &Self::Matrix;
}
pub trait MutMatrixWrapper: MatrixWrapper
    where Self::Matrix: MutMatrixTrait
{
    fn matrix_mut(&mut self) -> &mut Self::Matrix;
}

pub trait MatrixTransposer: MatrixWrapper {}

//error: the trait bound `<Self as MatrixWrapper>::Matrix: MutMatrixTrait` is not satisfied
pub trait MutMatrixTransposer: MatrixTransposer + MutMatrixWrapper {}

(playground)
Why does Rust understand that MatrixWrapper::Matrix: MatrixTrait in the MatrixTransposer-case, but it does not understand that MatrixWrapper::Matrix: MutMatrixTrait in the MutMatrixTransposer-case?

Minimal repro:

pub trait B {}

pub trait Wraps {
    type U;
}

pub trait WrapsB: Wraps
where
    Self::U: B,
{
}

// 'error[E0277]: the trait bound `<Self as Wraps>::U: B` is not satisfied'
pub trait TransposeB: WrapsB {}

(playground)

Looks like an RFC to incorporate where bounds like this in type inference was accepted but not (yet) implemented: tracking issue. So help is on the way but for now you have to just copy the where clause.

1 Like

Related issue rust-lang/rust#20671

As a (unstable) workaround you can use trait aliases since they imply all the bounds, not just supertraits and bounds on direct associated items

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.