diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 8cea98d2a21..64933d0acf5 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -4665,11 +4665,15 @@ impl DoubleEndedIterator for ops::RangeInclusive where NonEmpty { ref mut start, ref mut end } => { let one = A::one(); - let mut n = &*end - &one; - mem::swap(&mut n, end); + if start <= end { + let mut n = &*end - &one; + mem::swap(&mut n, end); - (if n == *start { Some(mem::replace(start, one)) } else { None }, - n) + (if n == *start { Some(mem::replace(start, one)) } else { None }, + Some(n)) + } else { + (Some(mem::replace(end, one)), None) + } } }; @@ -4677,7 +4681,7 @@ impl DoubleEndedIterator for ops::RangeInclusive where *self = Empty { at: start }; } - Some(n) + n } } diff --git a/src/test/run-pass/range_inclusive.rs b/src/test/run-pass/range_inclusive.rs index 52c8353d00e..970ea7cc078 100644 --- a/src/test/run-pass/range_inclusive.rs +++ b/src/test/run-pass/range_inclusive.rs @@ -99,6 +99,11 @@ pub fn main() { } assert_eq!(long, RangeInclusive::Empty { at: 251 }); + // check underflow + let mut narrow = 1...0; + assert_eq!(narrow.next_back(), None); + assert_eq!(narrow, RangeInclusive::Empty { at: 0 }); + // what happens if you have a nonsense range? let mut nonsense = 10...5; assert_eq!(nonsense.next(), None);