Skip to content

Incorrect hint when calling associated method as method with Deref #61411

Closed
@daboross

Description

@daboross

Here's a simple example:

use std::cell::RefCell;

struct HasAssocMethod;

impl HasAssocMethod {
    fn hello() {}
}
fn main() {
    let shared_state = RefCell::new(HasAssocMethod);
    
    let state = shared_state.borrow_mut();
    
    state.hello();
}

(playground)

This outputs the following error:

   Compiling playground v0.0.1 (/playground)
error[E0599]: no method named `hello` found for type `std::cell::RefMut<'_, HasAssocMethod>` in the current scope
  --> src/main.rs:13:11
   |
13 |     state.hello();
   |     ------^^^^^
   |     |     |
   |     |     this is an associated function, not a method
   |     help: use associated function syntax instead: `std::cell::RefMut<'_, HasAssocMethod>::hello`
   |
   = note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in an impl for the type `HasAssocMethod`
  --> src/main.rs:6:5
   |
6  |     fn hello() {}
   |     ^^^^^^^^^^

error: aborting due to previous error

The hint is incorrect, as std::cell::RefMut<'_, HasAssocMethod>::hello(); does not exist. Ideally the compiler should suggest HasAssocMethod::hello();.

Submitting this and #61412 as two separate issues, but I found both for the same hint.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions