tests: Avoid transmuting from fn item types.

This commit is contained in:
Eduard Burtescu 2016-02-18 15:19:38 +02:00
parent 644d6390be
commit 61a77b38e2
4 changed files with 43 additions and 51 deletions

View File

@ -25,16 +25,17 @@
#[repr(u32)]
enum Foo {
A = 0,
B = 23
A = 0,
B = 23
}
#[inline(never)]
extern "C" fn foo(_x: usize) -> Foo { Foo::B }
pub fn main() {
unsafe {
let f: extern "C" fn(usize) -> u32 = ::std::mem::transmute(foo);
assert_eq!(f(0xDEADBEEF), Foo::B as u32);
}
unsafe {
let f: extern "C" fn(usize) -> u32 =
::std::mem::transmute(foo as extern "C" fn(usize) -> Foo);
assert_eq!(f(0xDEADBEEF), Foo::B as u32);
}
}

View File

@ -204,48 +204,41 @@ fn t24() -> fn(u8) -> S {
C4
}
fn main(){
unsafe {
assert_eq!(t1()(), regular());
fn main() {
assert_eq!(t1()(), regular());
assert!(::std::mem::transmute::<_, *mut ()>(t2()) ==
::std::mem::transmute::<_, *mut ()>(E::U));
assert!(::std::mem::transmute::<_, *mut ()>(t3()) ==
::std::mem::transmute::<_, *mut ()>(S));
assert_eq!(t2() as *mut (), E::U as *mut ());
assert_eq!(t3() as *mut (), S as *mut ());
assert_eq!(t4()(), S::hey());
let s = S(42);
assert_eq!(t5()(&s), <S as X>::hoy(&s));
assert_eq!(t4()(), S::hey());
let s = S(42);
assert_eq!(t5()(&s), <S as X>::hoy(&s));
assert_eq!(t6()(), ext::regular_fn());
assert!(::std::mem::transmute::<_, *mut ()>(t7()) ==
::std::mem::transmute::<_, *mut ()>(ext::E::U));
assert!(::std::mem::transmute::<_, *mut ()>(t8()) ==
::std::mem::transmute::<_, *mut ()>(ext::S));
assert_eq!(t6()(), ext::regular_fn());
assert_eq!(t7() as *mut (), ext::E::U as *mut ());
assert_eq!(t8() as *mut (), ext::S as *mut ());
assert_eq!(t9()(), ext::S::hey());
let sext = ext::S(6);
assert_eq!(t10()(&sext), <ext::S as ext::X>::hoy(&sext));
assert_eq!(t9()(), ext::S::hey());
let sext = ext::S(6);
assert_eq!(t10()(&sext), <ext::S as ext::X>::hoy(&sext));
let p = parametric::<u8>;
assert!(::std::mem::transmute::<_, *mut ()>(t11()) ==
::std::mem::transmute::<_, *mut ()>(p));
let p = parametric::<u8>;
assert_eq!(t11() as *mut (), p as *mut ());
assert_eq!(t12(), C);
assert_eq!(t13(), C2);
assert_eq!(t13_2(), C3);
assert_eq!(t12(), C);
assert_eq!(t13(), C2);
assert_eq!(t13_2(), C3);
assert_eq!(t14()(), <S as X>::hoy2());
assert_eq!(t15()(&s), S::hey2(&s));
assert_eq!(t16()(10u32, 20u32), F::f(10u32, 20u32));
assert_eq!(t17()(30u32, 10u64), F::f(30u32, 10u64));
assert_eq!(t18()(50u64, 5u64), F::f(50u64, 5u64));
assert_eq!(t19()(322u64, 2u32), F::f(322u64, 2u32));
assert_eq!(t20()(123u64, 38u32), <u32 as T<_, _>>::staticmeth(123, 38));
assert_eq!(t21(), Unit);
assert_eq!(t22(), None);
assert_eq!(t23(), (CEnum::A, CEnum::B));
assert_eq!(t24(), C4);
}
assert_eq!(t14()(), <S as X>::hoy2());
assert_eq!(t15()(&s), S::hey2(&s));
assert_eq!(t16()(10u32, 20u32), F::f(10u32, 20u32));
assert_eq!(t17()(30u32, 10u64), F::f(30u32, 10u64));
assert_eq!(t18()(50u64, 5u64), F::f(50u64, 5u64));
assert_eq!(t19()(322u64, 2u32), F::f(322u64, 2u32));
assert_eq!(t20()(123u64, 38u32), <u32 as T<_, _>>::staticmeth(123, 38));
assert_eq!(t21(), Unit);
assert_eq!(t22(), None);
assert_eq!(t23(), (CEnum::A, CEnum::B));
assert_eq!(t24(), C4);
}

View File

@ -24,13 +24,14 @@
use std::mem;
#[inline(never)]
extern "C" fn foo<'a>(x: &'a isize) -> Option<&'a isize> { Some(x) }
extern "C" fn foo(x: &isize) -> Option<&isize> { Some(x) }
static FOO: isize = 0xDEADBEE;
pub fn main() {
unsafe {
let f: for<'a> extern "C" fn(&'a isize) -> &'a isize = mem::transmute(foo);
let f: extern "C" fn(&isize) -> &isize =
mem::transmute(foo as extern "C" fn(&isize) -> Option<&isize>);
assert_eq!(*f(&FOO), FOO);
}
}

View File

@ -12,8 +12,6 @@
#![allow(unknown_features)]
#![feature(box_syntax)]
use std::{option, mem};
// Iota-reduction is a rule in the Calculus of (Co-)Inductive Constructions,
// which "says that a destructor applied to an object built from a constructor
// behaves as expected". -- http://coq.inria.fr/doc/Reference-Manual006.html
@ -43,9 +41,9 @@ macro_rules! check_option {
check_option!($e, $T, |ptr| assert_eq!(*ptr, $e));
}};
($e:expr, $T:ty, |$v:ident| $chk:expr) => {{
assert!(option::Option::None::<$T>.is_none());
assert!(None::<$T>.is_none());
let e = $e;
let s_ = option::Option::Some::<$T>(e);
let s_ = Some::<$T>(e);
let $v = s_.as_ref().unwrap();
$chk
}}
@ -78,9 +76,8 @@ pub fn main() {
check_type!(&17, &isize);
check_type!(box 18, Box<isize>);
check_type!("foo".to_string(), String);
check_type!(vec!(20, 22), Vec<isize> );
let mint: usize = unsafe { mem::transmute(main) };
check_type!(vec!(20, 22), Vec<isize>);
check_type!(main, fn(), |pthing| {
assert_eq!(mint, unsafe { mem::transmute(*pthing) })
assert_eq!(main as fn(), *pthing as fn())
});
}