Skip to content

E0283 could be better for calls to associated functions on the trait itself #81701

Closed
@aticu

Description

@aticu

This code (playground)

trait MyTrait {
    fn my_fn();
}

struct MyStruct;

impl MyTrait for MyStruct {
    fn my_fn() {}
}

fn main() {
    MyTrait::my_fn();
}

currently produces this error:

error[E0283]: type annotations needed
  --> src/main.rs:12:5
   |
2  |     fn my_fn();
   |     ----------- required by `MyTrait::my_fn`
...
12 |     MyTrait::my_fn();
   |     ^^^^^^^^^^^^^^ cannot infer type
   |
   = note: cannot satisfy `_: MyTrait`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0283`.

When I recently stumbled across this error, it took me a little while to figure out what the problem was.
I was trying to call the trait function directly and not an implementation of the function.
I think this may be even more confusing for beginners.

I think the following error message would have helped me (and may help others) understand what the problem is and how to fix it much more quickly:

error[E0283]: a concrete implementation is needed to call the trait function
  --> src/main.rs:12:5
   |
12 |     MyTrait::my_fn();
   |     ^^^^^^^^^^^^^^ cannot infer concrete implementation of `my_fn`
   |
   = note: cannot satisfy `_: MyTrait`
   = help: try calling `my_fn` on an implementation of `MyTrait` such as `MyStruct::my_fn`
   = help: available implementations are `MyStruct`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0283`.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.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