diff options
| author | Ralf Jung <post@ralfj.de> | 2020-05-30 13:45:02 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-30 13:45:02 +0200 |
| commit | 93d45a01e79cf80ed5e4ffa85cdaa096c440dff3 (patch) | |
| tree | 44849ef9c57e43d4a2e96e650ebedb3f8e92a7a9 /src/libcore/iter | |
| parent | 35db8196f93a717173a8e58b076dd7c6d845629f (diff) | |
| parent | 406852ae0d92e5dfda890fa75ac522963065f903 (diff) | |
| download | rust-93d45a01e79cf80ed5e4ffa85cdaa096c440dff3.tar.gz rust-93d45a01e79cf80ed5e4ffa85cdaa096c440dff3.zip | |
Rollup merge of #72368 - CAD97:rangeto, r=dtolnay
Resolve overflow behavior for RangeFrom This specifies a documented unspecified implementation detail of `RangeFrom` and makes it consistently implement the specified behavior. Specifically, `(u8::MAX).next()` is defined to cause an overflow, and resolve that overflow in the same manner as the `Step::forward` implementation. The inconsistency that has existed is `<RangeFrom as Iterator>::nth`. The existing behavior should be plain to see after #69659: the skipping part previously always panicked if it caused an overflow, but the final step (to set up the state for further iteration) has always been debug-checked. The inconsistency, then, is that `RangeFrom::nth` does not implement the same behavior as the naive (and default) implementation of just calling `next` multiple times. This PR aligns `RangeFrom::nth` to have identical behavior to the naive implementation. It also lines up with the standard behavior of primitive math in Rust everywhere else in the language: debug checked overflow. cc @Amanieu --- Followup to #69659. Closes #25708 (by documenting the panic as intended). The documentation wording is preliminary and can probably be improved. This will probably need an FCP, as it changes observable stable behavior.
Diffstat (limited to 'src/libcore/iter')
| -rw-r--r-- | src/libcore/iter/range.rs | 10 |
1 files changed, 1 insertions, 9 deletions
diff --git a/src/libcore/iter/range.rs b/src/libcore/iter/range.rs index 57e3e8084dd..bd7e6cfa5a7 100644 --- a/src/libcore/iter/range.rs +++ b/src/libcore/iter/range.rs @@ -619,15 +619,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) } |
