Skip to content

method dispatcher doesn't handle generic implementations properly #5898

Closed
@thestinger

Description

@thestinger

With this example:

pub trait Iterator<A> {
    fn next(&mut self) -> Option<A>;
}

pub trait IteratorUtil<A> {
    fn map<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, Self>;
}

impl<A, T: Iterator<A>> IteratorUtil<A> for T {
    fn map<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, T> {
        MapIterator{iter: self, f: f}
    }
}

pub struct MapIterator<'self, A, B, T> {
    priv iter: T,
    priv f: &'self fn(A) -> B
}

fn main() {
    let x = Some(5);
    x.map(|x| x.to_str());
}

Option doesn't implement Iterator, but the following error happens:

foo.rs:22:4: 22:26 error: failed to find an implementation of trait Iterator<<V4>> for <V5>
foo.rs:22     x.map(|x| x.to_str());
              ^~~~~~~~~~~~~~~~~~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-frontendArea: Compiler frontend (errors, parsing and HIR)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions