From fe67c2beb06d478408eb7b7849cf60a1bb9fe46b Mon Sep 17 00:00:00 2001 From: Tim Fennis Date: Tue, 2 Jun 2026 07:52:38 +0200 Subject: [PATCH] =?UTF-8?q?fix(stdlib):=20return=20error=20instead=20of=20?= =?UTF-8?q?panicking=20on=20windows=20size=200=20=F0=9F=AA=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.8 (1M context) --- ndc_stdlib/src/sequence.rs | 6 ++++-- .../programs/900_bugs/bug0024_windows_zero_size.ndc | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 tests/functional/programs/900_bugs/bug0024_windows_zero_size.ndc diff --git a/ndc_stdlib/src/sequence.rs b/ndc_stdlib/src/sequence.rs index 7a1069eb..ea6c7f8d 100644 --- a/ndc_stdlib/src/sequence.rs +++ b/ndc_stdlib/src/sequence.rs @@ -707,8 +707,10 @@ mod inner { /// Returns a list of all contiguous windows of `length` size. The windows overlap. If the `seq` is shorter than size, the iterator returns no values. #[function(return_type = Vec<_>)] - pub fn windows(seq: SeqValue, length: i64) -> anyhow::Result { - let length = length as usize; + pub fn windows(seq: SeqValue, length: usize) -> anyhow::Result { + if length == 0 { + return Err(anyhow!("window size must be non-zero")); + } Ok(Value::list( seq.try_into_iter() .ok_or_else(|| anyhow!("windows requires a sequence"))? diff --git a/tests/functional/programs/900_bugs/bug0024_windows_zero_size.ndc b/tests/functional/programs/900_bugs/bug0024_windows_zero_size.ndc new file mode 100644 index 00000000..372e5172 --- /dev/null +++ b/tests/functional/programs/900_bugs/bug0024_windows_zero_size.ndc @@ -0,0 +1,5 @@ +// `windows(seq, 0)` used to reach `slice::windows(0)`, which panics with +// "window size must be non-zero". It now returns a graceful error, matching +// the sibling `chunks` function. +// expect-error: window size must be non-zero +windows([1, 2, 3], 0);