Skip to content

Bounds on associated types are not taken into account during typeck #26026

Closed
@Gankra

Description

@Gankra

Note that this is the current definition of IntoIterator:

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

    fn into_iter(self) -> Self::IntoIter;
}

So we try to make a type that can be instantiated with any IntoIter, storing its Iterator:

pub struct Internal<I> {
    iter: I
}

impl<I> Internal<I> {
    fn new<It: IntoIterator<IntoIter=I>>(iterable: It) -> Self {
        let iter = iterable.into_iter();
        Internal { iter: iter }
    }
}
<anon>:7:29: 7:40 error: the trait `core::iter::Iterator` is not implemented for the type `I` [E0277]
<anon>:7         let iter = iterable.into_iter();
                                     ^~~~~~~~~~~
<anon>:7:29: 7:40 note: `I` is not an iterator; maybe try calling `.iter()` or a similar method
<anon>:7         let iter = iterable.into_iter();
                                     ^~~~~~~~~~~

Completely nonsensical. Nothing is asking I to be an Iterator, except for IntoIterator's own definiton, which proves that it is in fact an Iterator. But ok, let's try to assert that it is:

pub struct Internal<I> {
    iter: I
}

impl<I: Iterator> Internal<I> {
    fn new<It: IntoIterator<IntoIter=I>>(iterable: It) -> Self {
        let iter = iterable.into_iter();
        Internal { iter: iter }
    }
}
<anon>:7:29: 7:40 error: type mismatch resolving `<I as core::iter::Iterator>::Item == <It as core::iter::IntoIterator>::Item`:
 expected trait `core::iter::Iterator`,
    found trait `core::iter::IntoIterator` [E0271]
<anon>:7         let iter = iterable.into_iter();
                                     ^~~~~~~~~~~

Again nothing is demanding this by IntoIterator, which is already proving it. This works, though:

pub struct Internal<I> {
    iter: I
}

impl<I: Iterator> Internal<I> {
    fn new<It: IntoIterator<IntoIter=I, Item=I::Item>>(iterable: It) -> Self {
        let iter = iterable.into_iter();
        Internal { iter: iter }
    }
}

Which shouldn't be necessary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-type-systemArea: Type systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions