Skip to content

Error message pointing to struct definition instead of pointing to trait impl with missing trait bound #93687

Closed
@brunoczim

Description

@brunoczim

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

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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