diff options
| author | CAD97 <cad97@cad97.com> | 2020-05-19 20:00:29 -0400 |
|---|---|---|
| committer | CAD97 <cad97@cad97.com> | 2020-05-19 22:31:31 -0400 |
| commit | 406852ae0d92e5dfda890fa75ac522963065f903 (patch) | |
| tree | 5cabab00bbffa68a3f7c96f86e7d311116935591 | |
| parent | 3a7dfda40a3e798bf086bd58cc7e5e09deb808b5 (diff) | |
| download | rust-406852ae0d92e5dfda890fa75ac522963065f903.tar.gz rust-406852ae0d92e5dfda890fa75ac522963065f903.zip | |
Resolve overflow behavior for RangeFrom
| -rw-r--r-- | src/libcore/iter/range.rs | 10 | ||||
| -rw-r--r-- | src/libcore/ops/range.rs | 14 |
2 files changed, 11 insertions, 13 deletions
diff --git a/src/libcore/iter/range.rs b/src/libcore/iter/range.rs index d74df82bddd..75cbda34662 100644 --- a/src/libcore/iter/range.rs +++ b/src/libcore/iter/range.rs @@ -551,15 +551,7 @@ impl<A: Step> Iterator for ops::RangeFrom<A> { #[inline] fn nth(&mut self, n: usize) -> Option<A> { - // If we would jump over the maximum value, panic immediately. - // This is consistent with behavior before the Step redesign, - // even though it's inconsistent with n `next` calls. - // To get consistent behavior, change it to use `forward` instead. - // This change should go through FCP separately to the redesign, so is for now left as a - // FIXME: make this consistent - let plus_n = - Step::forward_checked(self.start.clone(), n).expect("overflow in RangeFrom::nth"); - // The final step should always be debug-checked. + let plus_n = Step::forward(self.start.clone(), n); self.start = Step::forward(plus_n.clone(), 1); Some(plus_n) } diff --git a/src/libcore/ops/range.rs b/src/libcore/ops/range.rs index d4e6048579a..d86f39c4550 100644 --- a/src/libcore/ops/range.rs +++ b/src/libcore/ops/range.rs @@ -151,10 +151,16 @@ impl<Idx: PartialOrd<Idx>> Range<Idx> { /// /// The `RangeFrom` `start..` contains all values with `x >= start`. /// -/// *Note*: Currently, no overflow checking is done for the [`Iterator`] -/// implementation; if you use an integer range and the integer overflows, it -/// might panic in debug mode or create an endless loop in release mode. **This -/// overflow behavior might change in the future.** +/// *Note*: Overflow in the [`Iterator`] implementation (when the contained +/// data type reaches its numerical limit) is allowed to panic, wrap, or +/// saturate. This behavior is defined by the implementation of the [`Step`] +/// trait. For primitive integers, this follows the normal rules, and respects +/// the overflow checks profile (panic in debug, wrap in release). Note also +/// that overflow happens earlier than you might assume: the overflow happens +/// in the call to `next` that yields the maximum value, as the range must be +/// set to a state to yield the next value. +/// +/// [`Step`]: crate::iter::Step /// /// # Examples /// |
