Trait bounds on associated type projections via HRTB are broken · Issue #56556 · rust-lang/rust (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

@sfackler

Description

@sfackler

I believe this should successfully compile, but it currently fails claiming that vec's iterator isn't ExactSizeIterator:

fn foo(t: T) -> usize where for<'a> &'a T: IntoIterator, for<'a> <&'a T as IntoIterator>::IntoIter: ExactSizeIterator, { t.into_iter().len() }

fn main() { foo::<Vec>(vec![]); }

error[E0277]: the trait bound `for<'a> <&'a std::vec::Vec<u32> as std::iter::IntoIterator>::IntoIter: std::iter::ExactSizeIterator` is not satisfied
  --> src/main.rs:10:5
   |
10 |     foo::<Vec<u32>>(vec![]);
   |     ^^^^^^^^^^^^^^^ the trait `for<'a> std::iter::ExactSizeIterator` is not implemented for `<&'a std::vec::Vec<u32> as std::iter::IntoIterator>::IntoIter`
   |
   = help: the following implementations were found:
             <&'a mut I as std::iter::ExactSizeIterator>
note: required by `foo`
  --> src/main.rs:1:1
   |
1  | / fn foo<T>(t: T) -> usize
2  | | where
3  | |     for<'a> &'a T: IntoIterator,
4  | |     for<'a> <&'a T as IntoIterator>::IntoIter: ExactSizeIterator,
5  | | {
6  | |     t.into_iter().len()
7  | | }
   | |_^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.

Things compile correctly if the bounds are changed to T rather than for<'a> &'a T.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=c96139308ad1602c281e71c4c54c73ec