@@ -64,7 +64,7 @@ use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
64
64
use core:: ops:: { self , Index , IndexMut , Range , RangeBounds } ;
65
65
use core:: ptr:: { self , NonNull } ;
66
66
use core:: slice:: { self , SliceIndex } ;
67
- use core:: { fmt, intrinsics} ;
67
+ use core:: { fmt, intrinsics, ub_checks } ;
68
68
69
69
#[ stable( feature = "extract_if" , since = "1.87.0" ) ]
70
70
pub use self :: extract_if:: ExtractIf ;
@@ -1058,6 +1058,11 @@ impl<T, A: Allocator> Vec<T, A> {
1058
1058
#[ inline]
1059
1059
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
1060
1060
pub unsafe fn from_raw_parts_in ( ptr : * mut T , length : usize , capacity : usize , alloc : A ) -> Self {
1061
+ ub_checks:: assert_unsafe_precondition!(
1062
+ check_library_ub,
1063
+ "Vec::from_raw_parts_in requires that length <= capacity" ,
1064
+ ( length: usize = length, capacity: usize = capacity) => length <= capacity
1065
+ ) ;
1061
1066
unsafe { Vec { buf : RawVec :: from_raw_parts_in ( ptr, capacity, alloc) , len : length } }
1062
1067
}
1063
1068
@@ -1174,6 +1179,11 @@ impl<T, A: Allocator> Vec<T, A> {
1174
1179
#[ unstable( feature = "allocator_api" , reason = "new API" , issue = "32838" ) ]
1175
1180
// #[unstable(feature = "box_vec_non_null", issue = "130364")]
1176
1181
pub unsafe fn from_parts_in ( ptr : NonNull < T > , length : usize , capacity : usize , alloc : A ) -> Self {
1182
+ ub_checks:: assert_unsafe_precondition!(
1183
+ check_library_ub,
1184
+ "Vec::from_parts_in requires that length <= capacity" ,
1185
+ ( length: usize = length, capacity: usize = capacity) => length <= capacity
1186
+ ) ;
1177
1187
unsafe { Vec { buf : RawVec :: from_nonnull_in ( ptr, capacity, alloc) , len : length } }
1178
1188
}
1179
1189
@@ -1950,7 +1960,11 @@ impl<T, A: Allocator> Vec<T, A> {
1950
1960
#[ inline]
1951
1961
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1952
1962
pub unsafe fn set_len ( & mut self , new_len : usize ) {
1953
- debug_assert ! ( new_len <= self . capacity( ) ) ;
1963
+ ub_checks:: assert_unsafe_precondition!(
1964
+ check_library_ub,
1965
+ "Vec::set_len requires that new_len <= capacity()" ,
1966
+ ( new_len: usize = new_len, capacity: usize = self . capacity( ) ) => new_len <= capacity
1967
+ ) ;
1954
1968
1955
1969
self . len = new_len;
1956
1970
}
@@ -3695,7 +3709,7 @@ impl<T, A: Allocator> Vec<T, A> {
3695
3709
/// This is optimal if:
3696
3710
///
3697
3711
/// * The tail (elements in the vector after `range`) is empty,
3698
- /// * or `replace_with` yields fewer or equal elements than `range`’ s length
3712
+ /// * or `replace_with` yields fewer or equal elements than `range`' s length
3699
3713
/// * or the lower bound of its `size_hint()` is exact.
3700
3714
///
3701
3715
/// Otherwise, a temporary vector is allocated and the tail is moved twice.
0 commit comments