Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
chris-morgan opened this issue Jul 9, 2013 · 4 comments

Comments

@chris-morgan
Copy link
Member

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

@Blei
Copy link
Contributor

Blei commented Jul 9, 2013

Related to #7183, #7320.

@msullivan
Copy link
Contributor

Yeah, the interaction of default methods and inheritance basically doesn't work at all. Part of #2794.

@chris-morgan
Copy link
Member Author

@msullivan Thanks, that fixes the first example, but it doesn't fix the second example (the Send one).

@chris-morgan
Copy link
Member Author

Opened #8171 about the second example (Send).

flip1995 pushed a commit to flip1995/rust that referenced this issue Sep 28, 2021
Fix various redundant_closure bugs

changelog: Fix various false negatives and false positives for [`redundant_closure`]

Closes rust-lang#3071
Closes rust-lang#4002

This lint is full of weird nuances and this is basically a re-write to tighten up the logic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants