Skip to content

doesn't point out the arbitrary self type is wrong #138440

Open
@est31

Description

@est31

Consider this code:

use std::sync::Arc;

struct Foo;
impl Foo {
    fn bar(&self) {
        self.hello_world_next();
    }
    fn hello_world(&self) {}
    fn hello_world_next(self: &Arc<Self>) {}
}
fn main() {}

it will error as bar takes &self but hello_world_next takes &Arc<Self>. We give a suboptimal error here though:

error[E0599]: no method named `hello_world_next` found for reference `&Foo` in the current scope
 --> src/main.rs:6:14
  |
6 |         self.hello_world_next();
  |              ^^^^^^^^^^^^^^^^
  |
help: there is a method `hello_world` with a similar name
  |
6 -         self.hello_world_next();
6 +         self.hello_world();
  |

For more information about this error, try `rustc --explain E0599`.

We should instead suggest that hello_world_next exists as a function, but takes &Arc<Self> as type so can't be used.

If one puts &mut self instead of &Arc<Self>, it gives a nicer error:

error[E0596]: cannot borrow `*self` as mutable, as it is behind a `&` reference
 --> a.rs:6:9
  |
6 |         self.hello_world_next();
  |         ^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable
  |
help: consider changing this to be a mutable reference
  |
5 |     fn bar(&mut self) {
  |             +++

error: aborting due to 1 previous error; 1 warning emitted

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions