rust-lang / trait-system-refactor-initiative

The Rustc Trait System Refactor Initiative
21 stars 0 forks source link

`DoubleEndedIterator` implementation causes overflow libcore build #88

Closed compiler-errors closed 7 months ago

compiler-errors commented 7 months ago

Minimized when trying to build core:

#![feature(associated_type_bounds)]

use std::iter::FlatMap;

trait DoubleEndedIterator: Iterator {
    fn next_back(&mut self) -> Option<Self::Item>;
}

impl<I: DoubleEndedIterator, U, F> DoubleEndedIterator for FlatMap<I, U, F>
where
    F: FnMut(I::Item) -> U,
    U: IntoIterator,
    <U as IntoIterator>::IntoIter: DoubleEndedIterator, //~ This line is bad, specifically b/c `DoubleEndedIterator` has the `Iterator` supertrait, I believe.
{
    fn next_back(&mut self) -> Option<U::Item> {
        todo!()
    }
}

I think it's because the solver is having trouble projecting <<U as IntoIterator>::IntoIter as Iterator>::Item into <U as IntoIterator>::Item. It should be possible to do that via this item bound type IntoIter: Iterator<Item = Self::Item>.

compiler-errors commented 7 months ago

Ah, it has to do with the blanket impl I: IntoIterator :- I: Iterator

trait Iterator {
    type Item;

    fn next(&mut self) -> Option<Self::Item>;
}

trait IntoIterator {
    type IntoIter: Iterator<Item = Self::Item>;
    type Item;
}

/* Uncomment this to make it fail:
impl<I: Iterator> IntoIterator for I {
    type Item = I::Item;
    type IntoIter = I;
}
*/

trait DoubleEndedIterator: Iterator {
    fn next_back(&mut self) -> Option<Self::Item>;
}

struct Foo<U>(U);

impl<U> Iterator for Foo<U> where U: IntoIterator {
    type Item = U::Item;

    fn next(&mut self) -> Option<Self::Item> { todo!() }
}

impl<U> DoubleEndedIterator for Foo<U>
where
    U: IntoIterator,
    <U as IntoIterator>::IntoIter: DoubleEndedIterator,
{
    fn next_back(&mut self) -> Option<U::Item> {
        todo!()
    }
}

fn main() {}
compiler-errors commented 7 months ago

:thinking: is this #76?

lcnr commented 7 months ago

jup, it's #76