Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
aticu opened this issue Feb 3, 2021 · 2 comments · Fixed by #98028
Closed

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

aticu opened this issue Feb 3, 2021 · 2 comments · Fixed by #98028
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@aticu
Copy link
Contributor

aticu commented Feb 3, 2021

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`.
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Feb 3, 2021
@estebank estebank added A-inference Area: Type inference A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 3, 2021
@estebank
Copy link
Contributor

A new error code should be given for this case and looks something along the lines of:

error[EXXXX]: cannot call associated function on trait without specifying the corresponding `impl` type
  --> src/main.rs:12:5
   |
2  |     fn my_fn();
   |     ----------- `MyTrait::my_fn` defined here
...
12 |     MyTrait::my_fn();
   |     ^^^^^^^^^^^^^^ cannot call associated function of trait
   |
help: use a fully-qualified path to a specific available implementation
   |
12 |     <MyStruct as MyTrait>::my_fn();
   |     ^^^^^^^^^^^^^       ^

@aticu
Copy link
Contributor Author

aticu commented May 23, 2022

@rustbot claim

@aticu aticu changed the title E0238 could be better for calls to associated functions on the trait itself E0283 could be better for calls to associated functions on the trait itself May 30, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jul 19, 2022
Add E0789 as more specific variant of E0283

Fixes rust-lang#81701

I think this should be good to go, there are only two things where I am somewhat unsure:
- Is there a better way to get the fully-qualified path for the suggestion? I tried `self.tcx.def_path_str`, but that didn't seem to always give a correct path for the context.
- Should all this be extracted into it's own method or is it fine where it is?

r? `@estebank`
@bors bors closed this as completed in bfefd58 Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants