Skip to content

Commit

Permalink
Mark slice::copy_from_slice unstably const
Browse files Browse the repository at this point in the history
  • Loading branch information
okaneco committed Oct 8, 2024
1 parent b8495e5 commit 70042d7
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions library/core/src/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#![stable(feature = "rust1", since = "1.0.0")]

use crate::cmp::Ordering::{self, Equal, Greater, Less};
use crate::intrinsics::{exact_div, select_unpredictable, unchecked_sub};
use crate::intrinsics::{const_eval_select, exact_div, select_unpredictable, unchecked_sub};
use crate::mem::{self, SizedTypeProperties};
use crate::num::NonZero;
use crate::ops::{Bound, OneSidedRange, Range, RangeBounds};
Expand Down Expand Up @@ -3671,8 +3671,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,
{
Expand All @@ -3681,11 +3682,21 @@ 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 fn panic_at_const(_dst_len: usize, _src_len: usize) -> ! {
// Note that we cannot format in constant expressions.
panic!(
"copy_from_slice: source slice length does not match destination slice length"
);
}
fn panic_at_rt(dst_len: usize, src_len: usize) -> ! {
panic!(
"source slice length ({}) does not match destination slice length ({})",
src_len, dst_len,
);
}
// FIXME(const-hack): We would prefer to have streamlined panics when formatters become const-friendly.
const_eval_select((dst_len, src_len), panic_at_const, panic_at_rt)
}

if self.len() != src.len() {
Expand Down

0 comments on commit 70042d7

Please sign in to comment.