Skip to content

track_caller for slice length assertions #90306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3076,6 +3076,7 @@ impl<T> [T] {
/// [`copy_from_slice`]: slice::copy_from_slice
/// [`split_at_mut`]: slice::split_at_mut
#[stable(feature = "clone_from_slice", since = "1.7.0")]
#[track_caller]
pub fn clone_from_slice(&mut self, src: &[T])
where
T: Clone,
Expand Down Expand Up @@ -3139,6 +3140,7 @@ impl<T> [T] {
/// [`split_at_mut`]: slice::split_at_mut
#[doc(alias = "memcpy")]
#[stable(feature = "copy_from_slice", since = "1.9.0")]
#[track_caller]
pub fn copy_from_slice(&mut self, src: &[T])
where
T: Copy,
Expand Down Expand Up @@ -3259,6 +3261,7 @@ impl<T> [T] {
///
/// [`split_at_mut`]: slice::split_at_mut
#[stable(feature = "swap_with_slice", since = "1.27.0")]
#[track_caller]
pub fn swap_with_slice(&mut self, other: &mut [T]) {
assert!(self.len() == other.len(), "destination and source slices have different lengths");
// SAFETY: `self` is valid for `self.len()` elements by definition, and `src` was
Expand Down Expand Up @@ -3581,6 +3584,7 @@ impl<T> CloneFromSpec<T> for [T]
where
T: Clone,
{
#[track_caller]
default fn spec_clone_from(&mut self, src: &[T]) {
assert!(self.len() == src.len(), "destination and source slices have different lengths");
// NOTE: We need to explicitly slice them to the same length
Expand All @@ -3598,6 +3602,7 @@ impl<T> CloneFromSpec<T> for [T]
where
T: Copy,
{
#[track_caller]
fn spec_clone_from(&mut self, src: &[T]) {
self.copy_from_slice(src);
}
Expand Down