rust-lang / rust-lang/rust

rustc starts describing trait bounds and pointing at complicated library internals instead of just saying to add #[derive(Clone)]

Open
#119,114 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics D-confusing D-newcomer-roadblock T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
#[derive(Clone)]
enum Necessity<T> {
    Optional(T),
    Mandatory(T),
}

struct Element<T> {
    attributes: Vec<Necessity<T>>,
    children: Vec<Necessity<Element<T>>>,
}

impl<T: std::clone::Clone> Element<T> {
    fn clone(&self) {
        let _ = self.children.clone();
    }
}

fn main() {
    println!("Hello, world!");
}
Current output
Compiling rust_issue v0.1.0 (/home/seeb/rust/rust_issue)
error[E0599]: the method `clone` exists for struct `Vec<Necessity<Element<T>>>`, but its trait bounds were not satisfied
   --> src/main.rs:14:31
    |
2   | enum Necessity<T> {
    | ----------------- doesn't satisfy `Necessity<Element<T>>: Clone`
...
14  |         let _ = self.children.clone();
    |                               ^^^^^ method cannot be called on `Vec<Necessity<Element<T>>>` due to unsatisfied trait bounds
    |
   ::: /home/seeb/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:396:1
    |
396 | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
    | ------------------------------------------------------------------------------------------------ doesn't satisfy `Vec<Necessity<Element<T>>>: Clone`
    |
    = note: the following trait bounds were not satisfied:
            `Necessity<Element<T>>: Clone`
            which is required by `Vec<Necessity<Element<T>>>: Clone`
help: consider annotating `Necessity<Element<T>>` with `#[derive(Clone)]`
    |
2   + #[derive(Clone)]
3   | enum Necessity<T> {
    |

For more information about this error, try `rustc --explain E0599`.
error: could not compile `rust_issue` (bin "rust_issue") due to previous error
Desired output
help: consider annotating `Element<T>` with `#[derive(Clone)]`
    |
7   + #[derive(Clone)]
8   | struct Element<T> {
    |
Rationale and extra context

No response

Other cases

No response

Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the E0599 diagnostic with the example in src/main.rs and compare the current and desired suggestions. Start by tracing the trait-bound output involving Vec in alloc/src/vec/mod.rs; done when the help points to deriving Clone for Element rather than Necessity<Element>.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.