]> git.lizzy.rs Git - rust.git/commitdiff
auto merge of #8807 : alexcrichton/rust/remove-two-offsets, r=thestinger
authorbors <bors@rust-lang.org>
Wed, 28 Aug 2013 18:00:41 +0000 (11:00 -0700)
committerbors <bors@rust-lang.org>
Wed, 28 Aug 2013 18:00:41 +0000 (11:00 -0700)
Everything that we do is actually inbounds, so there's no reason for us to be exposing two of these functions

src/librustc/middle/trans/type_use.rs
src/librustc/middle/typeck/check/mod.rs
src/libstd/c_str.rs
src/libstd/ptr.rs
src/libstd/repr.rs
src/libstd/rt/stack.rs
src/libstd/str.rs
src/libstd/unstable/intrinsics.rs
src/libstd/vec.rs

index c67035021a3aab9f282abac4491cfefbd450a661..bf65986b5ba9bd60ba4f9a3f0b60edd0bd8bd77f 100644 (file)
@@ -149,7 +149,7 @@ fn store_type_uses(cx: Context, fn_id: def_id) -> @~[type_uses] {
                     "visit_tydesc"  | "forget" | "frame_address" |
                     "morestack_addr" => 0,
 
-                    "offset" | "offset_inbounds" |
+                    "offset" |
                     "memcpy32" | "memcpy64" | "memmove32" | "memmove64" |
                     "memset32" | "memset64" => use_repr,
 
index 706d6871f8639c69d54ec9309aab268fd4c17344..0b27a581a2aa4bc6cb4fc5bf041c147574b99964 100644 (file)
@@ -3663,20 +3663,6 @@ fn param(ccx: @mut CrateCtxt, n: uint) -> ty::t {
                    mutbl: ast::m_imm
                }))
             }
