about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--library/alloc/tests/slice.rs4
-rw-r--r--library/core/src/slice/mod.rs17
2 files changed, 18 insertions, 3 deletions
diff --git a/library/alloc/tests/slice.rs b/library/alloc/tests/slice.rs
index 147f7f7d0c7..3c7d57f8b32 100644
--- a/library/alloc/tests/slice.rs
+++ b/library/alloc/tests/slice.rs
@@ -1522,7 +1522,7 @@ fn test_copy_from_slice() {
 }
 
 #[test]
-#[should_panic(expected = "destination and source slices have different lengths")]
+#[should_panic(expected = "source slice length (4) does not match destination slice length (5)")]
 fn test_copy_from_slice_dst_longer() {
     let src = [0, 1, 2, 3];
     let mut dst = [0; 5];
@@ -1530,7 +1530,7 @@ fn test_copy_from_slice_dst_longer() {
 }
 
 #[test]
-#[should_panic(expected = "destination and source slices have different lengths")]
+#[should_panic(expected = "source slice length (4) does not match destination slice length (3)")]
 fn test_copy_from_slice_dst_shorter() {
     let src = [0, 1, 2, 3];
     let mut dst = [0; 3];
diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs
index f511ca8eb78..f2477ed6a6e 100644
--- a/library/core/src/slice/mod.rs
+++ b/library/core/src/slice/mod.rs
@@ -2501,7 +2501,22 @@ impl<T> [T] {
     where
         T: Copy,
     {
-        assert_eq!(self.len(), src.len(), "destination and source slices have different lengths");
+        // The panic code path was put into a cold function to not bloat the
+        // call site.
+        #[inline(never)]
+        #[cold]
+        #[track_caller]
+        fn len_mismatch_fail(dst_len: usize, src_len: usize) -> ! {
+            panic!(
+                "source slice length ({}) does not match destination slice length ({})",
+                src_len, dst_len,
+            );
+        }
+
+        if self.len() != src.len() {
+            len_mismatch_fail(self.len(), src.len());
+        }
+
         // SAFETY: `self` is valid for `self.len()` elements by definition, and `src` was
         // checked to have the same length. The slices cannot overlap because
         // mutable references are exclusive.