Skip to content

Commit 89ff7f4

Browse files
committed
Use slice_index in SliceIndex<[T]> for usize
1 parent 2d383ad commit 89ff7f4

File tree

3 files changed

+30
-98
lines changed

3 files changed

+30
-98
lines changed

library/core/src/slice/index.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use crate::intrinsics::assert_unsafe_precondition;
44
use crate::intrinsics::const_eval_select;
55
use crate::intrinsics::unchecked_sub;
6+
#[cfg(not(bootstrap))]
7+
use crate::intrinsics::slice_index;
68
use crate::ops;
79
use crate::ptr;
810

@@ -214,13 +216,27 @@ unsafe impl<T> SliceIndex<[T]> for usize {
214216
#[inline]
215217
fn get(self, slice: &[T]) -> Option<&T> {
216218
// SAFETY: `self` is checked to be in bounds.
217-
if self < slice.len() { unsafe { Some(&*self.get_unchecked(slice)) } } else { None }
219+
if self < slice.len() {
220+
#[cfg(bootstrap)]
221+
unsafe { Some(&*self.get_unchecked(slice)) }
222+
#[cfg(not(bootstrap))]
223+
unsafe { Some(slice_index(slice, self)) }
224+
} else {
225+
None
226+
}
218227
}
219228

220229
#[inline]
221230
fn get_mut(self, slice: &mut [T]) -> Option<&mut T> {
222231
// SAFETY: `self` is checked to be in bounds.
223-
if self < slice.len() { unsafe { Some(&mut *self.get_unchecked_mut(slice)) } } else { None }
232+
if self < slice.len() {
233+
#[cfg(bootstrap)]
234+
unsafe { Some(&mut *self.get_unchecked_mut(slice)) }
235+
#[cfg(not(bootstrap))]
236+
unsafe { Some(slice_index(slice, self)) }
237+
} else {
238+
None
239+
}
224240
}
225241

226242
#[inline]
@@ -230,26 +246,32 @@ unsafe impl<T> SliceIndex<[T]> for usize {
230246
// cannot be longer than `isize::MAX`. They also guarantee that
231247
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
232248
// so the call to `add` is safe.
249+
#[cfg(bootstrap)]
233250
unsafe {
234251
assert_unsafe_precondition!(
235252
"slice::get_unchecked requires that the index is within the slice",
236253
[T](this: usize, slice: *const [T]) => this < slice.len()
237254
);
238255
slice.as_ptr().add(self)
239256
}
257+
#[cfg(not(bootstrap))]
258+
unsafe { slice_index(slice, this) }
240259
}
241260

242261
#[inline]
243262
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut T {
244263
let this = self;
245264
// SAFETY: see comments for `get_unchecked` above.
265+
#[cfg(bootstrap)]
246266
unsafe {
247267
assert_unsafe_precondition!(
248268
"slice::get_unchecked_mut requires that the index is within the slice",
249269
[T](this: usize, slice: *mut [T]) => this < slice.len()
250270
);
251271
slice.as_mut_ptr().add(self)
252272
}
273+
#[cfg(not(bootstrap))]
274+
unsafe { slice_index(slice, this) }
253275
}
254276

255277
#[inline]

tests/mir-opt/pre-codegen/slice_index.slice_get_mut_usize.PreCodegen.after.panic-abort.mir

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,13 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
1212
debug slice => _1;
1313
let mut _3: usize;
1414
let mut _4: bool;
15-
let mut _5: *mut [u32];
16-
let mut _7: *mut u32;
17-
let mut _8: &mut u32;
15+
let mut _5: &mut u32;
1816
scope 3 {
19-
scope 4 (inlined <usize as SliceIndex<[u32]>>::get_unchecked_mut) {
20-
debug self => _2;
21-
debug slice => _5;
22-
let mut _6: *mut u32;
23-
scope 5 {
24-
debug this => _2;
25-
scope 6 {
26-
scope 7 (inlined <usize as SliceIndex<[T]>>::get_unchecked_mut::runtime::<u32>) {
27-
debug this => _2;
28-
debug slice => _5;
29-
scope 8 (inlined ptr::mut_ptr::<impl *mut [u32]>::len) {
30-
debug self => _5;
31-
let mut _9: *const [u32];
32-
scope 9 (inlined std::ptr::metadata::<[u32]>) {
33-
debug ptr => _9;
34-
scope 10 {
35-
}
36-
}
37-
}
38-
}
39-
scope 11 (inlined ptr::mut_ptr::<impl *mut [u32]>::as_mut_ptr) {
40-
debug self => _5;
41-
}
42-
scope 12 (inlined ptr::mut_ptr::<impl *mut u32>::add) {
43-
debug self => _6;
44-
debug count => _2;
45-
scope 13 {
46-
}
47-
}
48-
}
49-
}
50-
}
5117
}
5218
}
5319
}
5420

