Skip to content

Self is not recognised as implementing inherited traits in default method implementations #7661

Closed
@chris-morgan

Description

@chris-morgan

Sample code:

#[allow(default_methods)];

fn require_y<T: Y>(_: T){}

trait Y {
    fn y(self);
}

trait InheritedMethodCallIsOk: Y {
    fn x(self) {
        self.y();  // Observe this carefully; the methods are there
    }
}

trait SelfTypeIsBroken: Y {  // Self must implement Y
    fn x(self) {
        require_y(self);  // ... but it is not recognised as implementing Y
    }
}

I expect this to succeed. It does not:

test.rs:17:8: 18:17 error: failed to find an implementation of trait Y for Self
test.rs:17         require_y(self);  // ... but it is not recognised as implementing Y
                   ^~~~~~~~~

Note that the inheritance works correctly at the impl level:

#[allow(default_methods)];

fn require_y<T: Y>(_: T){}

trait Y {
    fn y(self) {}
}

trait SelfIsOkInImpl: Y {
    fn x(self);
}

struct X;
impl Y for X;
impl SelfIsNotY for X {
    fn x(self) {
        require_y(self);
    }
}

fn main(){}

Second example, using Send, a kind, rather than a trait of my own devising:

#[allow(default_methods)];

fn require_send<T: Send>(_: T){}

trait TragicallySelfIsNotSend: Send {
    fn x(self) {
        require_send(self);
    }
}

fn main(){}

This similarly does not compile, though it should:

test.rs:7:8: 7:20 error: instantiating a type parameter with an incompatible type `Self`, which does not fulfill `Send`
test.rs:7         require_send(self);
                  ^~~~~~~~~~~~
error: aborting due to previous error

This, in fact, is how I came across the problem, because I'm wanting to refer to self inside a spawn block, and that will only work if it's Send, and so there I sadly cannot provide a default method implementation of the trait at present.

rust 0.8-pre (a48ca32 2013-07-08 18:49:46 -0700)
host: x86_64-unknown-linux-gnu

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