diff options
Diffstat (limited to 'src/test')
21 files changed, 398 insertions, 60 deletions
diff --git a/src/test/compile-fail/associated-types-incomplete-object.rs b/src/test/compile-fail/associated-types-incomplete-object.rs index 1c708da30a7..c1feb8ac459 100644 --- a/src/test/compile-fail/associated-types-incomplete-object.rs +++ b/src/test/compile-fail/associated-types-incomplete-object.rs @@ -28,15 +28,15 @@ impl Foo for isize { } pub fn main() { - let a = &42 as &Foo<A=usize, B=char>; + let a = &42isize as &Foo<A=usize, B=char>; - let b = &42 as &Foo<A=usize>; + let b = &42isize as &Foo<A=usize>; //~^ ERROR the value of the associated type `B` (from the trait `Foo`) must be specified - let c = &42 as &Foo<B=char>; + let c = &42isize as &Foo<B=char>; //~^ ERROR the value of the associated type `A` (from the trait `Foo`) must be specified - let d = &42 as &Foo; + let d = &42isize as &Foo; //~^ ERROR the value of the associated type `A` (from the trait `Foo`) must be specified //~| ERROR the value of the associated type `B` (from the trait `Foo`) must be specified } diff --git a/src/test/compile-fail/cast-ptr-to-int-const.rs b/src/test/compile-fail/cast-ptr-to-int-const.rs new file mode 100644 index 00000000000..7c32e31d23b --- /dev/null +++ b/src/test/compile-fail/cast-ptr-to-int-const.rs @@ -0,0 +1,15 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + const X: u32 = main as u32; //~ ERROR E0018 + const Y: u32 = 0; + const Z: u32 = &Y as *const u32 as u32; //~ ERROR E0018 +} diff --git a/src/test/compile-fail/cast-rfc0401.rs b/src/test/compile-fail/cast-rfc0401.rs new file mode 100644 index 00000000000..f3537e54135 --- /dev/null +++ b/src/test/compile-fail/cast-rfc0401.rs @@ -0,0 +1,75 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn illegal_cast<U:?Sized,V:?Sized>(u: *const U) -> *const V +{ + u as *const V //~ ERROR vtable kinds +} + +fn illegal_cast_2<U:?Sized>(u: *const U) -> *const str +{ + u as *const str //~ ERROR vtable kinds +} + +trait Foo { fn foo(&self) {} } +impl<T> Foo for T {} + +enum E { + A, B +} + +fn main() +{ + let f: f32 = 1.2; + let v = 0 as *const u8; + let fat_v : *const [u8] = unsafe { &*(0 as *const [u8; 1])}; + let foo: &Foo = &f; + + let _ = v as &u8; //~ ERROR non-scalar + let _ = v as E; //~ ERROR non-scalar + let _ = v as fn(); //~ ERROR non-scalar + let _ = v as (u32,); //~ ERROR non-scalar + let _ = Some(&v) as *const u8; //~ ERROR non-scalar + + let _ = v as f32; //~ ERROR through a usize first + let _ = main as f64; //~ ERROR through a usize first + let _ = &v as usize; //~ ERROR through a raw pointer first + let _ = f as *const u8; //~ ERROR through a usize first + let _ = 3 as bool; //~ ERROR compare with zero + let _ = E::A as bool; //~ ERROR compare with zero + let _ = 0x61u32 as char; //~ ERROR only `u8` can be cast + + let _ = false as f32; //~ ERROR through an integer first + let _ = E::A as f32; //~ ERROR through an integer first + let _ = 'a' as f32; //~ ERROR through an integer first + + let _ = false as *const u8; //~ ERROR through a usize first + let _ = E::A as *const u8; //~ ERROR through a usize first + let _ = 'a' as *const u8; //~ ERROR through a usize first + + let _ = 42usize as *const [u8]; //~ ERROR illegal cast + let _ = v as *const [u8]; //~ ERROR illegal cast + let _ = fat_v as *const Foo; + //~^ ERROR `core::marker::Sized` is not implemented for the type `[u8]` + let _ = foo as *const str; //~ ERROR illegal cast + let _ = foo as *mut str; //~ ERROR illegal cast + let _ = main as *mut str; //~ ERROR illegal cast + let _ = &f as *mut f32; //~ ERROR illegal cast + let _ = &f as *const f64; //~ ERROR illegal cast + let _ = fat_v as usize; //~ ERROR through a raw pointer first + + let a : *const str = "hello"; + let _ = a as *const Foo; + //~^ ERROR `core::marker::Sized` is not implemented for the type `str` + + // check no error cascade + let _ = main.f as *const u32; //~ ERROR attempted access of field + +} diff --git a/src/test/compile-fail/cast-to-bare-fn.rs b/src/test/compile-fail/cast-to-bare-fn.rs index a7f0917ed86..7cc5c727bc7 100644 --- a/src/test/compile-fail/cast-to-bare-fn.rs +++ b/src/test/compile-fail/cast-to-bare-fn.rs @@ -13,7 +13,7 @@ fn foo(_x: isize) { } fn main() { let v: u64 = 5; let x = foo as extern "C" fn() -> isize; - //~^ ERROR mismatched types + //~^ ERROR non-scalar cast let y = v as extern "Rust" fn(isize) -> (isize, isize); //~^ ERROR non-scalar cast y(x()); diff --git a/src/test/compile-fail/const-cast-different-types.rs b/src/test/compile-fail/const-cast-different-types.rs index 6e3732908ac..e6851f02cb6 100644 --- a/src/test/compile-fail/const-cast-different-types.rs +++ b/src/test/compile-fail/const-cast-different-types.rs @@ -9,18 +9,8 @@ // except according to those terms. static a: &'static str = "foo"; -static b: *const u8 = a as *const u8; -//~^ ERROR mismatched types -//~| expected *const u8 -//~| found &'static str -//~| expected u8 -//~| found str -static c: *const u8 = &a as *const u8; -//~^ ERROR mismatched types -//~| expected *const u8 -//~| found &&'static str -//~| expected u8 -//~| found &-ptr +static b: *const u8 = a as *const u8; //~ ERROR illegal cast +static c: *const u8 = &a as *const u8; //~ ERROR illegal cast fn main() { } diff --git a/src/test/compile-fail/destructure-trait-ref.rs b/src/test/compile-fail/destructure-trait-ref.rs index 3f455e148a0..08db643df00 100644 --- a/src/test/compile-fail/destructure-trait-ref.rs +++ b/src/test/compile-fail/destructure-trait-ref.rs @@ -28,29 +28,29 @@ fn main() { // if n > m, it's a type mismatch error. // n < m - let &x = &(&1 as &T); - let &x = &&(&1 as &T); - let &&x = &&(&1 as &T); + let &x = &(&1isize as &T); + let &x = &&(&1isize as &T); + let &&x = &&(&1isize as &T); // n == m - let &x = &1 as &T; //~ ERROR type `&T` cannot be dereferenced - let &&x = &(&1 as &T); //~ ERROR type `&T` cannot be dereferenced - let box x = box 1 as Box<T>; //~ ERROR the trait `core::marker::Sized` is not implemented + let &x = &1isize as &T; //~ ERROR type `&T` cannot be dereferenced + let &&x = &(&1isize as &T); //~ ERROR type `&T` cannot be dereferenced + let box x = box 1isize as Box<T>; //~ ERROR the trait `core::marker::Sized` is not implemented // n > m - let &&x = &1 as &T; + let &&x = &1isize as &T; //~^ ERROR mismatched types //~| expected `T` //~| found `&_` //~| expected trait T //~| found &-ptr - let &&&x = &(&1 as &T); + let &&&x = &(&1isize as &T); //~^ ERROR mismatched types //~| expected `T` //~| found `&_` //~| expected trait T //~| found &-ptr - let box box x = box 1 as Box<T>; + let box box x = box 1isize as Box<T>; //~^ ERROR mismatched types //~| expected `T` //~| found `Box<_>` diff --git a/src/test/compile-fail/fat-ptr-cast.rs b/src/test/compile-fail/fat-ptr-cast.rs index 2099424b05c..25cab09b7cb 100644 --- a/src/test/compile-fail/fat-ptr-cast.rs +++ b/src/test/compile-fail/fat-ptr-cast.rs @@ -8,20 +8,23 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// Make sure casts between thin pointer <-> fat pointer are illegal. - -pub trait Trait {} +trait Trait {} +// Make sure casts between thin-pointer <-> fat pointer obey RFC401 fn main() { let a: &[i32] = &[1, 2, 3]; let b: Box<[i32]> = Box::new([1, 2, 3]); + let p = a as *const [i32]; + let q = a.as_ptr(); - a as usize; //~ ERROR non-scalar cast + a as usize; //~ ERROR illegal cast b as usize; //~ ERROR non-scalar cast + p as usize; //~ ERROR illegal cast; cast through a raw pointer - let a: usize = 42; - a as *const [i32]; //~ ERROR cast to fat pointer: `usize` as `*const [i32]` + // #22955 + q as *const [i32]; //~ ERROR illegal cast - let a: *const u8 = &42; - a as *const [u8]; //~ ERROR cast to fat pointer: `*const u8` as `*const [u8]` + // #21397 + let t: *mut (Trait + 'static) = 0 as *mut _; //~ ERROR illegal cast + let mut fail: *const str = 0 as *const str; //~ ERROR illegal cast } diff --git a/src/test/compile-fail/issue-14845.rs b/src/test/compile-fail/issue-14845.rs index 3f994102a17..219f08ad35a 100644 --- a/src/test/compile-fail/issue-14845.rs +++ b/src/test/compile-fail/issue-14845.rs @@ -15,18 +15,8 @@ struct X { fn main() { let x = X { a: [0] }; - let _f = &x.a as *mut u8; - //~^ ERROR mismatched types - //~| expected `*mut u8` - //~| found `&[u8; 1]` - //~| expected u8 - //~| found array of 1 elements + let _f = &x.a as *mut u8; //~ ERROR illegal cast let local: [u8; 1] = [0]; - let _v = &local as *mut u8; - //~^ ERROR mismatched types - //~| expected `*mut u8` - //~| found `&[u8; 1]` - //~| expected u8, - //~| found array of 1 elements + let _v = &local as *mut u8; //~ ERROR illegal cast } diff --git a/src/test/compile-fail/issue-17444.rs b/src/test/compile-fail/issue-17444.rs index 011a9c17776..a079161d42e 100644 --- a/src/test/compile-fail/issue-17444.rs +++ b/src/test/compile-fail/issue-17444.rs @@ -14,5 +14,5 @@ enum Test { fn main() { let _x = Test::Foo as *const isize; - //~^ ERROR illegal cast; cast through an integer first: `Test` as `*const isize` + //~^ ERROR illegal cast; cast through a usize first: `Test` as `*const isize` } diff --git a/src/test/compile-fail/issue-17458.rs b/src/test/compile-fail/issue-17458.rs index d6f70ae1e57..a3a9e17cb3c 100644 --- a/src/test/compile-fail/issue-17458.rs +++ b/src/test/compile-fail/issue-17458.rs @@ -9,7 +9,7 @@ // except according to those terms. static X: usize = 0 as *const usize as usize; -//~^ ERROR: can not cast a pointer to an integer in statics +//~^ ERROR: can't cast a pointer to an integer in statics fn main() { assert_eq!(X, 0); diff --git a/src/test/compile-fail/issue-21554.rs b/src/test/compile-fail/issue-21554.rs index a2cac55033c..16ce84715b1 100644 --- a/src/test/compile-fail/issue-21554.rs +++ b/src/test/compile-fail/issue-21554.rs @@ -11,5 +11,5 @@ struct Inches(i32); fn main() { - Inches as f32; //~ ERROR illegal cast; cast through an integer first + Inches as f32; //~ ERROR illegal cast; cast through a usize first } diff --git a/src/test/compile-fail/issue-22289.rs b/src/test/compile-fail/issue-22289.rs index 1fdc8735714..bcbc414d353 100644 --- a/src/test/compile-fail/issue-22289.rs +++ b/src/test/compile-fail/issue-22289.rs @@ -9,5 +9,5 @@ // except according to those terms. fn main() { - 0 as &std::any::Any; //~ ERROR cast to fat pointer: `i32` as `&core::any::Any` + 0 as &std::any::Any; //~ ERROR non-scalar cast } diff --git a/src/test/compile-fail/issue-5153.rs b/src/test/compile-fail/issue-5153.rs index da32408e199..b1d96f9b527 100644 --- a/src/test/compile-fail/issue-5153.rs +++ b/src/test/compile-fail/issue-5153.rs @@ -17,6 +17,6 @@ impl Foo for isize { } fn main() { - (&5 as &Foo).foo(); + (&5isize as &Foo).foo(); //~^ ERROR: no method named `foo` found for type `&Foo` in the current scope } diff --git a/src/test/compile-fail/kindck-impl-type-params.rs b/src/test/compile-fail/kindck-impl-type-params.rs index d4ee93e9ca5..988a7837b59 100644 --- a/src/test/compile-fail/kindck-impl-type-params.rs +++ b/src/test/compile-fail/kindck-impl-type-params.rs @@ -28,7 +28,6 @@ fn f<T>(val: T) { let a = &t as &Gettable<T>; //~^ ERROR the trait `core::marker::Send` is not implemented //~^^ ERROR the trait `core::marker::Copy` is not implemented - //~^^^ ERROR the parameter type `T` may not live long enough } fn g<T>(val: T) { diff --git a/src/test/compile-fail/typeck-cast-pointer-to-float.rs b/src/test/compile-fail/typeck-cast-pointer-to-float.rs index 285a5dbee05..e10a76c65bc 100644 --- a/src/test/compile-fail/typeck-cast-pointer-to-float.rs +++ b/src/test/compile-fail/typeck-cast-pointer-to-float.rs @@ -11,5 +11,5 @@ fn main() { let x : i16 = 22; ((&x) as *const i16) as f32; - //~^ ERROR illegal cast; cast through an integer first: `*const i16` as `f32` + //~^ ERROR illegal cast; cast through a usize first: `*const i16` as `f32` } diff --git a/src/test/compile-fail/vector-cast-weirdness.rs b/src/test/compile-fail/vector-cast-weirdness.rs index cac52306d6a..10227f1820d 100644 --- a/src/test/compile-fail/vector-cast-weirdness.rs +++ b/src/test/compile-fail/vector-cast-weirdness.rs @@ -28,7 +28,7 @@ fn main() { let mut x1 = X { y: [0, 0] }; // This is still an error since we don't allow casts from &mut [T; n] to *mut T. - let p1: *mut u8 = &mut x1.y as *mut _; //~ ERROR mismatched types + let p1: *mut u8 = &mut x1.y as *mut _; //~ ERROR illegal cast let t1: *mut [u8; 2] = &mut x1.y as *mut _; let h1: *mut [u8; 2] = &mut x1.y as *mut [u8; 2]; } diff --git a/src/test/run-pass-valgrind/cast-enum-with-dtor.rs b/src/test/run-pass-valgrind/cast-enum-with-dtor.rs new file mode 100644 index 00000000000..0bc1e33ce46 --- /dev/null +++ b/src/test/run-pass-valgrind/cast-enum-with-dtor.rs @@ -0,0 +1,44 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(dead_code)] + +// check dtor calling order when casting enums. + +use std::sync::atomic; +use std::sync::atomic::Ordering; +use std::mem; + +enum E { + A = 0, + B = 1, + C = 2 +} + +static FLAG: atomic::AtomicUsize = atomic::ATOMIC_USIZE_INIT; + +impl Drop for E { + fn drop(&mut self) { + // avoid dtor loop + unsafe { mem::forget(mem::replace(self, E::B)) }; + + FLAG.store(FLAG.load(Ordering::SeqCst)+1, Ordering::SeqCst); + } +} + +fn main() { + assert_eq!(FLAG.load(Ordering::SeqCst), 0); + { + let e = E::C; + assert_eq!(e as u32, 2); + assert_eq!(FLAG.load(Ordering::SeqCst), 0); + } + assert_eq!(FLAG.load(Ordering::SeqCst), 1); +} diff --git a/src/test/run-pass/cast-rfc0401-vtable-kinds.rs b/src/test/run-pass/cast-rfc0401-vtable-kinds.rs new file mode 100644 index 00000000000..e53d4af8e36 --- /dev/null +++ b/src/test/run-pass/cast-rfc0401-vtable-kinds.rs @@ -0,0 +1,55 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Check that you can cast between different pointers to trait objects +// whose vtable have the same kind (both lengths, or both trait pointers). + +trait Foo<T> { + fn foo(&self, _: T) -> u32 { 42 } +} + +trait Bar { + fn bar(&self) { println!("Bar!"); } +} + +impl<T> Foo<T> for () {} +impl Foo<u32> for u32 { fn foo(&self, _: u32) -> u32 { self+43 } } +impl Bar for () {} + +unsafe fn fool<'a>(t: *const (Foo<u32>+'a)) -> u32 { + let bar : *const Bar = t as *const Bar; + let foo_e : *const Foo<u16> = t as *const _; + let r_1 = foo_e as *mut Foo<u32>; + + (&*r_1).foo(0)*(&*(bar as *const Foo<u32>)).foo(0) +} + +#[repr(C)] +struct FooS<T:?Sized>(T); +#[repr(C)] +struct BarS<T:?Sized>(T); + +fn foo_to_bar<T:?Sized>(u: *const FooS<T>) -> *const BarS<T> { + u as *const BarS<T> +} + +fn main() { + let x = 4u32; + let y : &Foo<u32> = &x; + let fl = unsafe { fool(y as *const Foo<u32>) }; + assert_eq!(fl, (43+4)*(43+4)); + + let s = FooS([0,1,2]); + let u: &FooS<[u32]> = &s; + let u: *const FooS<[u32]> = u; + let bar_ref : *const BarS<[u32]> = foo_to_bar(u); + let z : &BarS<[u32]> = unsafe{&*bar_ref}; + assert_eq!(&z.0, &[0,1,2]); +} diff --git a/src/test/run-pass/cast-rfc0401.rs b/src/test/run-pass/cast-rfc0401.rs new file mode 100644 index 00000000000..7c64c34fae5 --- /dev/null +++ b/src/test/run-pass/cast-rfc0401.rs @@ -0,0 +1,170 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(dead_code)] + +use std::vec; + +enum Simple { + A, + B, + C +} + +enum Valued { + H8=163, + Z=0, + X=256, + H7=67, +} + +enum ValuedSigned { + M1=-1, + P1=1 +} + +fn main() +{ + // coercion-cast + let mut it = vec![137].into_iter(); + let itr: &mut vec::IntoIter<u32> = &mut it; + assert_eq!((itr as &mut Iterator<Item=u32>).next(), Some(137)); + assert_eq!((itr as &mut Iterator<Item=u32>).next(), None); + + assert_eq!(Some(4u32) as Option<u32>, Some(4u32)); + assert_eq!((1u32,2u32) as (u32,u32), (1,2)); + + // this isn't prim-int-cast. Check that it works. + assert_eq!(false as bool, false); + assert_eq!(true as bool, true); + + // numeric-cast + let l: u64 = 0x8090a0b0c0d0e0f0; + let lsz: usize = l as usize; + assert_eq!(l as u32, 0xc0d0e0f0); + + // numeric-cast + assert_eq!(l as u8, 0xf0); + assert_eq!(l as i8,-0x10); + assert_eq!(l as u32, 0xc0d0e0f0); + assert_eq!(l as u32 as usize as u32, l as u32); + assert_eq!(l as i32,-0x3f2f1f10); + assert_eq!(l as i32 as isize as i32, l as i32); + assert_eq!(l as i64,-0x7f6f5f4f3f2f1f10); + + assert_eq!(0 as f64, 0f64); + assert_eq!(1 as f64, 1f64); + + assert_eq!(l as f64, 9264081114510712022f64); + + assert_eq!(l as i64 as f64, -9182662959198838444f64); +// float overflow : needs fixing +// assert_eq!(l as f32 as i64 as u64, 9264082620822882088u64); +// assert_eq!(l as i64 as f32 as i64, 9182664080220408446i64); + + assert_eq!(4294967040f32 as u32, 0xffffff00u32); + assert_eq!(1.844674407370955e19f64 as u64, 0xfffffffffffff800u64); + + assert_eq!(9.223372036854775e18f64 as i64, 0x7ffffffffffffc00i64); + assert_eq!(-9.223372036854776e18f64 as i64, 0x8000000000000000u64 as i64); + + // addr-ptr-cast/ptr-addr-cast (thin ptr) + let p: *const [u8; 1] = lsz as *const [u8; 1]; + assert_eq!(p as usize, lsz); + + // ptr-ptr-cast (thin ptr) + let w: *const () = p as *const (); + assert_eq!(w as usize, lsz); + + // ptr-ptr-cast (fat->thin) + let u: *const [u8] = unsafe{&*p}; + assert_eq!(u as *const u8, p as *const u8); + assert_eq!(u as *const u16, p as *const u16); + + // ptr-ptr-cast (both vk=Length) + let mut l : [u8; 2] = [0,1]; + let w: *mut [u16; 2] = &mut l as *mut [u8; 2] as *mut _; + let w: *mut [u16] = unsafe {&mut *w}; + let w_u8 : *const [u8] = w as *const [u8]; + assert_eq!(unsafe{&*w_u8}, &l); + + let s: *mut str = w as *mut str; + let l_via_str = unsafe{&*(s as *const [u8])}; + assert_eq!(&l, l_via_str); + + // enum-cast + assert_eq!(Simple::A as u8, 0); + assert_eq!(Simple::B as u8, 1); + + assert_eq!(Valued::H8 as i8, -93); + assert_eq!(Valued::H7 as i8, 67); + assert_eq!(Valued::Z as i8, 0); + + assert_eq!(Valued::H8 as u8, 163); + assert_eq!(Valued::H7 as u8, 67); + assert_eq!(Valued::Z as u8, 0); + + assert_eq!(Valued::H8 as u16, 163); + assert_eq!(Valued::Z as u16, 0); + assert_eq!(Valued::H8 as u16, 163); + assert_eq!(Valued::Z as u16, 0); + + assert_eq!(ValuedSigned::M1 as u16, 65535); + assert_eq!(ValuedSigned::M1 as i16, -1); + assert_eq!(ValuedSigned::P1 as u16, 1); + assert_eq!(ValuedSigned::P1 as i16, 1); + + // prim-int-cast + assert_eq!(false as u16, 0); + assert_eq!(true as u16, 1); + assert_eq!(false as i64, 0); + assert_eq!(true as i64, 1); + assert_eq!('a' as u32, 0x61); + assert_eq!('a' as u16, 0x61); + assert_eq!('a' as u8, 0x61); + assert_eq!('א' as u8, 0xd0); + assert_eq!('א' as u16, 0x5d0); + assert_eq!('א' as u32, 0x5d0); + assert_eq!('🐵' as u8, 0x35); + assert_eq!('🐵' as u16, 0xf435); + assert_eq!('🐵' as u32, 0x1f435); + assert_eq!('英' as i16, -0x7d0f); + assert_eq!('英' as u16, 0x82f1); + + // u8-char-cast + assert_eq!(0x61 as char, 'a'); + assert_eq!(0u8 as char, '\0'); + assert_eq!(0xd7 as char, '×'); + + // array-ptr-cast + let x = [1,2,3]; + let first : *const u32 = &x[0]; + + assert_eq!(first, &x as *const _); + assert_eq!(first, &x as *const u32); + + // fptr-addr-cast + fn foo() { + println!("foo!"); + } + fn bar() { + println!("bar!"); + } + + assert!(foo as usize != bar as usize); + + assert_eq!(foo as i16, foo as usize as i16); + + // fptr-ptr-cast + + assert_eq!(foo as *const u8 as usize, foo as usize); + assert!(foo as *const u32 != first); +} +fn foo() { } diff --git a/src/test/run-pass/fat-ptr-cast.rs b/src/test/run-pass/fat-ptr-cast.rs index b7513da99c8..91637d111fe 100644 --- a/src/test/run-pass/fat-ptr-cast.rs +++ b/src/test/run-pass/fat-ptr-cast.rs @@ -32,13 +32,12 @@ fn main() { // Test conversion to an address (usize). let a: *const [i32; 3] = &[1, 2, 3]; let b: *const [i32] = a; - assert!(a as usize == b as usize); + assert!(a as usize == b as *const () as usize); // And conversion to a void pointer/address for trait objects too. let a: *mut Foo = &mut Bar; let b = a as *mut (); - let c = a as usize; - + let c = a as *const () as usize; let d = unsafe { let r: raw::TraitObject = mem::transmute(a); r.data @@ -46,4 +45,5 @@ fn main() { assert!(b == d); assert!(c == d as usize); + } diff --git a/src/test/run-pass/supported-cast.rs b/src/test/run-pass/supported-cast.rs index 811b9dce4bc..a47ae52f590 100644 --- a/src/test/run-pass/supported-cast.rs +++ b/src/test/run-pass/supported-cast.rs @@ -181,7 +181,6 @@ pub fn main() { println!("{:?}", true as isize); println!("{:?}", true as usize); - println!("{:?}", true as *const libc::FILE); println!("{:?}", true as i8); println!("{:?}", true as i16); println!("{:?}", true as i32); @@ -190,8 +189,6 @@ pub fn main() { println!("{:?}", true as u16); println!("{:?}", true as u32); println!("{:?}", true as u64); - println!("{:?}", true as f32); - println!("{:?}", true as f64); println!("{:?}", 1f32 as isize); println!("{:?}", 1f32 as usize); |
