From e3662b188041b50c732896ded3de823144ae6556 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 8 Aug 2013 22:22:52 -0700 Subject: [PATCH] Remove offset_inbounds for an unsafe offset function --- src/librustc/middle/trans/type_use.rs | 2 +- src/librustc/middle/typeck/check/mod.rs | 14 ---- src/libstd/c_str.rs | 4 +- src/libstd/ptr.rs | 98 ++++++------------------- src/libstd/repr.rs | 2 +- src/libstd/rt/stack.rs | 4 +- src/libstd/str.rs | 2 +- src/libstd/unstable/intrinsics.rs | 12 +-- src/libstd/vec.rs | 8 +- 9 files changed, 37 insertions(+), 109 deletions(-) diff --git a/src/librustc/middle/trans/type_use.rs b/src/librustc/middle/trans/type_use.rs index c67035021a3..bf65986b5ba 100644 --- a/src/librustc/middle/trans/type_use.rs +++ b/src/librustc/middle/trans/type_use.rs @@ -149,7 +149,7 @@ pub fn type_uses_for(ccx: @mut CrateContext, fn_id: def_id, n_tps: uint) "visit_tydesc" | "forget" | "frame_address" | "morestack_addr" => 0, - "offset" | "offset_inbounds" | + "offset" | "memcpy32" | "memcpy64" | "memmove32" | "memmove64" | "memset32" | "memset64" => use_repr, diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index 706d6871f86..0b27a581a2a 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -3663,20 +3663,6 @@ pub fn check_intrinsic_type(ccx: @mut CrateCtxt, it: @ast::foreign_item) { 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, ~[ diff --git a/src/libstd/c_str.rs b/src/libstd/c_str.rs index df2fe70ff0e..70b04e37ee1 100644 --- a/src/libstd/c_str.rs +++ b/src/libstd/c_str.rs @@ -179,7 +179,7 @@ impl<'self> ToCStr for &'self [u8] { 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 @@ impl<'self> Iterator for CStringIterator<'self> { if ch == 0 { None } else { - self.ptr = ptr::offset(self.ptr, 1); + self.ptr = unsafe { ptr::offset(self.ptr, 1) }; Some(ch) } } diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs index 860b1f4b768..02469527b7a 100644 --- a/src/libstd/ptr.rs +++ b/src/libstd/ptr.rs @@ -20,37 +20,36 @@ use sys; 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(ptr: *T, count: int) -> *T { +pub unsafe fn offset(ptr: *T, count: int) -> *T { (ptr as uint + (count as uint) * sys::size_of::()) as *T } /// Calculate the offset from a mut pointer #[inline] #[cfg(stage0)] -pub fn mut_offset(ptr: *mut T, count: int) -> *mut T { +pub unsafe fn mut_offset(ptr: *mut T, count: int) -> *mut T { (ptr as uint + (count as uint) * sys::size_of::()) as *mut T } /// Calculate the offset from a pointer #[inline] #[cfg(not(stage0))] -pub fn offset(ptr: *T, count: int) -> *T { - unsafe { intrinsics::offset(ptr, count) } +pub unsafe fn offset(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(ptr: *mut T, count: int) -> *mut T { - unsafe { intrinsics::offset(ptr as *T, count) as *mut T } +pub unsafe fn mut_offset(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 { 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 @@ impl RawPtr for *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 @@ impl RawPtr for *mut 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 @@ impl RawPtr for *mut T { /// 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 @@ impl Ord for *mut T { } } -#[cfg(not(test))] -impl Add 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 Sub 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 Add 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 Sub 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 @@ pub mod ptr_tests { 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 @@ pub mod ptr_tests { 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 @@ pub mod ptr_tests { 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 @@ pub mod ptr_tests { 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]); diff --git a/src/libstd/repr.rs b/src/libstd/repr.rs index ad4658d2f42..588010bd5b1 100644 --- a/src/libstd/repr.rs +++ b/src/libstd/repr.rs @@ -228,7 +228,7 @@ impl ReprVisitor { 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(']'); diff --git a/src/libstd/rt/stack.rs b/src/libstd/rt/stack.rs index 4b2a9b7a6cc..da70659acec 100644 --- a/src/libstd/rt/stack.rs +++ b/src/libstd/rt/stack.rs @@ -46,7 +46,9 @@ impl StackSegment { /// 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 + } } } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index e6cadd04a5b..9ca6e8ad089 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -1092,7 +1092,7 @@ pub mod raw { 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, }) } diff --git a/src/libstd/unstable/intrinsics.rs b/src/libstd/unstable/intrinsics.rs index 796567bd561..cdd93ce87c3 100644 --- a/src/libstd/unstable/intrinsics.rs +++ b/src/libstd/unstable/intrinsics.rs @@ -332,19 +332,13 @@ extern "rust-intrinsic" { /// 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(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(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(dst: *T, offset: int) -> *T; /// Equivalent to the `llvm.memcpy.p0i8.0i8.i32` intrinsic, with a size of /// `count` * `size_of::()` and an alignment of `min_align_of::()` diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 8cd1b09468d..12aebe20161 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -896,7 +896,7 @@ impl<'self,T> ImmutableVector<'self, T> for &'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 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [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 @@ macro_rules! iterator { // 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 @@ macro_rules! double_ended_iterator { // 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)) }