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

"#[derive] can't be used on a non-Copy #[repr(packed)] struct" but struct is Copy #50826

Open
RalfJung opened this issue May 17, 2018 · 6 comments
Labels
A-attributes Area: #[attributes(..)] C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@RalfJung
Copy link
Member

Reported by @retep998 at #46043 (comment), here's a minimal example:

#[repr(packed)]
#[derive(Debug)]
struct Foo {
    x: usize,
}

impl Clone for Foo {
    fn clone(&self) -> Self {
        Foo { x: self.x }
    }
}
impl Copy for Foo {}

play

@Arnavion
Copy link

Arnavion commented Jun 12, 2018

The warning changed in #51143 to require the struct to derive, rather than manually impl, Copy. So technically this issue is fixed.

However #51143 (comment) says that the compiler does generate code that uses references to fields if Copy is impled manually instead of #[derive]d. Is there an issue to track fixing the compiler to emit explicit impls of Copy correctly as you mentioned in #51143 (comment) or should this one be reused for that?

@Mark-Simulacrum
Copy link
Member

Let's just reuse this issue for that. It shouldn't be too hard of a change, I think, but I'm uncertain.

@RalfJung
Copy link
Member Author

However #51143 (comment) says that the compiler does generate code that uses references to fields if Copy is impled manually instead of #[derive]d.

I don't understand. Could you post some code that doesn't behave as it should?

@retep998
Copy link
Member

Switching from impl Copy for $name {} to #[derive(Copy)] allows winapi to avoid this issue, but also incurs a 0.2 second compile time penalty in expansion.

@XAMPPRocky XAMPPRocky added A-attributes Area: #[attributes(..)] T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Oct 2, 2018
@steveklabnik
Copy link
Member

Triage: this still warns today

@patrickelectric
Copy link
Contributor

I'm getting this warning here:

#[derive(Derivative, Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C, packed)]
pub struct QueryMenu {
warning: `#[derive]` can't be used on a `#[repr(packed)]` struct that does not derive Copy (error E0133)
   --> src/v4l2.rs:460:58
    |
460 | #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
    |                                                          ^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(safe_packed_borrows)]` on by default
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #46043 <https://github.com/rust-lang/rust/issues/46043>
    = note: this warning originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: #[attributes(..)] C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants