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

Improve method suggestion for methods with custom receiver type #138887

Open
0xdeafbeef opened this issue Mar 24, 2025 · 1 comment
Open

Improve method suggestion for methods with custom receiver type #138887

0xdeafbeef opened this issue Mar 24, 2025 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@0xdeafbeef
Copy link

0xdeafbeef commented Mar 24, 2025

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

@0xdeafbeef 0xdeafbeef added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 24, 2025
@xizheyin
Copy link
Contributor

Maybe similar to #138440

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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants