Skip to content

The compiler should be more helpful when trait bounds are not met. #64417

Closed
@glandium

Description

@glandium

Here's a reduced testcase:

use std::sync::Arc;

struct Foo;

#[derive(Clone)]
struct Bar<T>(Arc<T>);

fn main() {
    let a = Bar(Arc::new(Foo));
    let b = a.clone();
}

This fails to compile with:

error[E0599]: no method named `clone` found for type `Bar<Foo>` in the current scope
  --> src/main.rs:10:15
   |
6  | struct Bar<T>(Arc<T>);
   | ---------------------- method `clone` not found for this
...
10 |     let b = a.clone();
   |               ^^^^^
   |
   = note: the method `clone` exists but the following trait bounds were not satisfied:
           `Bar<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`

The way #[derive] works (#26925), and with 20/20 hindsight, it's obvious what's wrong. But even when you know about #26925 it takes a mental effort to go from that error message to "oh, right, #[derive(Clone)] adds T: Clone".

It would be useful if the compiler went further, and, considering there is an impl Clone for Bar<T>, detailed why that impl doesn't apply to Foo.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemD-confusingDiagnostics: Confusing error or lint that should be reworked.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