5656#[ cfg( not( no_global_oom_handling) ) ]
5757use core:: cmp;
5858use core:: cmp:: Ordering ;
59- use core:: fmt;
6059use core:: hash:: { Hash , Hasher } ;
6160#[ cfg( not( no_global_oom_handling) ) ]
6261use core:: iter;
@@ -65,6 +64,7 @@ use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
6564use core:: ops:: { self , Index , IndexMut , Range , RangeBounds } ;
6665use core:: ptr:: { self , NonNull } ;
6766use core:: slice:: { self , SliceIndex } ;
67+ use core:: { fmt, intrinsics} ;
6868
6969#[ unstable( feature = "extract_if" , reason = "recently added" , issue = "43244" ) ]
7070pub use self :: extract_if:: ExtractIf ;
@@ -2675,7 +2675,14 @@ impl<T, A: Allocator> Vec<T, A> {
26752675 #[ rustc_const_unstable( feature = "const_vec_string_slice" , issue = "129041" ) ]
26762676 #[ rustc_confusables( "length" , "size" ) ]
26772677 pub const fn len ( & self ) -> usize {
2678- self . len
2678+ let len = self . len ;
2679+
2680+ // SAFETY: The maximum capacity of `Vec<T>` is `isize::MAX` bytes, so the maximum value can
2681+ // be returned is `usize::checked_div(mem::size_of::<T>()).unwrap_or(usize::MAX)`, which
2682+ // matches the definition of `T::MAX_SLICE_LEN`.
2683+ unsafe { intrinsics:: assume ( len <= T :: MAX_SLICE_LEN ) } ;
2684+
2685+ len
26792686 }
26802687
26812688 /// Returns `true` if the vector contains no elements.
0 commit comments