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

Show error E0277 when the trait bound of a struct's member's type is unsatisfied even if E0204 is also present #89137

Closed
john01dav opened this issue Sep 21, 2021 · 4 comments · Fixed by #97664 or #97722
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@john01dav
Copy link

john01dav commented Sep 21, 2021

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=fbd47fd0a07c3a9a05978696f46aac3d

use std::fmt::Debug;

#[derive(Debug, Copy, Clone)]
pub struct Vector2<T: Debug + Copy + Clone>{
    pub x: T,
    pub y: T
}

#[derive(Debug, Copy, Clone)]
pub struct AABB<T>{
    pub loc: Vector2<T>,
    pub size: Vector2<T>
}

The current output is:

error[E0204]: the trait `Copy` may not be implemented for this type
  --> src/main.rs:10:17
   |
10 | #[derive(Debug, Copy, Clone)]
   |                 ^^^^
11 | pub struct AABB<T>{
12 |     pub loc: Vector2<T>,
   |     ------------------- this field does not implement `Copy`
13 |     pub size: Vector2<T>
   |     -------------------- this field does not implement `Copy`
   |
   = note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0204`.

Ideally the output should look like:

Keeping the other error may or may not make sense, but I propose adding this, as it points more towards how to fix the issue and prevents barking up the tree of how a derive(Copy)'d type can not implement copy (there's a good answer here, but it was not immediately obvious to me upon seeing the original error).

I generated this error message by removing the derive line from the AABB struct.

error[E0277]: the trait bound `T: Copy` is not satisfied
  --> src/lib.rs:10:14
   |
4  | pub struct Vector2<T: Debug + Copy + Clone>{
   |                               ---- required by this bound in `Vector2`
...
10 |     pub loc: Vector2<T>,
   |              ^^^^^^^^^^ the trait `Copy` is not implemented for `T`
   |
help: consider restricting type parameter `T`
   |
9  | pub struct AABB<T: std::marker::Copy>{
   |                  ^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to 2 previous errors

This bug takes place on beta and nightly according to the Rust playground.

@john01dav john01dav 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 Sep 21, 2021
@john01dav john01dav changed the title Show ereror E0277 when the trait bound of a struct's member's type is unsatisfied even if E0204 is also present Show error E0277 when the trait bound of a struct's member's type is unsatisfied even if E0204 is also present Sep 21, 2021
@rodrimati1992
Copy link
Contributor

rodrimati1992 commented Sep 21, 2021

This bug takes place on beta and nightly according to the Rust playground.

That's the exact error from Rust 1.43.0 onward, before that it doesn't say what derive the error comes from.
Just clarifying that this is not a recent change.

@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Sep 24, 2021
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Oct 15, 2021
@compiler-errors
Copy link
Member

The error has gotten (slightly) better on nightly since a recent change of mine:

error[[E0204]](https://doc.rust-lang.org/nightly/error-index.html#E0204): the trait `Copy` may not be implemented for this type
  [--> src/lib.rs:9:17
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018#)   |
9  | #[derive(Debug, Copy, Clone)]
   |                 ^^^^
10 | pub struct AABB<T>{
11 |     pub loc: Vector2<T>,
   |     ------------------- this field does not implement `Copy`
12 |     pub size: Vector2<T>
   |     -------------------- this field does not implement `Copy`
   |
note: the `Copy` impl for `Vector2<T>` requires that `T: Debug`
  [--> src/lib.rs:11:5
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018#)   |
11 |     pub loc: Vector2<T>,
   |     ^^^^^^^^^^^^^^^^^^^
note: the `Copy` impl for `Vector2<T>` requires that `T: Debug`
  [--> src/lib.rs:12:5
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018#)   |
12 |     pub size: Vector2<T>
   |     ^^^^^^^^^^^^^^^^^^^^
   = note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)

It only suggests one of the missing bounds, adding T: Debug then another error suggests adding T: Copy...

@estebank estebank self-assigned this Jun 2, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jun 3, 2022
… r=compiler-errors

On E0204 suggest missing type param bounds

```
error[E0204]: the trait `Copy` may not be implemented for this type
  --> f42.rs:9:17
   |
9  | #[derive(Debug, Copy, Clone)]
   |                 ^^^^
10 | pub struct AABB<K>{
11 |     pub loc: Vector2<K>,
   |     ------------------- this field does not implement `Copy`
12 |     pub size: Vector2<K>
   |     -------------------- this field does not implement `Copy`
   |
note: the `Copy` impl for `Vector2<K>` requires that `K: Debug`
  --> f42.rs:11:5
   |
11 |     pub loc: Vector2<K>,
   |     ^^^^^^^^^^^^^^^^^^^
note: the `Copy` impl for `Vector2<K>` requires that `K: Debug`
  --> f42.rs:12:5
   |
12 |     pub size: Vector2<K>
   |     ^^^^^^^^^^^^^^^^^^^^
   = note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider restricting type parameter `K`
   |
10 | pub struct AABB<K: Debug>{
   |                  +++++++
```

Fix rust-lang#89137.
@bors bors closed this as completed in 8567b68 Jun 3, 2022
@estebank
Copy link
Contributor

estebank commented Jun 3, 2022

@compiler-errors It would be nice if the obligation.cause.span that ends up being used in the "the Copy impl for Vector2<T> requires that T: Debug" note pointed at the field type, instead of the whole field as it does now. Would that be something you'd have time to explore? The actual change is simple enough, but finding where the obligation is constructed might require some exploring.

@compiler-errors
Copy link
Member

I'll put it on my to-do list. I'm pretty sure the obligation is just constructed in can_type_implement_copy, but maybe it's a sub-obligation.

@rustbot claim

@rustbot rustbot assigned compiler-errors and unassigned estebank Jun 3, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jun 4, 2022
…rror-spans, r=Dylan-DPC

Tighten spans for bad fields in struct deriving `Copy`

r? `@estebank`

Closes rust-lang#89137 for good, I think

Not sure if this is what you were looking for in rust-lang#89137 (comment)
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
5 participants