about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2019-06-23 20:08:55 +0200
committerRalf Jung <post@ralfj.de>2019-06-23 20:08:55 +0200
commit9954af4a9f76bd324a731b61feffb684a5572ce8 (patch)
tree580f0ca8306fa69d896b0070d009fdcb3e6dee13 /src
parent9492038ae9fe1c559d890bd0f41dfe5f37030a03 (diff)
downloadrust-9954af4a9f76bd324a731b61feffb684a5572ce8.tar.gz
rust-9954af4a9f76bd324a731b61feffb684a5572ce8.zip
deduplicate some code
Diffstat (limited to 'src')
-rw-r--r--src/librustc_mir/interpret/memory.rs32
1 files changed, 15 insertions, 17 deletions
diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs
index f9ab661ea87..8b72c367e9f 100644
--- a/src/librustc_mir/interpret/memory.rs
+++ b/src/librustc_mir/interpret/memory.rs
@@ -274,6 +274,19 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
         size: Size,
         align: Align,
     ) -> InterpResult<'tcx, Option<Pointer<M::PointerTag>>> {
+        fn check_offset_align(offset: u64, align: Align) -> InterpResult<'static> {
+            if offset % align.bytes() == 0 {
+                Ok(())
+            } else {
+                // The biggest power of two through which `offset` is divisible.
+                let offset_pow2 = 1 << offset.trailing_zeros();
+                err!(AlignmentCheckFailed {
+                    has: Align::from_bytes(offset_pow2).unwrap(),
+                    required: align,
+                })
+            }
+        }
+
         // Normalize to a `Pointer` if we definitely need one.
         let normalized = if size.bytes() == 0 {
             // Can be an integer, just take what we got.
@@ -290,14 +303,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
                 if bits == 0 {
                     return err!(InvalidNullPointerUsage);
                 }
-                if bits % align.bytes() != 0 {
-                    // The biggest power of two through which `bits` is divisible.
-                    let bits_pow2 = 1 << bits.trailing_zeros();
-                    return err!(AlignmentCheckFailed {
-                        has: Align::from_bytes(bits_pow2).unwrap(),
-                        required: align,
-                    });
-                }
+                check_offset_align(bits, align)?;
                 None
             }
             Err(ptr) => {
@@ -321,15 +327,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
                         required: align,
                     });
                 }
-                let offset = ptr.offset.bytes();
-                if offset % align.bytes() != 0 {
-                    // The biggest power of two through which `offset` is divisible.
-                    let bits_pow2 = 1 << offset.trailing_zeros();
-                    return err!(AlignmentCheckFailed {
-                        has: Align::from_bytes(bits_pow2).unwrap(),
-                        required: align,
-                    })
-                }
+                check_offset_align(ptr.offset.bytes(), align)?;
 
                 // We can still be zero-sized in this branch, in which case we have to
                 // return `None`.