Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
brunoczim opened this issue Feb 6, 2022 · 1 comment · Fixed by #93714
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@brunoczim
Copy link

brunoczim commented Feb 6, 2022

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.

@brunoczim brunoczim added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 6, 2022
@compiler-errors
Copy link
Member

Turns out we just attached the wrong ObligationCause. I got a fix!

@rustbot claim

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Feb 24, 2022
…error-span, r=jackh726

better ObligationCause for normalization errors in `can_type_implement_copy`

Some logic is needed so we can point to the field when given totally nonsense types like `struct Foo(<u32 as Iterator>::Item);`

Fixes rust-lang#93687
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Feb 24, 2022
…error-span, r=jackh726

better ObligationCause for normalization errors in `can_type_implement_copy`

Some logic is needed so we can point to the field when given totally nonsense types like `struct Foo(<u32 as Iterator>::Item);`

Fixes rust-lang#93687
@bors bors closed this as completed in 7f99536 Feb 25, 2022
flip1995 pushed a commit to flip1995/rust that referenced this issue Mar 14, 2022
…error-span, r=jackh726

better ObligationCause for normalization errors in `can_type_implement_copy`

Some logic is needed so we can point to the field when given totally nonsense types like `struct Foo(<u32 as Iterator>::Item);`

Fixes rust-lang#93687
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants