Skip to content

Tweak E0038 output to be more pedagogic. #77598

Closed

Description

@estebank

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

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.
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.
    on Oct 5, 2020
  2. added
    D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.
    on Oct 7, 2020
  3. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on Oct 9, 2020
  4. added a commit that references this issue on Oct 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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