]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_middle/src/mir/interpret/pointer.rs
add method to get absolute address of a pointer (useful only for Miri)
[rust.git] / compiler / rustc_middle / src / mir / interpret / pointer.rs
index 813c0912f539679db80f57d1ec455580c0ccb1de..4461f4e0568738deccbc4c32c00b134b8debf47f 100644 (file)
@@ -163,6 +163,9 @@ pub struct Pointer<Tag = AllocId> {
 }
 
 static_assert_size!(Pointer, 16);
+// `Option<Tag>` pointers are also passed around quite a bit
+// (but not stored in permanent machine state).
+static_assert_size!(Pointer<Option<AllocId>>, 16);
 
 // We want the `Debug` output to be readable as it is used by `derive(Debug)` for
 // all the Miri types.
@@ -204,6 +207,16 @@ pub fn into_pointer_or_addr(self) -> Result<Pointer<Tag>, Size> {
             None => Err(self.offset),
         }
     }
+
+    /// Returns the absolute address the pointer points to.
+    /// Only works if Tag::OFFSET_IS_ADDR is true!
+    pub fn addr(self) -> Size
+    where
+        Tag: Provenance,
+    {
+        assert!(Tag::OFFSET_IS_ADDR);
+        self.offset
+    }
 }
 
 impl<Tag> Pointer<Option<Tag>> {