From 4d873a7841b337e2b8d6a4131888fa756610a45b Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Sun, 10 Dec 2023 02:10:26 +0000 Subject: [PATCH 1/2] Add const assert to `MapWindows` Checks if const generic N is not set to 0, otherwise it prevents compilation. --- library/core/src/iter/adapters/map_windows.rs | 5 ++++- library/core/src/iter/traits/iterator.rs | 7 ++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/library/core/src/iter/adapters/map_windows.rs b/library/core/src/iter/adapters/map_windows.rs index 5f39b24583427..905d8c0aae842 100644 --- a/library/core/src/iter/adapters/map_windows.rs +++ b/library/core/src/iter/adapters/map_windows.rs @@ -49,9 +49,12 @@ struct Buffer { } impl MapWindows { - pub(in crate::iter) fn new(iter: I, f: F) -> Self { + const N_NOT_ZERO: () = assert!(N != 0, "array in `Iterator::map_windows` must contain more than 0 elements"); + pub(in crate::iter) fn new(iter: I, f: F) -> Self { + let _ = Self::N_NOT_ZERO; + // Only ZST arrays' length can be so large. if mem::size_of::() == 0 { assert!( diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 20dd95a3a4623..c8314caf6569a 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -1609,12 +1609,9 @@ pub trait Iterator { /// [`slice::windows()`]: slice::windows /// [`FusedIterator`]: crate::iter::FusedIterator /// - /// # Panics - /// - /// Panics if `N` is 0. This check will most probably get changed to a - /// compile time error before this method gets stabilized. + /// If `N` is 0, then the code will not compile. /// - /// ```should_panic + /// ```compile_fail /// #![feature(iter_map_windows)] /// /// let iter = std::iter::repeat(0).map_windows(|&[]| ()); From 26e40beaf12b6a6afbe087f07a1fa05cf6425ee6 Mon Sep 17 00:00:00 2001 From: Jareth Gomes Date: Thu, 15 Feb 2024 17:29:47 +0000 Subject: [PATCH 2/2] Move `MapWindow` test to doctest to check for intended compilation failure --- library/core/src/iter/traits/iterator.rs | 6 ++++++ library/core/tests/iter/adapters/map_windows.rs | 6 ------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index c8314caf6569a..48cdfffcb55ae 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -1617,6 +1617,12 @@ pub trait Iterator { /// let iter = std::iter::repeat(0).map_windows(|&[]| ()); /// ``` /// + /// ```compile_fail + /// #![feature(iter_map_windows)] + /// + /// let iter = std::iter::repeat(0).map_windows(|_: &[_; 0]| ()); + /// ``` + /// /// # Examples /// /// Building the sums of neighboring numbers. diff --git a/library/core/tests/iter/adapters/map_windows.rs b/library/core/tests/iter/adapters/map_windows.rs index 7fb2408f8acb7..56841871a88c4 100644 --- a/library/core/tests/iter/adapters/map_windows.rs +++ b/library/core/tests/iter/adapters/map_windows.rs @@ -167,12 +167,6 @@ fn test_case_from_pr_82413_comment() { for () in std::iter::repeat("0".to_owned()).map_windows(|_: &[_; 3]| {}).take(4) {} } -#[test] -#[should_panic = "array in `Iterator::map_windows` must contain more than 0 elements"] -fn check_zero_window() { - let _ = std::iter::repeat(0).map_windows(|_: &[_; 0]| ()); -} - #[test] fn test_zero_sized_type() { #[derive(Copy, Clone, Debug, Eq, PartialEq)]