Skip to content

Commit 63379d6

Browse files
committed
Auto merge of rust-lang#81354 - SkiFire13:binary-search-assume, r=nagisa
Instruct LLVM that binary_search returns a valid index This allows removing bound checks when the return value of `binary_search` is used to index into the slice it was call on. I also added a codegen test for this, not sure if it's the right thing to do (I didn't find anything on the dev guide), but it felt so.
2 parents f22c148 + 730fdc0 commit 63379d6

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

core/src/slice/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,6 +2204,8 @@ impl<T> [T] {
22042204
} else if cmp == Greater {
22052205
right = mid;
22062206
} else {
2207+
// SAFETY: same as the `get_unchecked` above
2208+
unsafe { crate::intrinsics::assume(mid < self.len()) };
22072209
return Ok(mid);
22082210
}
22092211

0 commit comments

Comments
 (0)