]> git.lizzy.rs Git - rust.git/blob - tests/ui/deriving/deriving-copyclone.rs
Rollup merge of #106898 - estebank:ice-forms-are-a-headache, r=Mark-Simulacrum
[rust.git] / tests / ui / deriving / deriving-copyclone.rs
1 // run-pass
2 //! Test that #[derive(Copy, Clone)] produces a shallow copy
3 //! even when a member violates RFC 1521
4
5 use std::sync::atomic::{AtomicBool, Ordering};
6
7 /// A struct that pretends to be Copy, but actually does something
8 /// in its Clone impl
9 #[derive(Copy)]
10 struct Liar;
11
12 /// Static cooperating with the rogue Clone impl
13 static CLONED: AtomicBool = AtomicBool::new(false);
14
15 impl Clone for Liar {
16     fn clone(&self) -> Self {
17         // this makes Clone vs Copy observable
18         CLONED.store(true, Ordering::SeqCst);
19
20         *self
21     }
22 }
23
24 /// This struct is actually Copy... at least, it thinks it is!
25 #[derive(Copy, Clone)]
26 struct Innocent(#[allow(unused_tuple_struct_fields)] Liar);
27
28 impl Innocent {
29     fn new() -> Self {
30         Innocent(Liar)
31     }
32 }
33
34 fn main() {
35     let _ = Innocent::new().clone();
36     // if Innocent was byte-for-byte copied, CLONED will still be false
37     assert!(!CLONED.load(Ordering::SeqCst));
38 }