Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a45f69f

Browse files
authored
Rollup merge of rust-lang#100822 - WaffleLapkin:no_offset_question_mark, r=scottmcm
Replace most uses of `pointer::offset` with `add` and `sub` As PR title says, it replaces `pointer::offset` in compiler and standard library with `pointer::add` and `pointer::sub`. This generally makes code cleaner, easier to grasp and removes (or, well, hides) integer casts. This is generally trivially correct, `.offset(-constant)` is just `.sub(constant)`, `.offset(usized as isize)` is just `.add(usized)`, etc. However in some cases we need to be careful with signs of things. r? ````@scottmcm```` _split off from #100746_
2 parents fd403f5 + e4720e1 commit a45f69f

File tree

22 files changed

+57
-57
lines changed

22 files changed

+57
-57
lines changed

compiler/rustc_arena/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl<T> TypedArena<T> {
219219
} else {
220220
let ptr = self.ptr.get();
221221
// Advance the pointer.
222-
self.ptr.set(self.ptr.get().offset(1));
222+
self.ptr.set(self.ptr.get().add(1));
223223
// Write into uninitialized memory.
224224
ptr::write(ptr, object);
225225
&mut *ptr

compiler/rustc_codegen_cranelift/example/alloc_system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ mod platform {
9494
struct Header(*mut u8);
9595
const HEAP_ZERO_MEMORY: DWORD = 0x00000008;
9696
unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
97-
&mut *(ptr as *mut Header).offset(-1)
97+
&mut *(ptr as *mut Header).sub(1)
9898
}
9999
unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
100100
let aligned = ptr.add(align - (ptr as usize & (align - 1)));

compiler/rustc_codegen_gcc/example/alloc_system.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ mod platform {
156156
struct Header(*mut u8);
157157
const HEAP_ZERO_MEMORY: DWORD = 0x00000008;
158158
unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
159-
&mut *(ptr as *mut Header).offset(-1)
159+
&mut *(ptr as *mut Header).sub(1)
160160
}
161161
unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
162162
let aligned = ptr.add(align - (ptr as usize & (align - 1)));

compiler/rustc_serialize/src/serialize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> {
273273
unsafe {
274274
let ptr: *mut T = vec.as_mut_ptr();
275275
for i in 0..len {
276-
std::ptr::write(ptr.offset(i as isize), Decodable::decode(d));
276+
std::ptr::write(ptr.add(i), Decodable::decode(d));
277277
}
278278
vec.set_len(len);
279279
}

library/alloc/src/alloc/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn allocate_zeroed() {
1515
let end = i.add(layout.size());
1616
while i < end {
1717
assert_eq!(*i, 0);
18-
i = i.offset(1);
18+
i = i.add(1);
1919
}
2020
Global.deallocate(ptr.as_non_null_ptr(), layout);
2121
}

library/alloc/src/collections/vec_deque/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,8 +2447,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
24472447
let mut right_offset = 0;
24482448
for i in left_edge..right_edge {
24492449
right_offset = (i - left_edge) % (cap - right_edge);
2450-
let src: isize = (right_edge + right_offset) as isize;
2451-
ptr::swap(buf.add(i), buf.offset(src));
2450+
let src = right_edge + right_offset;
2451+
ptr::swap(buf.add(i), buf.add(src));
24522452
}
24532453
let n_ops = right_edge - left_edge;
24542454
left_edge += n_ops;

library/alloc/src/slice.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ where
10241024
// Consume the greater side.
10251025
// If equal, prefer the right run to maintain stability.
10261026
unsafe {
1027-
let to_copy = if is_less(&*right.offset(-1), &*left.offset(-1)) {
1027+
let to_copy = if is_less(&*right.sub(1), &*left.sub(1)) {
10281028
decrement_and_get(left)
10291029
} else {
10301030
decrement_and_get(right)
@@ -1038,12 +1038,12 @@ where
10381038

10391039
unsafe fn get_and_increment<T>(ptr: &mut *mut T) -> *mut T {
10401040
let old = *ptr;
1041-
*ptr = unsafe { ptr.offset(1) };
1041+
*ptr = unsafe { ptr.add(1) };
10421042
old
10431043
}
10441044

10451045
unsafe fn decrement_and_get<T>(ptr: &mut *mut T) -> *mut T {
1046-
*ptr = unsafe { ptr.offset(-1) };
1046+
*ptr = unsafe { ptr.sub(1) };
10471047
*ptr
10481048
}
10491049

library/alloc/src/vec/in_place_collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ where
267267
// one slot in the underlying storage will have been freed up and we can immediately
268268
// write back the result.
269269
unsafe {
270-
let dst = dst_buf.offset(i as isize);
270+
let dst = dst_buf.add(i);
271271
debug_assert!(dst as *const _ <= end, "InPlaceIterable contract violation");
272272
ptr::write(dst, self.__iterator_get_unchecked(i));
273273
// Since this executes user code which can panic we have to bump the pointer

library/alloc/src/vec/into_iter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
160160
Some(unsafe { mem::zeroed() })
161161
} else {
162162
let old = self.ptr;
163-
self.ptr = unsafe { self.ptr.offset(1) };
163+
self.ptr = unsafe { self.ptr.add(1) };
164164

165165
Some(unsafe { ptr::read(old) })
166166
}
@@ -272,7 +272,7 @@ impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
272272
// Make up a value of this ZST.
273273
Some(unsafe { mem::zeroed() })
274274
} else {
275-
self.end = unsafe { self.end.offset(-1) };
275+
self.end = unsafe { self.end.sub(1) };
276276

277277
Some(unsafe { ptr::read(self.end) })
278278
}
@@ -288,7 +288,7 @@ impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
288288
}
289289
} else {
290290
// SAFETY: same as for advance_by()
291-
self.end = unsafe { self.end.offset(step_size.wrapping_neg() as isize) };
291+
self.end = unsafe { self.end.sub(step_size) };
292292
}
293293
let to_drop = ptr::slice_from_raw_parts_mut(self.end as *mut T, step_size);
294294
// SAFETY: same as for advance_by()

library/alloc/src/vec/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ impl<T, A: Allocator> Vec<T, A> {
13931393
if index < len {
13941394
// Shift everything over to make space. (Duplicating the
13951395
// `index`th element into two consecutive places.)
1396-
ptr::copy(p, p.offset(1), len - index);
1396+
ptr::copy(p, p.add(1), len - index);
13971397
} else if index == len {
13981398
// No elements need shifting.
13991399
} else {
@@ -1455,7 +1455,7 @@ impl<T, A: Allocator> Vec<T, A> {
14551455
ret = ptr::read(ptr);
14561456

14571457
// Shift everything down to fill in that spot.
1458-
ptr::copy(ptr.offset(1), ptr, len - index - 1);
1458+
ptr::copy(ptr.add(1), ptr, len - index - 1);
14591459
}
14601460
self.set_len(len - 1);
14611461
ret
@@ -2408,7 +2408,7 @@ impl<T, A: Allocator> Vec<T, A> {
24082408
// Write all elements except the last one
24092409
for _ in 1..n {
24102410
ptr::write(ptr, value.next());
2411-
ptr = ptr.offset(1);
2411+
ptr = ptr.add(1);
24122412
// Increment the length in every step in case next() panics
24132413
local_len.increment_len(1);
24142414
}

0 commit comments

Comments
 (0)