Super trait associated type bounds - confusing error

trait MyInterface {
    fn something(&self);
}

trait MyIterator: Iterator
where
    Self::Item: MyInterface
{}

impl<T: Iterator> MyIterator for T
where
    T::Item: MyInterface
{}

fn iterate<T: MyIterator>(mut i: T) {
    while let Some(x) = i.next() {
        x.something();
    }
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `<T as std::iter::Iterator>::Item: MyInterface` is not satisfied
  --> src/lib.rs:15:15
   |
5  | trait MyIterator: Iterator
   |       ---------- required by a bound in this
6  | where
7  |     Self::Item: MyInterface
   |                 ----------- required by this bound in `MyIterator`
...
15 | fn iterate<T: MyIterator>(mut i: T) {
   |               ^^^^^^^^^^           - help: consider further restricting the associated type: `where <T as std::iter::Iterator>::Item: MyInterface`
   |               |
   |               the trait `MyInterface` is not implemented for `<T as std::iter::Iterator>::Item`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

I'm trying to find a workaround for the lack of associated type bounds by doing something similar to the code above, and surprisingly the first two blocks compile, while the last one fn iterate does not. I can fix it by also adding the where T::Item: MyInterface clause to the function, but I have to repeat this in every place I use the MyIterator trait. Why is this? Shouldn't the T: MyInterator bound be enough to "vet" any T as a fully-valid MyIterator implementation?

I believe you've run into https://github.com/rust-lang/rust/issues/24159 - that issue has more details on the nature of the problem / bug / lack of feature in rust, as well as the status.

1 Like

Yeah that looks to be the case. Any estimate on when chalk will be turned on the compiler?

I don't think there's a timeline yet, but I also don't expect it to happen anytime too soon.

Here's the tracking issue, it has some notes: https://github.com/rust-lang/rust/issues/48049

In addition, the current chalk integration is available with the -Z chalk rustc flag, if you want to try it out. I think most code fails to compile, which kind of reflects where chalk is right now.

That isn't to say work isn't being done - chalk is essentially the whole focus of the traits working group, and there are a number of people working on getting it working & integrated. It's just that it's a large project, and getting it fully working is non-trivially.

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.