@@ -560,8 +560,9 @@ impl<T> [T] {
560
560
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
561
561
#[ inline]
562
562
pub fn swap ( & mut self , a : usize , b : usize ) {
563
- assert ! ( a < self . len( ) ) ;
564
- assert ! ( b < self . len( ) ) ;
563
+ assert_in_bounds ( self . len ( ) , a) ;
564
+ assert_in_bounds ( self . len ( ) , b) ;
565
+
565
566
// SAFETY: we just checked that both `a` and `b` are in bounds
566
567
unsafe { self . swap_unchecked ( a, b) }
567
568
}
@@ -595,8 +596,12 @@ impl<T> [T] {
595
596
/// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
596
597
#[ unstable( feature = "slice_swap_unchecked" , issue = "88539" ) ]
597
598
pub unsafe fn swap_unchecked ( & mut self , a : usize , b : usize ) {
598
- debug_assert ! ( a < self . len( ) ) ;
599
- debug_assert ! ( b < self . len( ) ) ;
599
+ #[ cfg( debug_assertions) ]
600
+ {
601
+ assert_in_bounds ( self . len ( ) , a) ;
602
+ assert_in_bounds ( self . len ( ) , b) ;
603
+ }
604
+
600
605
let ptr = self . as_mut_ptr ( ) ;
601
606
// SAFETY: caller has to guarantee that `a < self.len()` and `b < self.len()`
602
607
unsafe {
@@ -3497,6 +3502,12 @@ impl<T> [T] {
3497
3502
}
3498
3503
}
3499
3504
3505
+ fn assert_in_bounds ( len : usize , idx : usize ) {
3506
+ if idx >= len {
3507
+ panic ! ( "index out of bounds: the len is {} but the index is {}" , len, idx) ;
3508
+ }
3509
+ }
3510
+
3500
3511
trait CloneFromSpec < T > {
3501
3512
fn spec_clone_from ( & mut self , src : & [ T ] ) ;
3502
3513
}
0 commit comments