- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Given the following code:
trait TraitFoo {
    type Bar;
}
struct Foo<T>
where
    T: TraitFoo,
{
    inner: T::Bar,
}
impl<T> Clone for Foo<T>
where
    T: TraitFoo,
    T::Bar: Clone,
{
    fn clone(&self) -> Self {
        Self { inner: self.inner.clone() }
    }
}
impl<T> Copy for Foo<T> {}The current output is:
error[E0277]: the trait bound `T: TraitFoo` is not satisfied
 --> src/main.rs:9:5
  |
9 |     inner: T::Bar,
  |     ^^^^^^^^^^^^^ the trait `TraitFoo` is not implemented for `T`
For more information about this error, try `rustc --explain E0277`.
Ideally the output should look like:
error[EXYZW: the trait bound `T: TraitFoo` is not satisfied
 --> src/main.rs:22:1-26
   |
22 |     impl<T> Copy for Foo<T> {}
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `TraitFoo` is not implemented for `T`
For more information about this error, try `rustc --explain EXYZW`.
To correct the code, one should add two trait bounds to line 22, not to line 9, and it would look like this:
trait TraitFoo {
    type Bar;
}
struct Foo<T>
where
    T: TraitFoo,
{
    inner: T::Bar,
}
impl<T> Clone for Foo<T>
where
    T: TraitFoo,
    T::Bar: Clone,
{
    fn clone(&self) -> Self {
        Self { inner: self.inner.clone() }
    }
}
impl<T> Copy for Foo<T>
where
    T: TraitFoo,
    T::Bar: Copy,
{}Tested on both stable and nightly.
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.