Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arielb1 committed Jan 3, 2019
1 parent 4ab27b2 commit c2aa748
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/test/run-pass/traits/principal-less-trait-objects.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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, [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);
}

0 comments on commit c2aa748

Please sign in to comment.