-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Copy derive is ignored by rustc #56008
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
Comments
I don't believe this is a bug. Your original code expands to #![feature(prelude_import)]
#![no_std]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
use std::marker::PhantomData;
pub trait Precision {}
pub struct Satoshi;
impl Precision for Satoshi {}
type Inner = i64;
#[rustc_copy_clone_marker]
pub struct Amount<P: Precision = Satoshi>(Inner, PhantomData<P>);
#[automatically_derived]
#[allow(unused_qualifications)]
impl<P: ::std::marker::Copy + Precision> ::std::marker::Copy for Amount<P> {}
#[automatically_derived]
#[allow(unused_qualifications)]
impl<P: ::std::clone::Clone + Precision> ::std::clone::Clone for Amount<P> {
#[inline]
fn clone(&self) -> Amount<P> {
match *self {
Amount(ref __self_0_0, ref __self_0_1) => Amount(
::std::clone::Clone::clone(&(*__self_0_0)),
::std::clone::Clone::clone(&(*__self_0_1)),
),
}
}
}
~\tmp\foo [master +14 ~0 -0 !]> Note that |
Well, before I used With this snippet, the compiler doesn't complain about the derives. I double checked, if I remove that one specific unit test that tries to copy, all other tests run fine. |
Here is the original PR that contains this code: rust-bitcoin/rust-bitcoin#192 |
It does, but |
I ran into the same issue. use std::marker::PhantomData;
#[derive(Copy, Clone)]
struct SpookyThing<T> {
phantom: PhantomData<*const T>,
}
#[derive(Clone)]
struct NotCopyable;
fn main() {
let scary = SpookyThing::<NotCopyable> {
phantom: PhantomData,
};
creep(scary, scary);
}
fn creep<T>(_creep1: SpookyThing<T>, _creep2: SpookyThing<T>) {}
It seems derive pays attention to the type of template argument, not the actual data items. |
You can work around the issue by implementing Clone and Copy manually. struct SpookyThing<T> {
phantom: PhantomData<*const T>,
}
impl<T> Clone for SpookyThing<T> {
fn clone(&self) -> Self {
SpookyThing {
phantom: PhantomData
}
}
}
impl<T> Copy for SpookyThing<T> {} If you attempt to derive Copy, and implement Copy manually, rustc does report an error. #[derive(Copy, Clone)]
struct SpookyThing<T> {
phantom: PhantomData<*const T>,
}
impl<T> Clone for SpookyThing<T> {
fn clone(&self) -> Self {
SpookyThing {
phantom: PhantomData
}
}
}
impl<T> Copy for SpookyThing<T> {}
|
Yeah I also implemented the traits manually as a workaround. I put the original issue (#26925) in the OP. |
You can work around this by creating a type alias which passes a PhantomData of P,
|
@rodrimati1992 Hmm I don't see how that is better than just implementing trivial |
This is a duplicate of #26925, I suggest closing in favor of that |
Uh oh!
There was an error while loading. Please reload this page.
(Related with #26925)
I have a type
Amount
defined like this (took all irrelevant pieces out):Now, in some unit test, I'm trying to do something like this (again, redacted):
For which rustc gives me the following error:
The text was updated successfully, but these errors were encountered: