]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/intptrcast.rs
update miri-seed handling for run-pass test suite
[rust.git] / tests / run-pass / intptrcast.rs
1 // This returns a miri pointer at type usize, if the argument is a proper pointer
2 fn transmute_ptr_to_int<T>(x: *const T) -> usize {
3     unsafe { std::mem::transmute(x) }
4 }
5
6 fn main() {
7     // Some casting-to-int with arithmetic.
8     let x = &42 as *const i32 as usize;
9     let y = x * 2;
10     assert_eq!(y, x + x);
11     let z = y as u8 as usize;
12     assert_eq!(z, y % 256);
13
14     // Pointer string formatting! We can't check the output as it changes when libstd changes,
15     // but we can make sure Miri does not error.
16     format!("{:?}", &mut 13 as *mut _);
17
18     // Check that intptrcast is triggered for explicit casts and that it is consistent with
19     // transmuting.
20     let a: *const i32 = &42;
21     let b = transmute_ptr_to_int(a) as u8;
22     let c = a as usize as u8;
23     assert_eq!(b, c);
24 }