rust/tests/run-pass-noseed/intptrcast.rs

27 lines
845 B
Rust
Raw Normal View History

2019-06-22 09:25:16 -05:00
// compile-flags: -Zmiri-seed=0000000000000000
2019-07-03 13:42:01 -05:00
// This returns a miri pointer at type usize, if the argument is a proper pointer
fn transmute_ptr_to_int<T>(x: *const T) -> usize {
unsafe { std::mem::transmute(x) }
2019-06-29 13:17:15 -05:00
}
2019-06-22 09:25:16 -05:00
fn main() {
2019-06-28 13:42:00 -05:00
// Some casting-to-int with arithmetic.
2019-07-03 13:42:01 -05:00
let x = &42 as *const i32 as usize;
2019-06-22 09:25:16 -05:00
let y = x * 2;
2019-06-24 21:47:37 -05:00
assert_eq!(y, x + x);
2019-06-22 09:25:16 -05:00
let z = y as u8 as usize;
assert_eq!(z, y % 256);
2019-06-28 13:42:00 -05:00
// Pointer string formatting! We can't check the output as it changes when libstd changes,
// but we can make sure Miri does not error.
format!("{:?}", &mut 13 as *mut _);
2019-06-29 13:17:15 -05:00
// Check that intptrcast is triggered for explicit casts and that it is consistent with
// transmuting.
let a: *const i32 = &42;
let b = transmute_ptr_to_int(a) as u8;
let c = a as usize as u8;
assert_eq!(b, c);
2019-06-22 09:25:16 -05:00
}