Skip to content

Unexpected "failed to find an implementation of trait" when combining mut self and impl for T where T is a type variable #6594

@Dretch

Description

@Dretch

This code doesn't compile:

struct Foo { x: int }

impl io::Reader for Foo {
    fn read(&self, _bytes: &mut [u8], _len: uint) -> uint { fail!() }
    fn read_byte(&self) -> int { fail!() }
    fn eof(&self) -> bool { fail!() }
    fn seek(&self, _position: int, _style: io::SeekStyle) { fail!() }
    fn tell(&self) -> uint { fail!() }
}

impl Foo {
    fn bar(&mut self) {
        self.read_be_u32(); // read_be_u32 is defined by io::ReaderUtil, which is defined for all <T: io::Reader>
    }
}

fn main() {}

rustc fails with this error:

test-impl-for-t.rs:15:12: 15:31 error: failed to find an implementation of trait core::io::Reader for &mut Foo
test-impl-for-t.rs:15             self.read_be_u32();
                                   ^~~~~~~~~~~~~~~~~~~

I don't think that rustc should have a problem with this code.

Note that this error seems to be related to &mut pointers and immutable borrows, because if the bar function is changed to the following then rustc is happy:

let iself: &Foo = self;
iself.read_be_u32();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions