]> git.lizzy.rs Git - rust.git/blob - tests/ui/traits/principal-less-objects.rs
Rollup merge of #105526 - Xiretza:iter-from-generator-derive, r=scottmcm
[rust.git] / tests / ui / traits / principal-less-objects.rs
1 // run-pass
2 // Check that trait objects without a principal codegen properly.
3
4 use std::sync::atomic::{AtomicUsize, Ordering};
5 use std::mem;
6
7 // Array is to make sure the size is not exactly pointer-size, so
8 // we can be sure we are measuring the right size in the
9 // `size_of_val` test.
10 struct SetOnDrop<'a>(&'a AtomicUsize, #[allow(unused_tuple_struct_fields)] [u8; 64]);
11 impl<'a> Drop for SetOnDrop<'a> {
12     fn drop(&mut self) {
13         self.0.store(self.0.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
14     }
15 }
16
17 trait TypeEq<V: ?Sized> {}
18 impl<T: ?Sized> TypeEq<T> for T {}
19 fn assert_types_eq<U: ?Sized, V: ?Sized>() where U: TypeEq<V> {}
20
21 fn main() {
22     // Check that different ways of writing the same type are equal.
23     assert_types_eq::<dyn Sync, dyn Sync + Sync>();
24     assert_types_eq::<dyn Sync + Send, dyn Send + Sync>();
25     assert_types_eq::<dyn Sync + Send + Sync, dyn Send + Sync>();
26
27     // Check that codegen works.
28     //
29     // Using `AtomicUsize` here because `Cell<u32>` is not `Sync`, and
30     // so can't be made into a `Box<dyn Sync>`.
31     let c = AtomicUsize::new(0);
32     {
33         let d: Box<dyn Sync> = Box::new(SetOnDrop(&c, [0; 64]));
34
35         assert_eq!(mem::size_of_val(&*d),
36                    mem::size_of::<SetOnDrop>());
37         assert_eq!(mem::align_of_val(&*d),
38                    mem::align_of::<SetOnDrop>());
39         assert_eq!(c.load(Ordering::Relaxed), 0);
40     }
41     assert_eq!(c.load(Ordering::Relaxed), 1);
42 }