Skip to content

Commit da62410

Browse files
committed
Auto merge of rust-lang#141068 - TDecking:slice-assumption, r=<try>
Provide slice length information to the compiler This adds the same information already available for `Vec<T>` values to slices.
2 parents c79bbfa + 9de434a commit da62410

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

library/core/src/slice/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#![stable(feature = "rust1", since = "1.0.0")]
88

99
use crate::cmp::Ordering::{self, Equal, Greater, Less};
10-
use crate::intrinsics::{exact_div, unchecked_sub};
10+
use crate::intrinsics::{assume, exact_div, unchecked_sub};
1111
use crate::mem::{self, MaybeUninit, SizedTypeProperties};
1212
use crate::num::NonZero;
1313
use crate::ops::{OneSidedRange, OneSidedRangeBound, Range, RangeBounds, RangeInclusive};
@@ -113,7 +113,13 @@ impl<T> [T] {
113113
#[inline]
114114
#[must_use]
115115
pub const fn len(&self) -> usize {
116-
ptr::metadata(self)
116+
let len = ptr::metadata(self);
117+
118+
// SAFETY: The value of `T::MAX_SLICE_LEN` is per definition
119+
// the largest possible value for the slice length.
120+
unsafe { assume(len <= T::MAX_SLICE_LEN) };
121+
122+
len
117123
}
118124

119125
/// Returns `true` if the slice has a length of 0.

0 commit comments

Comments
 (0)