Apply #[rustc_panics_when_zero] to the relevant core functions

This commit is contained in:
Urgau
2026-03-08 11:42:58 +01:00
parent 73d7d8dc85
commit d67cce722c
3 changed files with 24 additions and 17 deletions

View File

@@ -1651,14 +1651,7 @@ pub const trait Iterator {
///
/// # Panics
///
/// Panics if `N` is zero. This check will most probably get changed to a
/// compile time error before this method gets stabilized.
///
/// ```should_panic
/// #![feature(iter_map_windows)]
///
/// let iter = std::iter::repeat(0).map_windows(|&[]| ());
/// ```
/// Panics if `N` is zero.
///
/// # Examples
///
@@ -1768,7 +1761,10 @@ pub const trait Iterator {
/// ```
#[inline]
#[unstable(feature = "iter_map_windows", issue = "87155")]
fn map_windows<F, R, const N: usize>(self, f: F) -> MapWindows<Self, F, N>
fn map_windows<F, R, #[rustc_panics_when_zero] const N: usize>(
self,
f: F,
) -> MapWindows<Self, F, N>
where
Self: Sized,
F: FnMut(&[Self::Item; N]) -> R,
@@ -3630,7 +3626,7 @@ pub const trait Iterator {
/// ```
#[track_caller]
#[unstable(feature = "iter_array_chunks", issue = "100450")]
fn array_chunks<const N: usize>(self) -> ArrayChunks<Self, N>
fn array_chunks<#[rustc_panics_when_zero] const N: usize>(self) -> ArrayChunks<Self, N>
where
Self: Sized,
{

View File

@@ -1334,7 +1334,9 @@ impl<T> [T] {
#[inline]
#[must_use]
#[track_caller]
pub const unsafe fn as_chunks_unchecked<const N: usize>(&self) -> &[[T; N]] {
pub const unsafe fn as_chunks_unchecked<#[rustc_panics_when_zero] const N: usize>(
&self,
) -> &[[T; N]] {
assert_unsafe_precondition!(
check_language_ub,
"slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks",
@@ -1392,7 +1394,7 @@ impl<T> [T] {
#[inline]
#[track_caller]
#[must_use]
pub const fn as_chunks<const N: usize>(&self) -> (&[[T; N]], &[T]) {
pub const fn as_chunks<#[rustc_panics_when_zero] const N: usize>(&self) -> (&[[T; N]], &[T]) {
assert!(N != 0, "chunk size must be non-zero");
let len_rounded_down = self.len() / N * N;
// SAFETY: The rounded-down value is always the same or smaller than the
@@ -1439,7 +1441,7 @@ impl<T> [T] {
#[inline]
#[track_caller]
#[must_use]
pub const fn as_rchunks<const N: usize>(&self) -> (&[T], &[[T; N]]) {
pub const fn as_rchunks<#[rustc_panics_when_zero] const N: usize>(&self) -> (&[T], &[[T; N]]) {
assert!(N != 0, "chunk size must be non-zero");
let len = self.len() / N;
let (remainder, multiple_of_n) = self.split_at(self.len() - len * N);
@@ -1494,7 +1496,9 @@ impl<T> [T] {
#[inline]
#[must_use]
#[track_caller]
pub const unsafe fn as_chunks_unchecked_mut<const N: usize>(&mut self) -> &mut [[T; N]] {
pub const unsafe fn as_chunks_unchecked_mut<#[rustc_panics_when_zero] const N: usize>(
&mut self,
) -> &mut [[T; N]] {
assert_unsafe_precondition!(
check_language_ub,
"slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks",
@@ -1548,7 +1552,9 @@ impl<T> [T] {
#[inline]
#[track_caller]
#[must_use]
pub const fn as_chunks_mut<const N: usize>(&mut self) -> (&mut [[T; N]], &mut [T]) {
pub const fn as_chunks_mut<#[rustc_panics_when_zero] const N: usize>(
&mut self,
) -> (&mut [[T; N]], &mut [T]) {
assert!(N != 0, "chunk size must be non-zero");
let len_rounded_down = self.len() / N * N;
// SAFETY: The rounded-down value is always the same or smaller than the
@@ -1601,7 +1607,9 @@ impl<T> [T] {
#[inline]
#[track_caller]
#[must_use]
pub const fn as_rchunks_mut<const N: usize>(&mut self) -> (&mut [T], &mut [[T; N]]) {
pub const fn as_rchunks_mut<#[rustc_panics_when_zero] const N: usize>(
&mut self,
) -> (&mut [T], &mut [[T; N]]) {
assert!(N != 0, "chunk size must be non-zero");
let len = self.len() / N;
let (remainder, multiple_of_n) = self.split_at_mut(self.len() - len * N);
@@ -1642,7 +1650,9 @@ impl<T> [T] {
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
#[inline]
#[track_caller]
pub const fn array_windows<const N: usize>(&self) -> ArrayWindows<'_, T, N> {
pub const fn array_windows<#[rustc_panics_when_zero] const N: usize>(
&self,
) -> ArrayWindows<'_, T, N> {
assert!(N != 0, "window size must be non-zero");
ArrayWindows::new(self)
}

View File

@@ -217,6 +217,7 @@ fn test_case_from_pr_82413_comment() {
#[test]
#[should_panic = "array in `Iterator::map_windows` must contain more than 0 elements"]
#[allow(unconditional_panic)]
fn check_zero_window() {
let _ = std::iter::repeat(0).map_windows(|_: &[_; 0]| ());
}