5521
bb0: {
56-
StorageLive(_7);
5722
StorageLive(_4);
5823
StorageLive(_3);
5924
_3 = Len((*_1));
@@ -69,25 +34,15 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
6934

7035
bb2: {
7136
StorageDead(_3);
72-
StorageLive(_8);
7337
StorageLive(_5);
74-
_5 = &raw mut (*_1);
75-
StorageLive(_9);
76-
StorageLive(_6);
77-
_6 = _5 as *mut u32 (PtrToPtr);
78-
_7 = Offset(_6, _2);
79-
StorageDead(_6);
80-
StorageDead(_9);
38+
_5 = &mut (*_1)[_2];
39+
_0 = Option::<&mut u32>::Some(move _5);
8140
StorageDead(_5);
82-
_8 = &mut (*_7);
83-
_0 = Option::<&mut u32>::Some(move _8);
84-
StorageDead(_8);
8541
goto -> bb3;
8642
}
8743

8844
bb3: {
8945
StorageDead(_4);
90-
StorageDead(_7);
9146
return;
9247
}
9348
}

tests/mir-opt/pre-codegen/slice_index.slice_get_mut_usize.PreCodegen.after.panic-unwind.mir

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,13 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
1212
debug slice => _1;
1313
let mut _3: usize;
1414
let mut _4: bool;
15-
let mut _5: *mut [u32];
16-
let mut _7: *mut u32;
17-
let mut _8: &mut u32;
15+
let mut _5: &mut u32;
1816
scope 3 {
19-
scope 4 (inlined <usize as SliceIndex<[u32]>>::get_unchecked_mut) {
20-
debug self => _2;
21-
debug slice => _5;
22-
let mut _6: *mut u32;
23-
scope 5 {
24-
debug this => _2;
25-
scope 6 {
26-
scope 7 (inlined <usize as SliceIndex<[T]>>::get_unchecked_mut::runtime::<u32>) {
27-
debug this => _2;
28-
debug slice => _5;
29-
scope 8 (inlined ptr::mut_ptr::<impl *mut [u32]>::len) {
30-
debug self => _5;
31-
let mut _9: *const [u32];
32-
scope 9 (inlined std::ptr::metadata::<[u32]>) {
33-
debug ptr => _9;
34-
scope 10 {
35-
}
36-
}
37-
}
38-
}
39-
scope 11 (inlined ptr::mut_ptr::<impl *mut [u32]>::as_mut_ptr) {
40-
debug self => _5;
41-
}
42-
scope 12 (inlined ptr::mut_ptr::<impl *mut u32>::add) {
43-
debug self => _6;
44-
debug count => _2;
45-
scope 13 {
46-
}
47-
}
48-
}
49-
}
50-
}
5117
}
5218
}
5319
}
5420

5521
bb0: {
56-
StorageLive(_7);
5722
StorageLive(_4);
5823
StorageLive(_3);
5924
_3 = Len((*_1));
@@ -69,25 +34,15 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
6934

7035
bb2: {
7136
StorageDead(_3);
72-
StorageLive(_8);
7337
StorageLive(_5);
74-
_5 = &raw mut (*_1);
75-
StorageLive(_9);
76-
StorageLive(_6);
77-
_6 = _5 as *mut u32 (PtrToPtr);
78-
_7 = Offset(_6, _2);
79-
StorageDead(_6);
80-
StorageDead(_9);
38+
_5 = &mut (*_1)[_2];
39+
_0 = Option::<&mut u32>::Some(move _5);
8140
StorageDead(_5);
82-
_8 = &mut (*_7);
83-
_0 = Option::<&mut u32>::Some(move _8);
84-
StorageDead(_8);
8541
goto -> bb3;
8642
}
8743

8844
bb3: {
8945
StorageDead(_4);
90-
StorageDead(_7);
9146
return;
9247
}
9348
}

0 commit comments

Comments
 (0)