From 2c1bfd9c631cc901c0b0be2f3a1cccff6fc07a56 Mon Sep 17 00:00:00 2001 From: Vibhu Gupta Date: Wed, 21 Oct 2026 09:35:56 +0000 Subject: [PATCH] Make RangeInclusive::spec_try_(r)fold set exhausted consistently with next/next_back --- library/core/src/iter/range.rs | 36 ++++++++++++--------------- library/coretests/tests/iter/range.rs | 31 +++++++++++++++++++++++ 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/library/core/src/iter/range.rs b/library/core/src/iter/range.rs index 331c367314e63..e8c6f9d01da7d 100644 --- a/library/core/src/iter/range.rs +++ b/library/core/src/iter/range.rs @@ -1431,11 +1431,10 @@ impl RangeInclusiveIteratorImpl for ops::RangeInclusive { accum = f(accum, n)?; } - self.exhausted = true; - - if self.start == self.end { - accum = f(accum, self.start.clone())?; - } + let (n, o) = Step::forward_overflowing(self.start.clone(), 1); + let last = mem::replace(&mut self.start, n); + self.exhausted = o; + accum = f(accum, last)?; try { accum } } @@ -1460,11 +1459,10 @@ impl RangeInclusiveIteratorImpl for ops::RangeInclusive { accum = f(accum, n)?; } - self.exhausted = true; - - if self.start == self.end { - accum = f(accum, self.start.clone())?; - } + let (n, o) = Step::backward_overflowing(self.end.clone(), 1); + let last = mem::replace(&mut self.end, n); + self.exhausted = o; + accum = f(accum, last)?; try { accum } } @@ -1491,11 +1489,10 @@ impl RangeInclusiveIteratorImpl for ops::RangeInclusive { accum = f(accum, n)?; } - self.exhausted = true; - - if self.start == self.end { - accum = f(accum, self.start)?; - } + let (n, o) = Step::forward_overflowing(self.start, 1); + let last = mem::replace(&mut self.start, n); + self.exhausted = o; + accum = f(accum, last)?; try { accum } } @@ -1520,11 +1517,10 @@ impl RangeInclusiveIteratorImpl for ops::RangeInclusive { accum = f(accum, n)?; } - self.exhausted = true; - - if self.start == self.end { - accum = f(accum, self.start)?; - } + let (n, o) = Step::backward_overflowing(self.end, 1); + let last = mem::replace(&mut self.end, n); + self.exhausted = o; + accum = f(accum, last)?; try { accum } } diff --git a/library/coretests/tests/iter/range.rs b/library/coretests/tests/iter/range.rs index c9904f08c4240..b848544818c85 100644 --- a/library/coretests/tests/iter/range.rs +++ b/library/coretests/tests/iter/range.rs @@ -675,3 +675,34 @@ fn test_nonzero_range() { (usize::MAX, Some(usize::MAX)) ); } + +#[test] +fn test_range_inclusive_try_fold_state_matches_next() { + // normal range + let mut a = 1..=3; + let mut b = 1..=3; + a.try_fold((), |_, _| Some(())); + for _ in b.by_ref() {} + assert_eq!(a, b); + + // overflow at the end of the type + let mut a = 250u8..=255; + let mut b = 250u8..=255; + a.try_fold((), |_, _| Some(())); + for _ in b.by_ref() {} + assert_eq!(a, b); + + // try_rfold, and overflow at the start of the type + let mut a = 0u8..=5; + let mut b = 0u8..=5; + a.try_rfold((), |_, _| Some(())); + while b.next_back().is_some() {} + assert_eq!(a, b); + + // early break on the last element + let mut a = 1..=3; + let mut b = 1..=3; + a.try_fold((), |_, x| if x == 3 { None } else { Some(()) }); + for _ in b.by_ref() {} + assert_eq!(a, b); +}