Skip to content

Commit ea89080

Browse files
committed
Apply #[rustc_panics_when_zero] to the relevant core functions
1 parent 9026860 commit ea89080

3 files changed

Lines changed: 24 additions & 17 deletions

File tree

library/core/src/iter/traits/iterator.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,14 +1653,7 @@ pub const trait Iterator {
16531653
///
16541654
/// # Panics
16551655
///
1656-
/// Panics if `N` is zero. This check will most probably get changed to a
1657-
/// compile time error before this method gets stabilized.
1658-
///
1659-
/// ```should_panic
1660-
/// #![feature(iter_map_windows)]
1661-
///
1662-
/// let iter = std::iter::repeat(0).map_windows(|&[]| ());
1663-
/// ```
1656+
/// Panics if `N` is zero.
16641657
///
16651658
/// # Examples
16661659
///
@@ -1770,7 +1763,10 @@ pub const trait Iterator {
17701763
/// ```
17711764
#[inline]
17721765
#[unstable(feature = "iter_map_windows", issue = "87155")]
1773-
fn map_windows<F, R, const N: usize>(self, f: F) -> MapWindows<Self, F, N>
1766+
fn map_windows<F, R, #[rustc_panics_when_zero] const N: usize>(
1767+
self,
1768+
f: F,
1769+
) -> MapWindows<Self, F, N>
17741770
where
17751771
Self: Sized,
17761772
F: FnMut(&[Self::Item; N]) -> R,
@@ -3632,7 +3628,7 @@ pub const trait Iterator {
36323628
/// ```
36333629
#[track_caller]
36343630
#[unstable(feature = "iter_array_chunks", issue = "100450")]
3635-
fn array_chunks<const N: usize>(self) -> ArrayChunks<Self, N>
3631+
fn array_chunks<#[rustc_panics_when_zero] const N: usize>(self) -> ArrayChunks<Self, N>
36363632
where
36373633
Self: Sized,
36383634
{

library/core/src/slice/mod.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,9 @@ impl<T> [T] {
13351335
#[inline]
13361336
#[must_use]
13371337
#[track_caller]
1338-
pub const unsafe fn as_chunks_unchecked<const N: usize>(&self) -> &[[T; N]] {
1338+
pub const unsafe fn as_chunks_unchecked<#[rustc_panics_when_zero] const N: usize>(
1339+
&self,
1340+
) -> &[[T; N]] {
13391341
assert_unsafe_precondition!(
13401342
check_language_ub,
13411343
"slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks",
@@ -1393,7 +1395,7 @@ impl<T> [T] {
13931395
#[inline]
13941396
#[track_caller]
13951397
#[must_use]
1396-
pub const fn as_chunks<const N: usize>(&self) -> (&[[T; N]], &[T]) {
1398+
pub const fn as_chunks<#[rustc_panics_when_zero] const N: usize>(&self) -> (&[[T; N]], &[T]) {
13971399
assert!(N != 0, "chunk size must be non-zero");
13981400
let len_rounded_down = self.len() / N * N;
13991401
// SAFETY: The rounded-down value is always the same or smaller than the
@@ -1440,7 +1442,7 @@ impl<T> [T] {
14401442
#[inline]
14411443
#[track_caller]
14421444
#[must_use]
1443-
pub const fn as_rchunks<const N: usize>(&self) -> (&[T], &[[T; N]]) {
1445+
pub const fn as_rchunks<#[rustc_panics_when_zero] const N: usize>(&self) -> (&[T], &[[T; N]]) {
14441446
assert!(N != 0, "chunk size must be non-zero");
14451447
let len = self.len() / N;
14461448
let (remainder, multiple_of_n) = self.split_at(self.len() - len * N);
@@ -1495,7 +1497,9 @@ impl<T> [T] {
14951497
#[inline]
14961498
#[must_use]
14971499
#[track_caller]
1498-
pub const unsafe fn as_chunks_unchecked_mut<const N: usize>(&mut self) -> &mut [[T; N]] {
1500+
pub const unsafe fn as_chunks_unchecked_mut<#[rustc_panics_when_zero] const N: usize>(
1501+
&mut self,
1502+
) -> &mut [[T; N]] {
14991503
assert_unsafe_precondition!(
15001504
check_language_ub,
15011505
"slice::as_chunks_unchecked requires `N != 0` and the slice to split exactly into `N`-element chunks",
@@ -1549,7 +1553,9 @@ impl<T> [T] {
15491553
#[inline]
15501554
#[track_caller]
15511555
#[must_use]
1552-
pub const fn as_chunks_mut<const N: usize>(&mut self) -> (&mut [[T; N]], &mut [T]) {
1556+
pub const fn as_chunks_mut<#[rustc_panics_when_zero] const N: usize>(
1557+
&mut self,
1558+
) -> (&mut [[T; N]], &mut [T]) {
15531559
assert!(N != 0, "chunk size must be non-zero");
15541560
let len_rounded_down = self.len() / N * N;
15551561
// SAFETY: The rounded-down value is always the same or smaller than the
@@ -1602,7 +1608,9 @@ impl<T> [T] {
16021608
#[inline]
16031609
#[track_caller]
16041610
#[must_use]
1605-
pub const fn as_rchunks_mut<const N: usize>(&mut self) -> (&mut [T], &mut [[T; N]]) {
1611+
pub const fn as_rchunks_mut<#[rustc_panics_when_zero] const N: usize>(
1612+
&mut self,
1613+
) -> (&mut [T], &mut [[T; N]]) {
16061614
assert!(N != 0, "chunk size must be non-zero");
16071615
let len = self.len() / N;
16081616
let (remainder, multiple_of_n) = self.split_at_mut(self.len() - len * N);
@@ -1643,7 +1651,9 @@ impl<T> [T] {
16431651
#[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
16441652
#[inline]
16451653
#[track_caller]
1646-
pub const fn array_windows<const N: usize>(&self) -> ArrayWindows<'_, T, N> {
1654+
pub const fn array_windows<#[rustc_panics_when_zero] const N: usize>(
1655+
&self,
1656+
) -> ArrayWindows<'_, T, N> {
16471657
assert!(N != 0, "window size must be non-zero");
16481658
ArrayWindows::new(self)
16491659
}

library/coretests/tests/iter/adapters/map_windows.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ fn test_case_from_pr_82413_comment() {
172172

173173
#[test]
174174
#[should_panic = "array in `Iterator::map_windows` must contain more than 0 elements"]
175+
#[allow(unconditional_panic)]
175176
fn check_zero_window() {
176177
let _ = std::iter::repeat(0).map_windows(|_: &[_; 0]| ());
177178
}

0 commit comments

Comments
 (0)