]> git.lizzy.rs Git - rust.git/commitdiff
Add intptrcast test for explicit casts
authorChristian Poveda <christianpoveda@protonmail.com>
Sat, 29 Jun 2019 18:17:15 +0000 (13:17 -0500)
committerChristian Poveda <cpovedar@fnal.gov>
Wed, 3 Jul 2019 18:38:39 +0000 (13:38 -0500)
tests/run-pass-noseed/intptrcast.rs

index 40b21f9a4729ee1510dd15dd1998d94be14de046..919083f98d3dfb24135f0dcfd20b2159b55ce61a 100644 (file)
@@ -1,4 +1,7 @@
 // compile-flags: -Zmiri-seed=0000000000000000
+fn transmute_ptr_to_int<T>(x: *const T) -> usize { 
+    unsafe { std::mem::transmute::<*const T, usize>(x) * 1 }
+}
 
 fn main() {
     // Some casting-to-int with arithmetic.
@@ -11,4 +14,11 @@ fn main() {
     // 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 _);
+
+    // 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);
 }