Skip to content

Improve method suggestion for methods with custom receiver type #138887

Open
@0xdeafbeef

Description

@0xdeafbeef

Code

use std::sync::Arc;

struct Foo;

impl Foo {
    fn frob(self: &Arc<Self>) {}  // Method exists for &Arc<Foo>
    fn no_frob(&self) {}
}

fn test(foo: &Foo) {
    foo.frob()  // Error: called on &Foo instead of &Arc<Foo>
}

playground

Current output

error[E0599]: no method named `frob` found for reference `&Foo` in the current scope
  --> src/lib.rs:12:9
   |
12 |     foo.frob()
   |         ^^^^
   |
help: there is a method `no_frob` with a similar name
   |
12 |     foo.no_frob()
   |         ~~~~~~~

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (lib) due to 1 previous error

Desired output

help: the method `frob` exists for `&Arc<Foo>`

Rationale and extra context

This issue becomes particularly important during refactoring scenarios. Consider this common situation:

  1. A codebase initially has a method taking &self
  2. During refactoring by the other dev, the method is changed to take &Arc<Self>
  3. Existing code using &Foo breaks, and the compiler suggests unrelated similarly named methods

The current error message can mislead developers into thinking the method was renamed (especially when it suggests alternative methods like no_frob), when in reality the method still exists but requires a different receiver type. This can lead to:

  • Wasted time searching for "renamed" methods
  • Confusion about the correct API usage
  • Potentially incorrect fixes (using suggested similar-named methods instead of properly wrapping in Arc)

Other cases

Rust Version

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.1.7

Anything else?

No response

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