diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/traits/principal-less-objects.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/test/ui/traits/principal-less-objects.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/test/ui/traits/principal-less-objects.rs b/src/test/ui/traits/principal-less-objects.rs new file mode 100644 index 000000000..62bad0d7d --- /dev/null +++ b/src/test/ui/traits/principal-less-objects.rs @@ -0,0 +1,42 @@ +// run-pass +// Check that trait objects without a principal codegen properly. + +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::mem; + +// Array is to make sure the size is not exactly pointer-size, so +// we can be sure we are measuring the right size in the +// `size_of_val` test. +struct SetOnDrop<'a>(&'a AtomicUsize, #[allow(unused_tuple_struct_fields)] [u8; 64]); +impl<'a> Drop for SetOnDrop<'a> { + fn drop(&mut self) { + self.0.store(self.0.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + } +} + +trait TypeEq<V: ?Sized> {} +impl<T: ?Sized> TypeEq<T> for T {} +fn assert_types_eq<U: ?Sized, V: ?Sized>() where U: TypeEq<V> {} + +fn main() { + // Check that different ways of writing the same type are equal. + assert_types_eq::<dyn Sync, dyn Sync + Sync>(); + assert_types_eq::<dyn Sync + Send, dyn Send + Sync>(); + assert_types_eq::<dyn Sync + Send + Sync, dyn Send + Sync>(); + + // Check that codegen works. + // + // Using `AtomicUsize` here because `Cell<u32>` is not `Sync`, and + // so can't be made into a `Box<dyn Sync>`. + let c = AtomicUsize::new(0); + { + let d: Box<dyn Sync> = Box::new(SetOnDrop(&c, [0; 64])); + + assert_eq!(mem::size_of_val(&*d), + mem::size_of::<SetOnDrop>()); + assert_eq!(mem::align_of_val(&*d), + mem::align_of::<SetOnDrop>()); + assert_eq!(c.load(Ordering::Relaxed), 0); + } + assert_eq!(c.load(Ordering::Relaxed), 1); +} |