about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAria Beingessner <a.beingessner@gmail.com>2022-03-26 17:03:40 -0400
committerAria Beingessner <a.beingessner@gmail.com>2022-03-29 20:18:27 -0400
commit7514d760b8fabd46e4874520b653ab8803c3517e (patch)
treead06f9326522bc789d650376a836f1d49fbfeb83
parent31e1cde4b5bd70dfe892f5e7bc449ad3a6e1d773 (diff)
downloadrust-7514d760b8fabd46e4874520b653ab8803c3517e.tar.gz
rust-7514d760b8fabd46e4874520b653ab8803c3517e.zip
cleanup some of the less terrifying library code
-rw-r--r--library/alloc/src/slice.rs4
-rw-r--r--library/core/src/slice/ascii.rs6
-rw-r--r--library/core/src/slice/iter/macros.rs2
-rw-r--r--library/std/src/io/error/repr_bitpacked.rs2
4 files changed, 7 insertions, 7 deletions
diff --git a/library/alloc/src/slice.rs b/library/alloc/src/slice.rs
index 7c892f03bfb..31365562ddb 100644
--- a/library/alloc/src/slice.rs
+++ b/library/alloc/src/slice.rs
@@ -1043,9 +1043,9 @@ where
 
     impl<T> Drop for MergeHole<T> {
         fn drop(&mut self) {
-            // `T` is not a zero-sized type, so it's okay to divide by its size.
-            let len = (self.end.addr() - self.start.addr()) / mem::size_of::<T>();
+            // `T` is not a zero-sized type, and these are pointers into a slice's elements.
             unsafe {
+                let len = self.end.offset_from(self.start) as usize;
                 ptr::copy_nonoverlapping(self.start, self.dest, len);
             }
         }
diff --git a/library/core/src/slice/ascii.rs b/library/core/src/slice/ascii.rs
index 63d761d3c02..6c9107401fd 100644
--- a/library/core/src/slice/ascii.rs
+++ b/library/core/src/slice/ascii.rs
@@ -294,7 +294,7 @@ fn is_ascii(s: &[u8]) -> bool {
     // Paranoia check about alignment, since we're about to do a bunch of
     // unaligned loads. In practice this should be impossible barring a bug in
     // `align_offset` though.
-    debug_assert_eq!((word_ptr.addr()) % mem::align_of::<usize>(), 0);
+    debug_assert_eq!(word_ptr.addr() % mem::align_of::<usize>(), 0);
 
     // Read subsequent words until the last aligned word, excluding the last
     // aligned word by itself to be done in tail check later, to ensure that
@@ -302,9 +302,9 @@ fn is_ascii(s: &[u8]) -> bool {
     while byte_pos < len - USIZE_SIZE {
         debug_assert!(
             // Sanity check that the read is in bounds
-            (word_ptr.addr() + USIZE_SIZE) <= (start.wrapping_add(len).addr()) &&
+            (word_ptr.addr() + USIZE_SIZE) <= start.addr().wrapping_add(len) &&
             // And that our assumptions about `byte_pos` hold.
-            (word_ptr.addr()) - (start.addr()) == byte_pos
+            (word_ptr.addr() - start.addr()) == byte_pos
         );
 
         // SAFETY: We know `word_ptr` is properly aligned (because of
diff --git a/library/core/src/slice/iter/macros.rs b/library/core/src/slice/iter/macros.rs
index 96ead49dd6a..b74ab28fc09 100644
--- a/library/core/src/slice/iter/macros.rs
+++ b/library/core/src/slice/iter/macros.rs
@@ -20,7 +20,7 @@ macro_rules! len {
         if size == 0 {
             // This _cannot_ use `unchecked_sub` because we depend on wrapping
             // to represent the length of long ZST slice iterators.
-            ($self.end.addr()).wrapping_sub(start.as_ptr().addr())
+            $self.end.addr().wrapping_sub(start.as_ptr().addr())
         } else {
             // We know that `start <= end`, so can do better than `offset_from`,
             // which needs to deal in signed.  By setting appropriate flags here
diff --git a/library/std/src/io/error/repr_bitpacked.rs b/library/std/src/io/error/repr_bitpacked.rs
index 7cc1c701064..e80068b46ab 100644
--- a/library/std/src/io/error/repr_bitpacked.rs
+++ b/library/std/src/io/error/repr_bitpacked.rs
@@ -136,7 +136,7 @@ impl Repr {
         let p = Box::into_raw(b).cast::<u8>();
         // Should only be possible if an allocator handed out a pointer with
         // wrong alignment.
-        debug_assert_eq!((p.addr() & TAG_MASK), 0);
+        debug_assert_eq!(p.addr() & TAG_MASK, 0);
         // Note: We know `TAG_CUSTOM <= size_of::<Custom>()` (static_assert at
         // end of file), and both the start and end of the expression must be
         // valid without address space wraparound due to `Box`'s semantics.