From 3ab742b04d0f3ec7bbfdaf38273ceb0f8da86717 Mon Sep 17 00:00:00 2001 From: SomeFlyingThing <306498559+SomeFlyingThing@users.noreply.github.com> Date: Sat, 8 Aug 2026 12:50:49 -0700 Subject: [PATCH 1/2] Hint that VecDeque binary search results are in bounds --- .../alloc/src/collections/vec_deque/mod.rs | 11 ++++++- ...eque-binary-search-index-no-bound-check.rs | 29 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/codegen-llvm/vec-deque-binary-search-index-no-bound-check.rs diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 940fce7377938..f2092e80eda1d 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -3314,13 +3314,22 @@ impl VecDeque { let (front, back) = self.as_slices(); let cmp_back = back.first().map(|elem| f(elem)); - if let Some(Ordering::Equal) = cmp_back { + let result = if let Some(Ordering::Equal) = cmp_back { Ok(front.len()) } else if let Some(Ordering::Less) = cmp_back { back.binary_search_by(f).map(|idx| idx + front.len()).map_err(|idx| idx + front.len()) } else { front.binary_search_by(f) + }; + + match result { + // SAFETY: `index` points at an element of one of the two slices. + Ok(index) => unsafe { core::hint::assert_unchecked(index < self.len()) }, + // SAFETY: An insertion point in either slice is at most one past the deque's end. + Err(index) => unsafe { core::hint::assert_unchecked(index <= self.len()) }, } + + result } /// Binary searches this `VecDeque` with a key extraction function. diff --git a/tests/codegen-llvm/vec-deque-binary-search-index-no-bound-check.rs b/tests/codegen-llvm/vec-deque-binary-search-index-no-bound-check.rs new file mode 100644 index 0000000000000..796f924cab10c --- /dev/null +++ b/tests/codegen-llvm/vec-deque-binary-search-index-no-bound-check.rs @@ -0,0 +1,29 @@ +//@ compile-flags: -Copt-level=3 +#![crate_type = "lib"] + +use std::collections::VecDeque; + +// Make sure no bounds check is emitted when indexing with a successful search result. + +// CHECK-LABEL: @vec_deque_binary_search_index_no_bounds_check +#[no_mangle] +pub fn vec_deque_binary_search_index_no_bounds_check(deque: &VecDeque) -> u8 { + // CHECK-NOT: expect_failed + // CHECK-NOT: panic + if let Ok(index) = deque.binary_search_by(|element| element.cmp(&b'\\')) { + deque[index] + } else { + 42 + } +} + +// An unsuccessful search result is a valid insertion point. + +// CHECK-LABEL: @vec_deque_binary_search_insert_no_bounds_check +#[no_mangle] +pub fn vec_deque_binary_search_insert_no_bounds_check(deque: &mut VecDeque) { + // CHECK-NOT: panic + if let Err(index) = deque.binary_search_by(|element| element.cmp(&b'\\')) { + deque.insert(index, b'\\'); + } +} From 9dcb499d179cdc95c38788b1bbab493d857b7dc8 Mon Sep 17 00:00:00 2001 From: SomeFlyingThing <306498559+SomeFlyingThing@users.noreply.github.com> Date: Sat, 8 Aug 2026 16:27:06 -0700 Subject: [PATCH 2/2] Fix VecDeque binary search codegen test --- ...c-deque-binary-search-index-no-bound-check.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/codegen-llvm/vec-deque-binary-search-index-no-bound-check.rs b/tests/codegen-llvm/vec-deque-binary-search-index-no-bound-check.rs index 796f924cab10c..ed824baf964ed 100644 --- a/tests/codegen-llvm/vec-deque-binary-search-index-no-bound-check.rs +++ b/tests/codegen-llvm/vec-deque-binary-search-index-no-bound-check.rs @@ -3,13 +3,16 @@ use std::collections::VecDeque; +unsafe extern "C" { + safe fn vec_deque_binary_search_insertion_point_out_of_bounds(); +} + // Make sure no bounds check is emitted when indexing with a successful search result. // CHECK-LABEL: @vec_deque_binary_search_index_no_bounds_check #[no_mangle] pub fn vec_deque_binary_search_index_no_bounds_check(deque: &VecDeque) -> u8 { // CHECK-NOT: expect_failed - // CHECK-NOT: panic if let Ok(index) = deque.binary_search_by(|element| element.cmp(&b'\\')) { deque[index] } else { @@ -19,11 +22,14 @@ pub fn vec_deque_binary_search_index_no_bounds_check(deque: &VecDeque) -> u8 // An unsuccessful search result is a valid insertion point. -// CHECK-LABEL: @vec_deque_binary_search_insert_no_bounds_check +// CHECK-LABEL: @vec_deque_binary_search_insertion_point_in_bounds #[no_mangle] -pub fn vec_deque_binary_search_insert_no_bounds_check(deque: &mut VecDeque) { - // CHECK-NOT: panic +pub fn vec_deque_binary_search_insertion_point_in_bounds(deque: &VecDeque) { + // CHECK-NOT: call void @vec_deque_binary_search_insertion_point_out_of_bounds + // CHECK: ret void if let Err(index) = deque.binary_search_by(|element| element.cmp(&b'\\')) { - deque.insert(index, b'\\'); + if index > deque.len() { + vec_deque_binary_search_insertion_point_out_of_bounds() + } } }