Skip to content

Hint suggesting using associated method instead of method uses incorrect syntax for generic method access #61412

Closed
@daboross

Description

@daboross

Consider this example:

struct GenericAssocMethod<T>(T);
impl<T> GenericAssocMethod<T> {
    fn default_hello() {}
}

fn main() {
    let x = GenericAssocMethod(33i32);
    x.default_hello();
}

This errors with the following error:

error[E0599]: no method named `default_hello` found for type `GenericAssocMethod<i32>` in the current scope
 --> src/main.rs:8:7
  |
1 | struct GenericAssocMethod<T>(T);
  | -------------------------------- method `default_hello` not found for this
...
8 |     x.default_hello();
  |     --^^^^^^^^^^^^^
  |     | |
  |     | this is an associated function, not a method
  |     help: use associated function syntax instead: `GenericAssocMethod<i32>::default_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 `GenericAssocMethod<_>`
 --> src/main.rs:3:5
  |
3 |     fn default_hello() {}
  |     ^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

The problem is that if I follow the hint exactly, I end up with GenericAssocMethod<i32>::default_hello();, which is invalid rust syntax.

Ideally, I believe suggesting GenericAssocMethod::<i32>::default_hello would be more beneficial.

Submitting this and #61411 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-enhancementCategory: An issue proposing an enhancement or a PR with one.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