From 4fed67f94220351ffa60de1dca078c02a7c15734 Mon Sep 17 00:00:00 2001 From: Matthieu M Date: Sat, 9 Feb 2019 18:42:34 +0100 Subject: [PATCH] Fix exhaustion of inclusive range try_fold and try_rfold --- src/libcore/iter/range.rs | 14 ++++++++++++-- src/libcore/tests/iter.rs | 24 +++++++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/libcore/iter/range.rs b/src/libcore/iter/range.rs index 52b0ccd48d4..f0ed88c3dfd 100644 --- a/src/libcore/iter/range.rs +++ b/src/libcore/iter/range.rs @@ -389,6 +389,10 @@ impl Iterator for ops::RangeInclusive { { self.compute_is_empty(); + if self.is_empty() { + return Try::from_ok(init); + } + let mut accum = init; while self.start < self.end { @@ -397,11 +401,12 @@ impl Iterator for ops::RangeInclusive { accum = f(accum, n)?; } + self.is_empty = Some(true); + if self.start == self.end { accum = f(accum, self.start.clone())?; } - self.is_empty = Some(true); Try::from_ok(accum) } @@ -445,6 +450,10 @@ impl DoubleEndedIterator for ops::RangeInclusive { { self.compute_is_empty(); + if self.is_empty() { + return Try::from_ok(init); + } + let mut accum = init; while self.start < self.end { @@ -453,11 +462,12 @@ impl DoubleEndedIterator for ops::RangeInclusive { accum = f(accum, n)?; } + self.is_empty = Some(true); + if self.start == self.end { accum = f(accum, self.start.clone())?; } - self.is_empty = Some(true); Try::from_ok(accum) } } diff --git a/src/libcore/tests/iter.rs b/src/libcore/tests/iter.rs index cf19851c17b..89e190e074f 100644 --- a/src/libcore/tests/iter.rs +++ b/src/libcore/tests/iter.rs @@ -1738,19 +1738,37 @@ fn test_range_inclusive_folds() { assert_eq!((1..=10).sum::(), 55); assert_eq!((1..=10).rev().sum::(), 55); - let mut it = 40..=50; + let mut it = 44..=50; assert_eq!(it.try_fold(0, i8::checked_add), None); - assert_eq!(it, 44..=50); + assert_eq!(it, 47..=50); + assert_eq!(it.try_fold(0, i8::checked_add), None); + assert_eq!(it, 50..=50); + assert_eq!(it.try_fold(0, i8::checked_add), Some(50)); + assert!(it.is_empty()); + assert_eq!(it.try_fold(0, i8::checked_add), Some(0)); + assert!(it.is_empty()); + + let mut it = 40..=47; assert_eq!(it.try_rfold(0, i8::checked_add), None); - assert_eq!(it, 44..=47); + assert_eq!(it, 40..=44); + assert_eq!(it.try_rfold(0, i8::checked_add), None); + assert_eq!(it, 40..=41); + assert_eq!(it.try_rfold(0, i8::checked_add), Some(81)); + assert!(it.is_empty()); + assert_eq!(it.try_rfold(0, i8::checked_add), Some(0)); + assert!(it.is_empty()); let mut it = 10..=20; assert_eq!(it.try_fold(0, |a,b| Some(a+b)), Some(165)); assert!(it.is_empty()); + assert_eq!(it.try_fold(0, |a,b| Some(a+b)), Some(0)); + assert!(it.is_empty()); let mut it = 10..=20; assert_eq!(it.try_rfold(0, |a,b| Some(a+b)), Some(165)); assert!(it.is_empty()); + assert_eq!(it.try_rfold(0, |a,b| Some(a+b)), Some(0)); + assert!(it.is_empty()); } #[test]