]> git.lizzy.rs Git - rust.git/blobdiff - src/tools/miri/tests/fail/unaligned_pointers/reference_to_packed.rs
make unaligned_reference a hard error
[rust.git] / src / tools / miri / tests / fail / unaligned_pointers / reference_to_packed.rs
index a807200771d62c087c51e9035a8fa5d50bc30f31..816b6ab9fb32f307a66270e8c27153da9d2c3bae 100644 (file)
@@ -1,7 +1,9 @@
 // This should fail even without validation/SB
 //@compile-flags: -Zmiri-disable-validation -Zmiri-disable-stacked-borrows
 
-#![allow(dead_code, unused_variables, unaligned_references)]
+#![allow(dead_code, unused_variables)]
+
+use std::{ptr, mem};
 
 #[repr(packed)]
 struct Foo {
@@ -9,11 +11,16 @@ struct Foo {
     y: i32,
 }
 
+unsafe fn raw_to_ref<'a, T>(x: *const T) -> &'a T {
+    mem::transmute(x)
+}
+
 fn main() {
     // Try many times as this might work by chance.
     for _ in 0..20 {
         let foo = Foo { x: 42, y: 99 };
-        let p = &foo.x;
-        let i = *p; //~ERROR: alignment 4 is required
+        // There seem to be implicit reborrows, which make the error already appear here
+        let p: &i32 = unsafe { raw_to_ref(ptr::addr_of!(foo.x)) }; //~ERROR: alignment 4 is required
+        let i = *p;
     }
 }