Skip to content

Unhelpful help when calling associated trait function as method #103646

Closed
@Noratrieb

Description

@Noratrieb

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=895f7d2db9412f8cb60bcdd2fa05105d

trait Cat {
    fn nya() {}
}

fn uwu<T: Cat>(c: T) {
    c.nya();
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0599]](https://doc.rust-lang.org/stable/error-index.html#E0599): no method named `nya` found for type parameter `T` in the current scope
 --> src/lib.rs:6:7
  |
5 | fn uwu<T: Cat>(c: T) {
  |        - method `nya` not found for this type parameter
6 |     c.nya();
  |       ^^^ this is an associated function, not a method
  |
  = note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in the trait `Cat`
 --> src/lib.rs:2:5
  |
2 |     fn nya() {}
  |     ^^^^^^^^
  = help: items from traits can only be used if the type parameter is bounded by the trait
help: disambiguate the associated function for the candidate
  |
6 |     <T as Cat>::nya(c);
  |     ~~~~~~~~~~~~~~~~~~

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` due to previous error
Compiling playground v0.0.1 (/playground)
error[[E0599]](https://doc.rust-lang.org/stable/error-index.html#E0599): no method named `nya` found for type parameter `T` in the current scope
 --> src/lib.rs:6:7
  |
5 | fn uwu<T: Cat>(c: T) {
  |        - method `nya` not found for this type parameter
6 |     c.nya();
  |       ^^^ this is an associated function, not a method
  |
  = note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: the candidate is defined in the trait `Cat`
 --> src/lib.rs:2:5
  |
2 |     fn nya() {}
  |     ^^^^^^^^


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

The error already explains the problem ("this is an associated function, not a method") and the suggestion to use a fully qualified call doesn't help. Optionally, it could even suggest calling T::nya() instead.

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