2018-08-30 12:18:55 +00:00
|
|
|
//@ run-pass
|
shallow Clone for #[derive(Copy,Clone)]
Changes #[derive(Copy, Clone)] to use a faster impl of Clone when
both derives are present, and there are no generics in the type.
The faster impl is simply returning *self (which works because the
type is also Copy). See the comments in libsyntax_ext/deriving/clone.rs
for more details.
There are a few types which are Copy but not Clone, in violation
of the definition of Copy. These include large arrays and tuples. The
very existence of these types is arguably a bug, but in order for this
optimization not to change the applicability of #[derive(Copy, Clone)],
the faster Clone impl also injects calls to a new function,
core::clone::assert_receiver_is_clone, to verify that all members are
actually Clone.
This is not a breaking change, because pursuant to RFC 1521, any type
that implements Copy should not do any observable work in its Clone
impl.
2016-02-04 00:40:59 +00:00
|
|
|
//! Test that #[derive(Copy, Clone)] produces a shallow copy
|
|
|
|
//! even when a member violates RFC 1521
|
|
|
|
|
2019-01-26 16:14:49 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
shallow Clone for #[derive(Copy,Clone)]
Changes #[derive(Copy, Clone)] to use a faster impl of Clone when
both derives are present, and there are no generics in the type.
The faster impl is simply returning *self (which works because the
type is also Copy). See the comments in libsyntax_ext/deriving/clone.rs
for more details.
There are a few types which are Copy but not Clone, in violation
of the definition of Copy. These include large arrays and tuples. The
very existence of these types is arguably a bug, but in order for this
optimization not to change the applicability of #[derive(Copy, Clone)],
the faster Clone impl also injects calls to a new function,
core::clone::assert_receiver_is_clone, to verify that all members are
actually Clone.
This is not a breaking change, because pursuant to RFC 1521, any type
that implements Copy should not do any observable work in its Clone
impl.
2016-02-04 00:40:59 +00:00
|
|
|
|
|
|
|
/// A struct that pretends to be Copy, but actually does something
|
|
|
|
/// in its Clone impl
|
|
|
|
#[derive(Copy)]
|
|
|
|
struct Liar;
|
|
|
|
|
|
|
|
/// Static cooperating with the rogue Clone impl
|
2019-01-26 16:14:49 +00:00
|
|
|
static CLONED: AtomicBool = AtomicBool::new(false);
|
shallow Clone for #[derive(Copy,Clone)]
Changes #[derive(Copy, Clone)] to use a faster impl of Clone when
both derives are present, and there are no generics in the type.
The faster impl is simply returning *self (which works because the
type is also Copy). See the comments in libsyntax_ext/deriving/clone.rs
for more details.
There are a few types which are Copy but not Clone, in violation
of the definition of Copy. These include large arrays and tuples. The
very existence of these types is arguably a bug, but in order for this
optimization not to change the applicability of #[derive(Copy, Clone)],
the faster Clone impl also injects calls to a new function,
core::clone::assert_receiver_is_clone, to verify that all members are
actually Clone.
This is not a breaking change, because pursuant to RFC 1521, any type
that implements Copy should not do any observable work in its Clone
impl.
2016-02-04 00:40:59 +00:00
|
|
|
|
|
|
|
impl Clone for Liar {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
// this makes Clone vs Copy observable
|
|
|
|
CLONED.store(true, Ordering::SeqCst);
|
|
|
|
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This struct is actually Copy... at least, it thinks it is!
|
|
|
|
#[derive(Copy, Clone)]
|
2023-12-27 22:11:58 +00:00
|
|
|
struct Innocent(#[allow(dead_code)] Liar);
|
shallow Clone for #[derive(Copy,Clone)]
Changes #[derive(Copy, Clone)] to use a faster impl of Clone when
both derives are present, and there are no generics in the type.
The faster impl is simply returning *self (which works because the
type is also Copy). See the comments in libsyntax_ext/deriving/clone.rs
for more details.
There are a few types which are Copy but not Clone, in violation
of the definition of Copy. These include large arrays and tuples. The
very existence of these types is arguably a bug, but in order for this
optimization not to change the applicability of #[derive(Copy, Clone)],
the faster Clone impl also injects calls to a new function,
core::clone::assert_receiver_is_clone, to verify that all members are
actually Clone.
This is not a breaking change, because pursuant to RFC 1521, any type
that implements Copy should not do any observable work in its Clone
impl.
2016-02-04 00:40:59 +00:00
|
|
|
|
|
|
|
impl Innocent {
|
|
|
|
fn new() -> Self {
|
|
|
|
Innocent(Liar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let _ = Innocent::new().clone();
|
|
|
|
// if Innocent was byte-for-byte copied, CLONED will still be false
|
|
|
|
assert!(!CLONED.load(Ordering::SeqCst));
|
|
|
|
}
|