Skip to content

Commit 64ed372

Browse files
committed
auto merge of #8807 : alexcrichton/rust/remove-two-offsets, r=thestinger
Everything that we do is actually inbounds, so there's no reason for us to be exposing two of these functions
2 parents 9708ef0 + e3662b1 commit 64ed372

File tree

9 files changed

+37
-109
lines changed

9 files changed

+37
-109
lines changed

src/librustc/middle/trans/type_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub fn type_uses_for(ccx: @mut CrateContext, fn_id: def_id, n_tps: uint)
149149
"visit_tydesc" | "forget" | "frame_address" |
150150
"morestack_addr" => 0,
151151

152-
"offset" | "offset_inbounds" |
152+
"offset" |
153153
"memcpy32" | "memcpy64" | "memmove32" | "memmove64" |
154154
"memset32" | "memset64" => use_repr,
155155

src/librustc/middle/typeck/check/mod.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3663,20 +3663,6 @@ pub fn check_intrinsic_type(ccx: @mut CrateCtxt, it: @ast::foreign_item) {
36633663
mutbl: ast::m_imm
36643664
}))
36653665
}
3666-
"offset_inbounds" => {
3667-
(1,
3668-
~[
3669-
ty::mk_ptr(tcx, ty::mt {
3670-
ty: param(ccx, 0),
3671-
mutbl: ast::m_imm
3672-
}),
3673-
ty::mk_int()
3674-
],
3675-
ty::mk_ptr(tcx, ty::mt {
3676-
ty: param(ccx, 0),
3677-
mutbl: ast::m_imm
3678-
}))
3679-
}
36803666
"memcpy32" => {
36813667
(1,
36823668
~[

src/libstd/c_str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<'self> ToCStr for &'self [u8] {
179179
do cs.with_mut_ref |buf| {
180180
for i in range(0, self.len()) {
181181
unsafe {
182-
let p = buf.offset_inbounds(i as int);
182+
let p = buf.offset(i as int);
183183
if *p == 0 {
184184
match null_byte::cond.raise(self.to_owned()) {
185185
Truncate => break,
@@ -222,7 +222,7 @@ impl<'self> Iterator<libc::c_char> for CStringIterator<'self> {
222222
if ch == 0 {
223223
None
224224
} else {
225-
self.ptr = ptr::offset(self.ptr, 1);
225+
self.ptr = unsafe { ptr::offset(self.ptr, 1) };
226226
Some(ch)
227227
}
228228
}

src/libstd/ptr.rs

Lines changed: 22 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,36 @@ use sys;
2020
use unstable::intrinsics;
2121
use util::swap;
2222

23-
#[cfg(not(test))] use ops::{Add,Sub};
24-
#[cfg(not(test))] use num::Int;
25-
2623
#[cfg(not(test))] use cmp::{Eq, Ord};
2724

28-
/// Calculate the offset from a pointer
25+
/// Calculate the offset from a pointer. The count *must* be in bounds or
26+
/// otherwise the loads of this address are undefined.
2927
#[inline]
3028
#[cfg(stage0)]
31-
pub fn offset<T>(ptr: *T, count: int) -> *T {
29+
pub unsafe fn offset<T>(ptr: *T, count: int) -> *T {
3230
(ptr as uint + (count as uint) * sys::size_of::<T>()) as *T
3331
}
3432

3533
/// Calculate the offset from a mut pointer
3634
#[inline]
3735
#[cfg(stage0)]
38-
pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
36+
pub unsafe fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
3937
(ptr as uint + (count as uint) * sys::size_of::<T>()) as *mut T
4038
}
4139

4240
/// Calculate the offset from a pointer
4341
#[inline]
4442
#[cfg(not(stage0))]
45-
pub fn offset<T>(ptr: *T, count: int) -> *T {
46-
unsafe { intrinsics::offset(ptr, count) }
43+
pub unsafe fn offset<T>(ptr: *T, count: int) -> *T {
44+
intrinsics::offset(ptr, count)
4745
}
4846

49-
/// Calculate the offset from a mut pointer
47+
/// Calculate the offset from a mut pointer. The count *must* be in bounds or
48+
/// otherwise the loads of this address are undefined.
5049
#[inline]
5150
#[cfg(not(stage0))]
52-
pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
53-
unsafe { intrinsics::offset(ptr as *T, count) as *mut T }
51+
pub unsafe fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
52+
intrinsics::offset(ptr as *T, count) as *mut T
5453
}
5554

5655
/// Return the offset of the first null pointer in `buf`.
@@ -293,8 +292,7 @@ pub trait RawPtr<T> {
293292
fn is_not_null(&self) -> bool;
294293
fn to_uint(&self) -> uint;
295294
unsafe fn to_option(&self) -> Option<&T>;
296-
fn offset(&self, count: int) -> Self;
297-
unsafe fn offset_inbounds(self, count: int) -> Self;
295+
unsafe fn offset(self, count: int) -> Self;
298296
}
299297

300298
/// Extension methods for immutable pointers
@@ -332,16 +330,10 @@ impl<T> RawPtr<T> for *T {
332330
}
333331
}
334332

335-
/// Calculates the offset from a pointer.
336-
#[inline]
337-
fn offset(&self, count: int) -> *T { offset(*self, count) }
338-
339333
/// Calculates the offset from a pointer. The offset *must* be in-bounds of
340334
/// the object, or one-byte-past-the-end.
341335
#[inline]
342-
unsafe fn offset_inbounds(self, count: int) -> *T {
343-
intrinsics::offset_inbounds(self, count)
344-
}
336+
unsafe fn offset(self, count: int) -> *T { offset(self, count) }
345337
}
346338

347339
/// Extension methods for mutable pointers
@@ -379,20 +371,14 @@ impl<T> RawPtr<T> for *mut T {
379371
}
380372
}
381373

382-
/// Calculates the offset from a mutable pointer.
383-
#[inline]
384-
fn offset(&self, count: int) -> *mut T { mut_offset(*self, count) }
385-
386374
/// Calculates the offset from a pointer. The offset *must* be in-bounds of
387375
/// the object, or one-byte-past-the-end. An arithmetic overflow is also
388376
/// undefined behaviour.
389377
///
390378
/// This method should be preferred over `offset` when the guarantee can be
391379
/// satisfied, to enable better optimization.
392380
#[inline]
393-
unsafe fn offset_inbounds(self, count: int) -> *mut T {
394-
intrinsics::offset_inbounds(self as *T, count) as *mut T
395-
}
381+
unsafe fn offset(self, count: int) -> *mut T { mut_offset(self, count) }
396382
}
397383

398384
// Equality for pointers
@@ -513,46 +499,6 @@ impl<T> Ord for *mut T {
513499
}
514500
}
515501

516-
#[cfg(not(test))]
517-
impl<T, I: Int> Add<I, *T> for *T {
518-
/// Add an integer value to a pointer to get an offset pointer.
519-
/// Is calculated according to the size of the type pointed to.
520-
#[inline]
521-
fn add(&self, rhs: &I) -> *T {
522-
self.offset(rhs.to_int() as int)
523-
}
524-
}
525-
526-
#[cfg(not(test))]
527-
impl<T, I: Int> Sub<I, *T> for *T {
528-
/// Subtract an integer value from a pointer to get an offset pointer.
529-
/// Is calculated according to the size of the type pointed to.
530-
#[inline]
531-
fn sub(&self, rhs: &I) -> *T {
532-
self.offset(-rhs.to_int() as int)
533-
}
534-
}
535-
536-
#[cfg(not(test))]
537-
impl<T, I: Int> Add<I, *mut T> for *mut T {
538-
/// Add an integer value to a pointer to get an offset pointer.
539-
/// Is calculated according to the size of the type pointed to.
540-
#[inline]
541-
fn add(&self, rhs: &I) -> *mut T {
542-
self.offset(rhs.to_int() as int)
543-
}
544-
}
545-
546-
#[cfg(not(test))]
547-
impl<T, I: Int> Sub<I, *mut T> for *mut T {
548-
/// Subtract an integer value from a pointer to get an offset pointer.
549-
/// Is calculated according to the size of the type pointed to.
550-
#[inline]
551-
fn sub(&self, rhs: &I) -> *mut T {
552-
self.offset(-rhs.to_int() as int)
553-
}
554-
}
555-
556502
#[cfg(test)]
557503
pub mod ptr_tests {
558504
use super::*;
@@ -635,15 +581,15 @@ pub mod ptr_tests {
635581
assert!(p.is_null());
636582
assert!(!p.is_not_null());
637583

638-
let q = offset(p, 1);
584+
let q = unsafe { offset(p, 1) };
639585
assert!(!q.is_null());
640586
assert!(q.is_not_null());
641587

642588
let mp: *mut int = mut_null();
643589
assert!(mp.is_null());
644590
assert!(!mp.is_not_null());
645591

646-
let mq = mp.offset(1);
592+
let mq = unsafe { mp.offset(1) };
647593
assert!(!mq.is_null());
648594
assert!(mq.is_not_null());
649595
}
@@ -672,20 +618,20 @@ pub mod ptr_tests {
672618
unsafe {
673619
let xs = ~[5, ..16];
674620
let mut ptr = to_ptr(xs);
675-
let end = ptr + 16;
621+
let end = ptr.offset(16);
676622

677623
while ptr < end {
678624
assert_eq!(*ptr, 5);
679-
ptr = ptr + 1u;
625+
ptr = ptr.offset(1);
680626
}
681627

682628
let mut xs_mut = xs.clone();
683629
let mut m_ptr = to_mut_ptr(xs_mut);
684-
let m_end = m_ptr + 16i16;
630+
let m_end = m_ptr.offset(16);
685631

686632
while m_ptr < m_end {
687633
*m_ptr += 5;
688-
m_ptr = m_ptr + 1u8;
634+
m_ptr = m_ptr.offset(1);
689635
}
690636

691637
assert_eq!(xs_mut, ~[10, ..16]);
@@ -702,17 +648,17 @@ pub mod ptr_tests {
702648
let ptr = to_ptr(xs);
703649

704650
while idx >= 0i8 {
705-
assert_eq!(*(ptr + idx), idx as int);
651+
assert_eq!(*(ptr.offset(idx as int)), idx as int);
706652
idx = idx - 1i8;
707653
}
708654

709655
let mut xs_mut = xs.clone();
710656
let m_start = to_mut_ptr(xs_mut);
711-
let mut m_ptr = m_start + 9u32;
657+
let mut m_ptr = m_start.offset(9);
712658

713659
while m_ptr >= m_start {
714660
*m_ptr += *m_ptr;
715-
m_ptr = m_ptr - 1i8;
661+
m_ptr = m_ptr.offset(-1);
716662
}
717663

718664
assert_eq!(xs_mut, ~[0,2,4,6,8,10,12,14,16,18]);

src/libstd/repr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl ReprVisitor {
228228
self.writer.write_str(", ");
229229
}
230230
self.visit_ptr_inner(p as *c_void, inner);
231-
p = align(ptr::offset(p, sz as int) as uint, al) as *u8;
231+
p = align(unsafe { ptr::offset(p, sz as int) as uint }, al) as *u8;
232232
left -= dec;
233233
}
234234
self.writer.write_char(']');

src/libstd/rt/stack.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ impl StackSegment {
4646

4747
/// Point one word beyond the high end of the allocated stack
4848
pub fn end(&self) -> *uint {
49-
vec::raw::to_ptr(self.buf).offset(self.buf.len() as int) as *uint
49+
unsafe {
50+
vec::raw::to_ptr(self.buf).offset(self.buf.len() as int) as *uint
51+
}
5052
}
5153
}
5254

src/libstd/str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ pub mod raw {
10921092
pub unsafe fn slice_unchecked<'a>(s: &'a str, begin: uint, end: uint) -> &'a str {
10931093
do s.as_imm_buf |sbuf, _n| {
10941094
cast::transmute(Slice {
1095-
data: sbuf.offset_inbounds(begin as int),
1095+
data: sbuf.offset(begin as int),
10961096
len: end - begin,
10971097
})
10981098
}

src/libstd/unstable/intrinsics.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,19 +332,13 @@ extern "rust-intrinsic" {
332332
/// Get the address of the `__morestack` stack growth function.
333333
pub fn morestack_addr() -> *();
334334

335-
/// Calculates the offset from a pointer.
336-
///
337-
/// This is implemented as an intrinsic to avoid converting to and from an
338-
/// integer, since the conversion would throw away aliasing information.
339-
pub fn offset<T>(dst: *T, offset: int) -> *T;
340-
341335
/// Calculates the offset from a pointer. The offset *must* be in-bounds of
342336
/// the object, or one-byte-past-the-end. An arithmetic overflow is also
343337
/// undefined behaviour.
344338
///
345-
/// This intrinsic should be preferred over `offset` when the guarantee can
346-
/// be satisfied, to enable better optimization.
347-
pub fn offset_inbounds<T>(dst: *T, offset: int) -> *T;
339+
/// This is implemented as an intrinsic to avoid converting to and from an
340+
/// integer, since the conversion would throw away aliasing information.
341+
pub fn offset<T>(dst: *T, offset: int) -> *T;
348342

349343
/// Equivalent to the `llvm.memcpy.p0i8.0i8.i32` intrinsic, with a size of
350344
/// `count` * `size_of::<T>()` and an alignment of `min_align_of::<T>()`

src/libstd/vec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
896896
lifetime: cast::transmute(p)}
897897
} else {
898898
VecIterator{ptr: p,
899-
end: p.offset_inbounds(self.len() as int),
899+
end: p.offset(self.len() as int),
900900
lifetime: cast::transmute(p)}
901901
}
902902
}
@@ -1884,7 +1884,7 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
18841884
lifetime: cast::transmute(p)}
18851885
} else {
18861886
VecMutIterator{ptr: p,
1887-
end: p.offset_inbounds(self.len() as int),
1887+
end: p.offset(self.len() as int),
18881888
lifetime: cast::transmute(p)}
18891889
}
18901890
}
@@ -2247,7 +2247,7 @@ macro_rules! iterator {
22472247
// same pointer.
22482248
cast::transmute(self.ptr as uint + 1)
22492249
} else {
2250-
self.ptr.offset_inbounds(1)
2250+
self.ptr.offset(1)
22512251
};
22522252

22532253
Some(cast::transmute(old))
@@ -2279,7 +2279,7 @@ macro_rules! double_ended_iterator {
22792279
// See above for why 'ptr.offset' isn't used
22802280
cast::transmute(self.end as uint - 1)
22812281
} else {
2282-
self.end.offset_inbounds(-1)
2282+
self.end.offset(-1)
22832283
};
22842284
Some(cast::transmute(self.end))
22852285
}

0 commit comments

Comments
 (0)