Skip to content

Deriving Clone and Copy twice generates bad diagnostics #131083

New issue

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

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

Already on GitHub? # to your account

Closed
cyrgani opened this issue Oct 1, 2024 · 1 comment · Fixed by #131153
Closed

Deriving Clone and Copy twice generates bad diagnostics #131083

cyrgani opened this issue Oct 1, 2024 · 1 comment · Fixed by #131153
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

@cyrgani
Copy link
Contributor

cyrgani commented Oct 1, 2024

Code

#[derive(Clone, Copy)]
#[derive(Clone, Copy)]
enum E {}

Current output

error[E0119]: conflicting implementations of trait `Clone` for type `E`
 --> src/lib.rs:2:10
  |
1 | #[derive(Clone, Copy)]
  |          ----- first implementation here
2 | #[derive(Clone, Copy)]
  |          ^^^^^ conflicting implementation for `E`
  |
  = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0119]: conflicting implementations of trait `Copy` for type `E`
 --> src/lib.rs:2:17
  |
1 | #[derive(Clone, Copy)]
  |                 ---- first implementation here
2 | #[derive(Clone, Copy)]
  |                 ^^^^ conflicting implementation for `E`
  |
  = note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0507]: cannot move out of `*self` which is behind a shared reference
 --> src/lib.rs:2:10
  |
2 | #[derive(Clone, Copy)]
  |          ^^^^^ move occurs because `*self` has type `E`, which does not implement the `Copy` trait
  |
note: if `E` implemented `Clone`, you could clone the value
 --> src/lib.rs:3:1
  |
2 | #[derive(Clone, Copy)]
  |          ----- you could clone this value
3 | enum E {}
  | ^^^^^^ consider implementing `Clone` for this type
  = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0507]: cannot move out of `*self` which is behind a shared reference
 --> src/lib.rs:1:10
  |
1 | #[derive(Clone, Copy)]
  |          ^^^^^ move occurs because `*self` has type `E`, which does not implement the `Copy` trait
  |
note: if `E` implemented `Clone`, you could clone the value
 --> src/lib.rs:3:1
  |
1 | #[derive(Clone, Copy)]
  |          ----- you could clone this value
2 | #[derive(Clone, Copy)]
3 | enum E {}
  | ^^^^^^ consider implementing `Clone` for this type
  = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)

Desired output

error[E0119]: conflicting implementations of trait `Clone` for type `E`
 --> src/lib.rs:2:10
  |
1 | #[derive(Clone, Copy)]
  |          ----- first implementation here
2 | #[derive(Clone, Copy)]
  |          ^^^^^ conflicting implementation for `E`
  |
  = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0119]: conflicting implementations of trait `Copy` for type `E`
 --> src/lib.rs:2:17
  |
1 | #[derive(Clone, Copy)]
  |                 ---- first implementation here
2 | #[derive(Clone, Copy)]
  |                 ^^^^ conflicting implementation for `E`
  |
  = note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)

Rationale and extra context

First encountered this with a macro that automatically derived Clone and Copy. The later two errors are pointing to internal implementation details and have wrong spans; they should be removed. They also suggest "consider implementing Clone for this type", which is nonsense.

Other cases

No response

Rust Version

1.83.0 nightly on playground

Anything else?

No response

@cyrgani cyrgani 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 Oct 1, 2024
@VulnBandit
Copy link
Contributor

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 14, 2024
…ler-errors

Improve duplicate derive Copy/Clone diagnostics

Improve duplicate derive Copy/Clone diagnostics.
Closes rust-lang#131083
tgross35 added a commit to tgross35/rust that referenced this issue Nov 5, 2024
…ler-errors

Improve duplicate derive Copy/Clone diagnostics

Improve duplicate derive Copy/Clone diagnostics.
Closes rust-lang#131083
@bors bors closed this as completed in 972fef2 Nov 5, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 5, 2024
Rollup merge of rust-lang#131153 - VulnBandit:copy_impl_vuln, r=compiler-errors

Improve duplicate derive Copy/Clone diagnostics

Improve duplicate derive Copy/Clone diagnostics.
Closes rust-lang#131083
# for free to join this conversation on GitHub. Already have an account? # 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