@@ -560,8 +560,9 @@ impl<T> [T] {
560560 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
561561 #[ inline]
562562 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+
565566 // SAFETY: we just checked that both `a` and `b` are in bounds
566567 unsafe { self . swap_unchecked ( a, b) }
567568 }
@@ -595,8 +596,12 @@ impl<T> [T] {
595596 /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
596597 #[ unstable( feature = "slice_swap_unchecked" , issue = "88539" ) ]
597598 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+
600605 let ptr = self . as_mut_ptr ( ) ;
601606 // SAFETY: caller has to guarantee that `a < self.len()` and `b < self.len()`
602607 unsafe {
@@ -3497,6 +3502,12 @@ impl<T> [T] {
34973502 }
34983503}
34993504
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+
35003511trait CloneFromSpec < T > {
35013512 fn spec_clone_from ( & mut self , src : & [ T ] ) ;
35023513}
0 commit comments