-            "offset_inbounds" => {
-              (1,
-               ~[
-                  ty::mk_ptr(tcx, ty::mt {
-                      ty: param(ccx, 0),
-                      mutbl: ast::m_imm
-                  }),
-                  ty::mk_int()
-               ],
-               ty::mk_ptr(tcx, ty::mt {
-                   ty: param(ccx, 0),
-                   mutbl: ast::m_imm
-               }))
-            }
             "memcpy32" => {
               (1,
                ~[
index df2fe70ff0e12a9d97abad9682debece3938edcb..70b04e37ee1ff3785de55096c1f9c56fb773073a 100644 (file)
@@ -179,7 +179,7 @@ fn to_c_str(&self) -> CString {
         do cs.with_mut_ref |buf| {
             for i in range(0, self.len()) {
                 unsafe {
-                    let p = buf.offset_inbounds(i as int);
+                    let p = buf.offset(i as int);
                     if *p == 0 {
                         match null_byte::cond.raise(self.to_owned()) {
                             Truncate => break,
@@ -222,7 +222,7 @@ fn next(&mut self) -> Option<libc::c_char> {
         if ch == 0 {
             None
         } else {
-            self.ptr = ptr::offset(self.ptr, 1);
+            self.ptr = unsafe { ptr::offset(self.ptr, 1) };
             Some(ch)
         }
     }
index 860b1f4b7683b704151fc8bb95578ee13c70d374..02469527b7af66a741181950c9e85c7ab0ec4ec6 100644 (file)
 use unstable::intrinsics;
 use util::swap;
 
-#[cfg(not(test))] use ops::{Add,Sub};
-#[cfg(not(test))] use num::Int;
-
 #[cfg(not(test))] use cmp::{Eq, Ord};
 
-/// Calculate the offset from a pointer
+/// Calculate the offset from a pointer. The count *must* be in bounds or
+/// otherwise the loads of this address are undefined.
 #[inline]
 #[cfg(stage0)]
-pub fn offset<T>(ptr: *T, count: int) -> *T {
+pub unsafe fn offset<T>(ptr: *T, count: int) -> *T {
     (ptr as uint + (count as uint) * sys::size_of::<T>()) as *T
 }
 
 /// Calculate the offset from a mut pointer
 #[inline]
 #[cfg(stage0)]
-pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
+pub unsafe fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
     (ptr as uint + (count as uint) * sys::size_of::<T>()) as *mut T
 }
 
 /// Calculate the offset from a pointer
 #[inline]
 #[cfg(not(stage0))]
-pub fn offset<T>(ptr: *T, count: int) -> *T {
-    unsafe { intrinsics::offset(ptr, count) }
+pub unsafe fn offset<T>(ptr: *T, count: int) -> *T {
+    intrinsics::offset(ptr, count)
 }
 
-/// Calculate the offset from a mut pointer
+/// Calculate the offset from a mut pointer. The count *must* be in bounds or
+/// otherwise the loads of this address are undefined.
 #[inline]
 #[cfg(not(stage0))]
-pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
-    unsafe { intrinsics::offset(ptr as *T, count) as *mut T }
+pub unsafe fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
+    intrinsics::offset(ptr as *T, count) as *mut T
 }
 
 /// Return the offset of the first null pointer in `buf`.
@@ -293,8 +292,7 @@ pub trait RawPtr<T> {
     fn is_not_null(&self) -> bool;
     fn to_uint(&self) -> uint;
     unsafe fn to_option(&self) -> Option<&T>;
-    fn offset(&self, count: int) -> Self;
-    unsafe fn offset_inbounds(self, count: int) -> Self;
+    unsafe fn offset(self, count: int) -> Self;
 }
 
 /// Extension methods for immutable pointers
@@ -332,16 +330,10 @@ unsafe fn to_option(&self) -> Option<&T> {
         }
     }
 
-    /// Calculates the offset from a pointer.
-    #[inline]
-    fn offset(&self, count: int) -> *T { offset(*self, count) }
-
     /// Calculates the offset from a pointer. The offset *must* be in-bounds of
     /// the object, or one-byte-past-the-end.
     #[inline]
-    unsafe fn offset_inbounds(self, count: int) -> *T {
-        intrinsics::offset_inbounds(self, count)
-    }
+    unsafe fn offset(self, count: int) -> *T { offset(self, count) }
 }
 
 /// Extension methods for mutable pointers
@@ -379,10 +371,6 @@ unsafe fn to_option(&self) -> Option<&T> {
         }
     }
 
-    /// Calculates the offset from a mutable pointer.
-    #[inline]
-    fn offset(&self, count: int) -> *mut T { mut_offset(*self, count) }
-
     /// Calculates the offset from a pointer. The offset *must* be in-bounds of
     /// the object, or one-byte-past-the-end. An arithmetic overflow is also
     /// undefined behaviour.
@@ -390,9 +378,7 @@ fn offset(&self, count: int) -> *mut T { mut_offset(*self, count) }
     /// This method should be preferred over `offset` when the guarantee can be
     /// satisfied, to enable better optimization.
     #[inline]
-    unsafe fn offset_inbounds(self, count: int) -> *mut T {
-        intrinsics::offset_inbounds(self as *T, count) as *mut T
-    }
+    unsafe fn offset(self, count: int) -> *mut T { mut_offset(self, count) }
 }
 
 // Equality for pointers
@@ -513,46 +499,6 @@ fn gt(&self, other: &*mut T) -> bool {
     }
 }
 
-#[cfg(not(test))]
-impl<T, I: Int> Add<I, *T> for *T {
-    /// Add an integer value to a pointer to get an offset pointer.
-    /// Is calculated according to the size of the type pointed to.
-    #[inline]
-    fn add(&self, rhs: &I) -> *T {
-        self.offset(rhs.to_int() as int)
-    }
-}
-
-#[cfg(not(test))]
-impl<T, I: Int> Sub<I, *T> for *T {
-    /// Subtract an integer value from a pointer to get an offset pointer.
-    /// Is calculated according to the size of the type pointed to.
-    #[inline]
-    fn sub(&self, rhs: &I) -> *T {
-        self.offset(-rhs.to_int() as int)
-    }
-}
-
-#[cfg(not(test))]
-impl<T, I: Int> Add<I, *mut T> for *mut T {
-    /// Add an integer value to a pointer to get an offset pointer.
-    /// Is calculated according to the size of the type pointed to.
-    #[inline]
-    fn add(&self, rhs: &I) -> *mut T {
-        self.offset(rhs.to_int() as int)
-    }
-}
-
-#[cfg(not(test))]
-impl<T, I: Int> Sub<I, *mut T> for *mut T {
-    /// Subtract an integer value from a pointer to get an offset pointer.
-    /// Is calculated according to the size of the type pointed to.
-    #[inline]
-    fn sub(&self, rhs: &I) -> *mut T {
-        self.offset(-rhs.to_int() as int)
-    }
-}
-
 #[cfg(test)]
 pub mod ptr_tests {
     use super::*;
@@ -635,7 +581,7 @@ fn test_is_null() {
         assert!(p.is_null());
         assert!(!p.is_not_null());
 
-        let q = offset(p, 1);
+        let q = unsafe { offset(p, 1) };
         assert!(!q.is_null());
         assert!(q.is_not_null());
 
@@ -643,7 +589,7 @@ fn test_is_null() {
         assert!(mp.is_null());
         assert!(!mp.is_not_null());
 
-        let mq = mp.offset(1);
+        let mq = unsafe { mp.offset(1) };
         assert!(!mq.is_null());
         assert!(mq.is_not_null());
     }
@@ -672,20 +618,20 @@ fn test_ptr_addition() {
         unsafe {
             let xs = ~[5, ..16];
             let mut ptr = to_ptr(xs);
-            let end = ptr + 16;
+            let end = ptr.offset(16);
 
             while ptr < end {
                 assert_eq!(*ptr, 5);
-                ptr = ptr + 1u;
+                ptr = ptr.offset(1);
             }
 
             let mut xs_mut = xs.clone();
             let mut m_ptr = to_mut_ptr(xs_mut);
-            let m_end = m_ptr + 16i16;
+            let m_end = m_ptr.offset(16);
 
             while m_ptr < m_end {
                 *m_ptr += 5;
-                m_ptr = m_ptr + 1u8;
+                m_ptr = m_ptr.offset(1);
             }
 
             assert_eq!(xs_mut, ~[10, ..16]);
@@ -702,17 +648,17 @@ fn test_ptr_subtraction() {
             let ptr = to_ptr(xs);
 
             while idx >= 0i8 {
-                assert_eq!(*(ptr + idx), idx as int);
+                assert_eq!(*(ptr.offset(idx as int)), idx as int);
                 idx = idx - 1i8;
             }
 
             let mut xs_mut = xs.clone();
             let m_start = to_mut_ptr(xs_mut);
-            let mut m_ptr = m_start + 9u32;
+            let mut m_ptr = m_start.offset(9);
 
             while m_ptr >= m_start {
                 *m_ptr += *m_ptr;
-                m_ptr = m_ptr - 1i8;
+                m_ptr = m_ptr.offset(-1);
             }
 
             assert_eq!(xs_mut, ~[0,2,4,6,8,10,12,14,16,18]);
index ad4658d2f42bb1a7bbdb01e6d9dec192790ec5d4..588010bd5b179f14a41a825d46090d055671568a 100644 (file)
@@ -228,7 +228,7 @@ pub fn write_vec_range(&self,
                 self.writer.write_str(", ");
             }
             self.visit_ptr_inner(p as *c_void, inner);
-            p = align(ptr::offset(p, sz as int) as uint, al) as *u8;
+            p = align(unsafe { ptr::offset(p, sz as int) as uint }, al) as *u8;
             left -= dec;
         }
         self.writer.write_char(']');
index 4b2a9b7a6cce40b418df65d081be82ac787a5ab9..da70659acec2edfe577607b7a54b1fa1152cad27 100644 (file)
@@ -46,7 +46,9 @@ pub fn start(&self) -> *uint {
 
     /// Point one word beyond the high end of the allocated stack
     pub fn end(&self) -> *uint {
-        vec::raw::to_ptr(self.buf).offset(self.buf.len() as int) as *uint
+        unsafe {
+            vec::raw::to_ptr(self.buf).offset(self.buf.len() as int) as *uint
+        }
     }
 }
 
index e6cadd04a5b6ea9eb82068df94d20f53336fccd8..9ca6e8ad0899238bd568bc07d8e00faab7c71b60 100644 (file)
@@ -1092,7 +1092,7 @@ pub unsafe fn slice_bytes<'a>(s: &'a str, begin: uint, end: uint) -> &'a str {
     pub unsafe fn slice_unchecked<'a>(s: &'a str, begin: uint, end: uint) -> &'a str {
         do s.as_imm_buf |sbuf, _n| {
              cast::transmute(Slice {
-                 data: sbuf.offset_inbounds(begin as int),
+                 data: sbuf.offset(begin as int),
                  len: end - begin,
              })
         }
index 796567bd561196a3903f67a81b3547af5e63249c..cdd93ce87c35bbdfb14944571782e07cd71a1d06 100644 (file)
@@ -332,19 +332,13 @@ fn visit_leave_fn(&self, purity: uint, proto: uint,
     /// Get the address of the `__morestack` stack growth function.
     pub fn morestack_addr() -> *();
 
-    /// Calculates the offset from a pointer.
-    ///
-    /// This is implemented as an intrinsic to avoid converting to and from an
-    /// integer, since the conversion would throw away aliasing information.
-    pub fn offset<T>(dst: *T, offset: int) -> *T;
-
     /// Calculates the offset from a pointer. The offset *must* be in-bounds of
     /// the object, or one-byte-past-the-end. An arithmetic overflow is also
     /// undefined behaviour.
     ///
-    /// This intrinsic should be preferred over `offset` when the guarantee can
-    /// be satisfied, to enable better optimization.
-    pub fn offset_inbounds<T>(dst: *T, offset: int) -> *T;
+    /// This is implemented as an intrinsic to avoid converting to and from an
+    /// integer, since the conversion would throw away aliasing information.
+    pub fn offset<T>(dst: *T, offset: int) -> *T;
 
     /// Equivalent to the `llvm.memcpy.p0i8.0i8.i32` intrinsic, with a size of
     /// `count` * `size_of::<T>()` and an alignment of `min_align_of::<T>()`
index 8cd1b09468d46c721755f9e5cabc95335351be25..12aebe20161a03f337c7a6a677984d17de73e300 100644 (file)
@@ -896,7 +896,7 @@ fn iter(self) -> VecIterator<'self, T> {
                             lifetime: cast::transmute(p)}
             } else {
                 VecIterator{ptr: p,
-                            end: p.offset_inbounds(self.len() as int),
+                            end: p.offset(self.len() as int),
                             lifetime: cast::transmute(p)}
             }
         }
@@ -1884,7 +1884,7 @@ fn mut_iter(self) -> VecMutIterator<'self, T> {
                                lifetime: cast::transmute(p)}
             } else {
                 VecMutIterator{ptr: p,
-                               end: p.offset_inbounds(self.len() as int),
+                               end: p.offset(self.len() as int),
                                lifetime: cast::transmute(p)}
             }
         }
@@ -2247,7 +2247,7 @@ fn next(&mut self) -> Option<$elem> {
                             // same pointer.
                             cast::transmute(self.ptr as uint + 1)
                         } else {
-                            self.ptr.offset_inbounds(1)
+                            self.ptr.offset(1)
                         };
 
                         Some(cast::transmute(old))
@@ -2279,7 +2279,7 @@ fn next_back(&mut self) -> Option<$elem> {
                             // See above for why 'ptr.offset' isn't used
                             cast::transmute(self.end as uint - 1)
                         } else {
-                            self.end.offset_inbounds(-1)
+                            self.end.offset(-1)
                         };
                         Some(cast::transmute(self.end))
                     }