From d67cce722c69bfeff346d268fab352aef91dbbc2 Mon Sep 17 00:00:00 2001 From: Urgau Date: Sun, 8 Mar 2026 11:42:58 +0100 Subject: [PATCH] Apply `#[rustc_panics_when_zero]` to the relevant core functions --- library/core/src/iter/traits/iterator.rs | 16 +++++-------- library/core/src/slice/mod.rs | 24 +++++++++++++------ .../tests/iter/adapters/map_windows.rs | 1 + 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index c5182cb3d07..3867a44099f 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -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(self, f: F) -> MapWindows + fn map_windows( + self, + f: F, + ) -> MapWindows 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(self) -> ArrayChunks + fn array_chunks<#[rustc_panics_when_zero] const N: usize>(self) -> ArrayChunks where Self: Sized, { diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 83d8ab4ca53..f73f0c53906 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -1334,7 +1334,9 @@ impl [T] { #[inline] #[must_use] #[track_caller] - pub const unsafe fn as_chunks_unchecked(&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] { #[inline] #[track_caller] #[must_use] - pub const fn as_chunks(&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] { #[inline] #[track_caller] #[must_use] - pub const fn as_rchunks(&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] { #[inline] #[must_use] #[track_caller] - pub const unsafe fn as_chunks_unchecked_mut(&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] { #[inline] #[track_caller] #[must_use] - pub const fn as_chunks_mut(&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] { #[inline] #[track_caller] #[must_use] - pub const fn as_rchunks_mut(&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] { #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")] #[inline] #[track_caller] - pub const fn array_windows(&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) } diff --git a/library/coretests/tests/iter/adapters/map_windows.rs b/library/coretests/tests/iter/adapters/map_windows.rs index fa84f9d1c9c..9e8068387f1 100644 --- a/library/coretests/tests/iter/adapters/map_windows.rs +++ b/library/coretests/tests/iter/adapters/map_windows.rs @@ -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]| ()); }