Skip to content

Commit 70042d7

Browse files
committed
Mark slice::copy_from_slice unstably const
1 parent b8495e5 commit 70042d7

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

library/core/src/slice/mod.rs

Lines changed: 18 additions & 7 deletions
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, select_unpredictable, unchecked_sub};
10+
use crate::intrinsics::{const_eval_select, exact_div, select_unpredictable, unchecked_sub};
1111
use crate::mem::{self, SizedTypeProperties};
1212
use crate::num::NonZero;
1313
use crate::ops::{Bound, OneSidedRange, Range, RangeBounds};
@@ -3671,8 +3671,9 @@ impl<T> [T] {
36713671
/// [`split_at_mut`]: slice::split_at_mut
36723672
#[doc(alias = "memcpy")]
36733673
#[stable(feature = "copy_from_slice", since = "1.9.0")]
3674+
#[rustc_const_unstable(feature = "const_copy_from_slice", issue = "131415")]
36743675
#[track_caller]
3675-
pub fn copy_from_slice(&mut self, src: &[T])
3676+
pub const fn copy_from_slice(&mut self, src: &[T])
36763677
where
36773678
T: Copy,
36783679
{
@@ -3681,11 +3682,21 @@ impl<T> [T] {
36813682
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
36823683
#[cfg_attr(feature = "panic_immediate_abort", inline)]
36833684
#[track_caller]
3684-
fn len_mismatch_fail(dst_len: usize, src_len: usize) -> ! {
3685-
panic!(
3686-
"source slice length ({}) does not match destination slice length ({})",
3687-
src_len, dst_len,
3688-
);
3685+
const fn len_mismatch_fail(dst_len: usize, src_len: usize) -> ! {
3686+
const fn panic_at_const(_dst_len: usize, _src_len: usize) -> ! {
3687+
// Note that we cannot format in constant expressions.
3688+
panic!(
3689+
"copy_from_slice: source slice length does not match destination slice length"
3690+
);
3691+
}
3692+
fn panic_at_rt(dst_len: usize, src_len: usize) -> ! {
3693+
panic!(
3694+
"source slice length ({}) does not match destination slice length ({})",
3695+
src_len, dst_len,
3696+
);
3697+
}
3698+
// FIXME(const-hack): We would prefer to have streamlined panics when formatters become const-friendly.
3699+
const_eval_select((dst_len, src_len), panic_at_const, panic_at_rt)
36893700
}
36903701

36913702
if self.len() != src.len() {

0 commit comments

Comments
 (0)