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

Hint about missing trait method suggests incorrect code (self parameter not named "self"). #71150

Closed
steffahn opened this issue Apr 14, 2020 · 6 comments · Fixed by #71188
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. F-arbitrary_self_types `#![feature(arbitrary_self_types)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@steffahn
Copy link
Member

steffahn commented Apr 14, 2020

In the error message below, the first argument of poll is not named self.

use core::future::Future;

struct S;
impl Future for S {}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0046]: not all trait items implemented, missing: `Output`, `poll`
 --> src/lib.rs:4:1
  |
4 | impl Future for S {}
  | ^^^^^^^^^^^^^^^^^ missing `Output`, `poll` in implementation
  |
  = help: implement the missing item: `type Output = Type;`
  = help: implement the missing item: `fn poll(_: std::pin::Pin<&mut Self>, _: &mut std::task::Context<'_>) -> std::task::Poll<<Self as std::future::Future>::Output> { unimplemented!() }`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0046`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

@rustbot modify labels: T-compiler, A-diagnostics, C-enhancement, D-papercut, F-arbitrary_self_types.

This issue has been assigned to @Duddino via this comment.

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. F-arbitrary_self_types `#![feature(arbitrary_self_types)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 14, 2020
@Duddino
Copy link

Duddino commented Apr 15, 2020

@rustbot claim

@rustbot rustbot self-assigned this Apr 15, 2020
@Duddino
Copy link

Duddino commented Apr 15, 2020

I managed to fix this issue by just manually checking if the argument is of type std::pin::Pin<&mut Self>, and if it is it returns format!("self: {}", ty); where ty is the type of the argument (in this case std::pin::Pin<&mut Self>). I'm not sure if there is a better way, I'm going to make a PR tomorrow, if anyone has a better solution, please tell me.

@steffahn
Copy link
Member Author

I’m not just interested in a fix for this particular case. Types that allow dispatch can be arbitrarily nested AFAIK and also I didn’t tag F-arbitrary_self_types for no reason - these can be extended. Furthermore the correct way to fix this is probably not to just add code but rather to change existing logic that must be somewhere already, for example some code currently does make self, &self and &mut self parameters print correctly and differently from non-self parameters that have type Self or...... let me double-check that....

Oh, wow, I’ll expand this bug-report.

Hint about missing trait method suggests incorrect code (non-self parameter mistakenly named "self").

Create a binary crate named “my-crate” with:

main.rs

struct S;
impl my_crate::Tr for S {}

fn main() {
    println!("Hello, world!");
}

lib.rs

pub trait Tr {
  fn f(self: &Self, not_self: &Self);
}

Error message is:

error[E0046]: not all trait items implemented, missing: `f`
 --> src\main.rs:2:1
  |
2 | impl my_crate::Tr for S {}
  | ^^^^^^^^^^^^^^^^^^^^^^^ missing `f` in implementation
  |
  = help: implement the missing item: `fn f(&self, &self) { unimplemented!() }`

@Duddino
Copy link

Duddino commented Apr 15, 2020

Makes sense as if I recall correctly (can't link files rn as I am on my phone) the code literally just checks if the type is (&/&mut)Self and if it is it prints (&/&mut)self otherwise it prints _: Type. A potential fix could be printing (&/&mut)self only if it's the first argument of the function

@steffahn
Copy link
Member Author

steffahn commented Apr 15, 2020

Won’t be enough. The first parameter of a trait fn could be of type Self without being named self. It actually needs to check if it’s a self parameter. That information should be retrievable somewhere in the compiler. Furthermore then the rest is easy: First step: check if it’s a self parameter. If not: use _ : {ty}. If it however is one, then check for the type being Self or &Self or &mut Self, printing self, &self or &mut self respectively; otherwise print self: {ty}.

@Duddino
Copy link

Duddino commented Apr 16, 2020

Another issue related to this is when you have a function that has a signature of fn name(whatever: &Self); The compiler suggests to implement the function using fn name(&self){ todo!() };, but that doesn't work

I should have fixed it, as soon as all of the tests pass I will make a PR

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 19, 2020
Fixed missing trait method suggests incorrect code (self parameter not named "self").

fixes rust-lang#71150
@bors bors closed this as completed in 36791da Apr 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. F-arbitrary_self_types `#![feature(arbitrary_self_types)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants