auto merge of #7426 : thestinger/rust/zero-size-noncopyable, r=catamorphism

4885918 r=huonw
42a63fc r=thestinger
7ec5a08 r=catamorphism
fb1e5f1 r=thestinger
659cd55 r=cmr
This commit is contained in:
bors 2013-06-28 05:28:32 -07:00
commit 811e045c60
14 changed files with 302 additions and 195 deletions

View file

@ -82,8 +82,14 @@ pub mod reader {
use core::cast::transmute;
use core::int;
use core::io;
use core::ptr::offset;
use core::str;
#[cfg(target_arch = "x86")]
#[cfg(target_arch = "x86_64")]
use core::ptr::offset;
#[cfg(target_arch = "x86")]
#[cfg(target_arch = "x86_64")]
use core::unstable::intrinsics::bswap32;
// ebml reading

View file

@ -36,6 +36,7 @@ struct RcBox<T> {
/// Immutable reference counted pointer type
#[non_owned]
#[unsafe_no_drop_flag]
pub struct Rc<T> {
priv ptr: *mut RcBox<T>,
}
@ -168,6 +169,7 @@ struct RcMutBox<T> {
/// Mutable reference counted pointer type
#[non_owned]
#[mutable]
#[unsafe_no_drop_flag]
pub struct RcMut<T> {
priv ptr: *mut RcMutBox<T>,
}

View file

@ -3904,7 +3904,7 @@ impl DtorKind {
pub fn ty_dtor(cx: ctxt, struct_id: def_id) -> DtorKind {
match cx.destructor_for_type.find(&struct_id) {
Some(&method_def_id) => {
let flag = !has_attr(cx, struct_id, "no_drop_flag");
let flag = !has_attr(cx, struct_id, "unsafe_no_drop_flag");
TraitDtor(method_def_id, flag)
}

View file

@ -349,7 +349,6 @@ pub mod types {
use libc::types::os::arch::c95::{c_uchar, c_uint, c_ulong, time_t};
use libc::types::os::arch::c99::{c_longlong, c_ulonglong};
use libc::types::os::arch::posix88::{uid_t, gid_t, ino_t};
use libc::types::os::arch::posix88::{uid_t};
pub type nlink_t = u16;
pub type blksize_t = u32;

View file

@ -447,7 +447,7 @@ fn test_option_dance() {
}
#[test] #[should_fail] #[ignore(cfg(windows))]
fn test_option_too_much_dance() {
let mut y = Some(util::NonCopyable::new());
let mut y = Some(util::NonCopyable);
let _y2 = y.swap_unwrap();
let _y3 = y.swap_unwrap();
}

View file

@ -62,7 +62,7 @@ pub struct AtomicPtr<T> {
/**
* An owned atomic pointer. Ensures that only a single reference to the data is held at any time.
*/
#[no_drop_flag]
#[unsafe_no_drop_flag]
pub struct AtomicOption<T> {
priv p: *mut c_void
}

View file

@ -75,18 +75,14 @@ pub fn replace<T>(dest: &mut T, mut src: T) -> T {
}
/// A non-copyable dummy type.
#[deriving(Eq, TotalEq, Ord, TotalOrd)]
#[unsafe_no_drop_flag]
pub struct NonCopyable;
impl NonCopyable {
/// Creates a dummy non-copyable structure and returns it for use.
pub fn new() -> NonCopyable { NonCopyable }
}
impl Drop for NonCopyable {
fn drop(&self) { }
}
/// A type with no inhabitants
pub enum Void { }
@ -130,39 +126,73 @@ pub fn unreachable() -> ! {
#[cfg(test)]
mod tests {
use super::*;
use option::{None, Some};
use util::{Void, NonCopyable, id, replace, swap};
use either::{Either, Left, Right};
use sys::size_of;
use kinds::Drop;
#[test]
pub fn identity_crisis() {
fn identity_crisis() {
// Writing a test for the identity function. How did it come to this?
let x = ~[(5, false)];
//FIXME #3387 assert!(x.eq(id(copy x)));
let y = copy x;
assert!(x.eq(&id(y)));
}
#[test]
pub fn test_swap() {
fn test_swap() {
let mut x = 31337;
let mut y = 42;
swap(&mut x, &mut y);
assert_eq!(x, 42);
assert_eq!(y, 31337);
}
#[test]
pub fn test_replace() {
let mut x = Some(NonCopyable::new());
fn test_replace() {
let mut x = Some(NonCopyable);
let y = replace(&mut x, None);
assert!(x.is_none());
assert!(y.is_some());
}
#[test]
pub fn test_uninhabited() {
fn test_uninhabited() {
let could_only_be_coin : Either <Void, ()> = Right (());
match could_only_be_coin {
Right (coin) => coin,
Left (is_void) => is_void.uninhabited ()
}
}
#[test]
fn test_noncopyable() {
assert_eq!(size_of::<NonCopyable>(), 0);
// verify that `#[unsafe_no_drop_flag]` works as intended on a zero-size struct
// NOTE: uncomment after snapshot, will not parse yet
//static mut did_run: bool = false;
struct Foo { five: int }
impl Drop for Foo {
fn drop(&self) {
assert_eq!(self.five, 5);
// NOTE: uncomment after snapshot, will not parse yet
//unsafe {
//did_run = true;
//}
}
}
{
let _a = (NonCopyable, Foo { five: 5 }, NonCopyable);
}
// NOTE: uncomment after snapshot, will not parse yet
//unsafe { assert_eq!(did_run, true); }
}
}

View file

@ -10,7 +10,7 @@
use std::sys::size_of;
#[no_drop_flag]
#[unsafe_no_drop_flag]
struct Test<T> {
a: T
}