Skip to content

Commit 1acfaf2

Browse files
authored
Rollup merge of rust-lang#124699 - scottmcm:split_at_unchecked_should_use_unchecked, r=Nilstrieb
Use `unchecked_sub` in `split_at` LLVM currently isn't figuring it out on its own, even in the checked version where it hypothetically could. Before: <https://rust.godbolt.org/z/PEY38YrKs> ```llvm bb1: ; preds = %start %4 = getelementptr inbounds float, ptr %x.0, i64 %n %5 = sub i64 %x.1, %n ``` After: ```llvm bb1: ; preds = %start %4 = getelementptr inbounds float, ptr %x.0, i64 %n %5 = sub nuw i64 %x.1, %n ``` This is not using the wrapper because there's already a ubcheck covering it, so I don't want this to get a second one once rust-lang#121571 lands. --- This is basically the same as rust-lang#108763, since `split_at` is essentially doing two `get_unchecked`s.
2 parents 5bea81c + b16906d commit 1acfaf2

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

Diff for: core/src/slice/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use crate::cmp::Ordering::{self, Equal, Greater, Less};
1010
use crate::fmt;
1111
use crate::hint;
12-
use crate::intrinsics::exact_div;
12+
use crate::intrinsics::{exact_div, unchecked_sub};
1313
use crate::mem::{self, SizedTypeProperties};
1414
use crate::num::NonZero;
1515
use crate::ops::{Bound, OneSidedRange, Range, RangeBounds};
@@ -1983,7 +1983,7 @@ impl<T> [T] {
19831983
);
19841984

19851985
// SAFETY: Caller has to check that `0 <= mid <= self.len()`
1986-
unsafe { (from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), len - mid)) }
1986+
unsafe { (from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), unchecked_sub(len, mid))) }
19871987
}
19881988

19891989
/// Divides one mutable slice into two at an index, without doing bounds checking.
@@ -2035,7 +2035,12 @@ impl<T> [T] {
20352035
//
20362036
// `[ptr; mid]` and `[mid; len]` are not overlapping, so returning a mutable reference
20372037
// is fine.
2038-
unsafe { (from_raw_parts_mut(ptr, mid), from_raw_parts_mut(ptr.add(mid), len - mid)) }
2038+
unsafe {
2039+
(
2040+
from_raw_parts_mut(ptr, mid),
2041+
from_raw_parts_mut(ptr.add(mid), unchecked_sub(len, mid)),
2042+
)
2043+
}
20392044
}
20402045

20412046
/// Divides one slice into two at an index, returning `None` if the slice is

0 commit comments

Comments
 (0)