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

Tweak E0038 output to be more pedagogic. #77598

Closed
estebank opened this issue Oct 5, 2020 · 0 comments
Closed

Tweak E0038 output to be more pedagogic. #77598

estebank opened this issue Oct 5, 2020 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. A-traits Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Oct 5, 2020

Given:

pub trait Item {
    fn get_category()->String; // error
    fn do_thing_inner(&self);
    fn get_inner_val(&self) -> u32;
}

impl dyn Item {
    pub fn do_thing(&self) {
        self.do_thing_inner();
        //etc.
    }
    pub fn get_val(&self) -> u32 {
        self.get_inner_val() + 1
    }
}

We currently emit

error[E0038]: the trait `Item` cannot be made into an object
 --> src/lib.rs:7:6
  |
1 | pub trait Item {
  |           ---- this trait cannot be made into an object...
2 |     fn get_category()->String; // error
  |        ------------ ...because associated function `get_category` has no `self` parameter
...
7 | impl dyn Item {
  |      ^^^^^^^^ the trait `Item` cannot be made into an object
  |
help: consider turning `get_category` into a method by giving it a `&self` argument or constraining it so it does not apply to trait objects
  |
2 |     fn get_category()->String where Self: Sized; // error
  |                               ^^^^^^^^^^^^^^^^^

and two other similar errors.

Ideally, we would emit only a single error for this case, and produce output closer to

error[E0038]: the trait `Item` cannot be made into an object
 --> src/lib.rs:7:6
  |
7 | impl dyn Item {
  |      ^^^^^^^^ the trait `Item` cannot be made into an object
  |
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit https://doc.rust-lang.org/reference/items/traits.html#object-safety
  |
1 | pub trait Item {
  |           ---- this trait cannot be made into an object...
2 |     fn get_category()->String; // error
  |        ^^^^^^^^^^^^ ...because associated function `get_category` has no `self` parameter
  |
help: consider turning `get_category` into a method by giving it a `&self` argument
  |
2 |     fn get_category(&self)->String; // error
  |                     ^^^^^
help: alternatively, consider constraining it so it does not apply to trait objects
  |
2 |     fn get_category()->String where Self: Sized; // error
  |                               ^^^^^^^^^^^^^^^^^

Taken from https://users.rust-lang.org/t/why-cant-i-impl-dyn-a-trait-with-static-methods-and-whats-the-correct-way-to-achieve-this/49637

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Oct 5, 2020
@camelid camelid added A-traits Area: Trait system D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. labels Oct 7, 2020
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Oct 9, 2020
JohnTitor added a commit to JohnTitor/rust that referenced this issue Oct 21, 2020
@bors bors closed this as completed in ae0e3d0 Oct 21, 2020
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-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. A-traits Area: Trait system C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants