Skip to content

Improve diagnostics when a type does not impl a trait despite #[derive]'ing it #69658

@tmandry

Description

@tmandry

Originally posted by @simonbuchan in #26925 (comment)

I think I would be a lot happier with the current situation with "just" adding more information to the compiler diagnostic.

At the moment, you get something like:

error[E0599]: no method named `clone` found for type `Ptr<Foo>` in the current scope
 --> src/main.rs:9:15
  |
2 | struct Ptr<T>(*mut T);
  | ---------------------- method `clone` not found for this
...
9 |     let b = a.clone();
  |               ^^^^^ method not found in `Ptr<Foo>`
  |
  = note: the method `clone` exists but the following trait bounds were not satisfied:
          `Ptr<Foo> : std::clone::Clone`
  = help: items from traits can only be used if the trait is implemented and in scope
  = note: the following trait defines an item `clone`, perhaps you need to implement it:
          candidate #1: `std::clone::Clone`

or

error[E0382]: use of moved value: `a`
  --> src/main.rs:10:15
   |
8  |     let a = Ptr(&mut foo);
   |         - move occurs because `a` has type `Ptr<Foo>`, which does not implement the `Copy` trait
9  |     let b = a;
   |             - value moved here
10 |     let ptr = a.0;
   |               ^^^ value used here after move

Neither of these tell you that the reason Ptr doesn't implement Copy or Clone, despite #[derive(Copy, Clone)] being right there. I think some logic to add another sentence for this would get rid of the case where derive doesn't implement when it should, which I think newer users (like me!) would be more likely to hit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions