Skip to content

Calling a trait method as a free function yields odd diagnostics #98938

Description

@yoshuawuyts

Given the following code: playground

trait Foo {
    fn bar() {}
}

fn main() {
    Foo::bar();
}

The current output is:

error[E0283]: type annotations needed
 --> src/main.rs:4:5
  |
4 |     Foo::bar();
  |     ^^^^^^^^ cannot infer type
  |
  = note: cannot satisfy `_: Foo`

Ideally the output should look like:

Cannot call trait method as a free function. It needs an actual type to be implemented on for it to be called.

For bonus points: The trait is currently not callable since it's not implemented for anything. It needs to actually be implemented. "It looks like you're trying to call this trait method, but nothing implements it."

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jul 5, 2022
  2. added
    D-confusingDiagnostics: Confusing error or lint that should be reworked.
    on Jul 6, 2022
  3. obeis commented on Jul 10, 2022

    @obeis
    Contributor

    @rustbot claim

  4. added a commit that references this issue on Jul 19, 2022
  5. aticu commented on Jul 19, 2022

    @aticu
    Contributor

    This is addressed by #98028. The error message is now

    error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
     --> test.rs:6:5
      |
    2 |     fn bar() {}
      |     ----------- `Foo::bar` defined here
    ...
    6 |     Foo::bar();
      |     ^^^^^^^^ cannot call associated function of trait
    
  6. compiler-errors commented on Jul 19, 2022

    @compiler-errors
    Contributor

    Heads up @obeis #99308

  7. compiler-errors commented on Jul 19, 2022

    @compiler-errors
    Contributor

    I'm gonna close this since it seems like it's fixed. Feel free to file another bug if there are additional changes desired 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.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