Skip to content

Verbose output for simple mistake of using method syntax for associated functions #102354

Closed
@Rageking8

Description

@Rageking8

Given the following code: link

trait Trait {
    fn func() {}
}

impl Trait for i32 {}

fn main() {
    let x: i32 = 123;
    x.func();
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0599]](https://doc.rust-lang.org/nightly/error-index.html#E0599): no method named `func` found for type `i32` in the current scope
 --> src/main.rs:9:7
  |
9 |     x.func();
  |       ^^^^ 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 `Trait`
 --> src/main.rs:2:5
  |
2 |     fn func() {}
  |     ^^^^^^^^^
  = help: items from traits can only be used if the trait is implemented and in scope
note: `Trait` defines an item `func`, perhaps you need to implement it
 --> src/main.rs:1:1
  |
1 | trait Trait {
  | ^^^^^^^^^^^
help: use associated function syntax instead
  |
9 |     i32::func();
  |     ~~~~~~~~~
help: disambiguate the associated function for the candidate
  |
9 |     <i32 as Trait>::func(x);
  |     ~~~~~~~~~~~~~~~~~~~~~~~

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

In the above current output there are many general help and note messages which may not be super relevant in the above context. On top of that, there are 2 suggested help messages (bottom 2), where the first one (i32::func();) is correct and the second one (<i32 as Trait>::func(x);) is wrong with the extra parameter passed in. Hence, the ideal output should be much leaner and the only help message should the valid one. Thanks.

@rustbot label +D-invalid-suggestion +D-verbose +D-confusing

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.T-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