Skip to content

method lookup within an impl fails when receiver is an iface type variable #1909

Closed
@nikomatsakis

Description

@nikomatsakis

the following will not compile:

iface foo {
    fn one() -> uint;
}

impl of foo for uint {
    fn one() -> uint { ret self; }
}

impl extra<F: foo> for F {
    fn two() -> uint { ret self.one() * 2u; }

    fn four() -> uint { ret self.two() * 2u; }
    // ^ Error: method two() not found
}

fn main() {
    let x = 1u.one();
    let x = 1u.two();
    let x = 1u.four();
}

I assume the problem is that the method lookup says "ah, self is a type variable that implements foo" and thus just searches the iface foo for the method two(). This is of course correct, but it should also search for in-scope impls.

In the absence of traits, defining impls like this is a handy way to get "default" method implementations. I put "default" in quotes because they can't be overridden. Regardless it should work---it's still useful to be able to "extend" an iface like this. I wanted it for my mockups of the iter library.

I assigned @marijnh since he seems best qualified.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions