about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJacob Pratt <jacob@jhpratt.dev>2024-12-01 21:38:23 -0500
committerGitHub <noreply@github.com>2024-12-01 21:38:23 -0500
commitcb67512784a3986483d2a4c76eeed28c200beeea (patch)
treee672d86786249d3ed4aff7455a3e9bf8804b621f
parenta522d78598415cdd614ccc6d961160f192f64b5c (diff)
parent7f011a894fca7b47a9f29e1220ed10fc6a0f80ce (diff)
downloadrust-cb67512784a3986483d2a4c76eeed28c200beeea.tar.gz
rust-cb67512784a3986483d2a4c76eeed28c200beeea.zip
Rollup merge of #131416 - okaneco:const_copy, r=RalfJung
Mark `slice::copy_from_slice` unstably const

Tracking issue #131415

I used `const_eval_select` for runtime and const panic functions because const formatting isn't available yet.
-rw-r--r--library/core/src/slice/mod.rs16
1 files changed, 10 insertions, 6 deletions
diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs
index ec04852d44b..bc49b7d9797 100644
--- a/library/core/src/slice/mod.rs
+++ b/library/core/src/slice/mod.rs
@@ -11,6 +11,7 @@ use crate::intrinsics::{exact_div, select_unpredictable, unchecked_sub};
 use crate::mem::{self, SizedTypeProperties};
 use crate::num::NonZero;
 use crate::ops::{Bound, OneSidedRange, Range, RangeBounds, RangeInclusive};
+use crate::panic::const_panic;
 use crate::simd::{self, Simd};
 use crate::ub_checks::assert_unsafe_precondition;
 use crate::{fmt, hint, ptr, range, slice};
@@ -3703,8 +3704,9 @@ impl<T> [T] {
     /// [`split_at_mut`]: slice::split_at_mut
     #[doc(alias = "memcpy")]
     #[stable(feature = "copy_from_slice", since = "1.9.0")]
+    #[rustc_const_unstable(feature = "const_copy_from_slice", issue = "131415")]
     #[track_caller]
-    pub fn copy_from_slice(&mut self, src: &[T])
+    pub const fn copy_from_slice(&mut self, src: &[T])
     where
         T: Copy,
     {
@@ -3713,11 +3715,13 @@ impl<T> [T] {
         #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
         #[cfg_attr(feature = "panic_immediate_abort", inline)]
         #[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,
-            );
+        const fn len_mismatch_fail(dst_len: usize, src_len: usize) -> ! {
+            const_panic!(
+                "copy_from_slice: source slice length does not match destination slice length",
+                "copy_from_slice: source slice length ({src_len}) does not match destination slice length ({dst_len})",
+                src_len: usize,
+                dst_len: usize,
+            )
         }
 
         if self.len() != src.len() {