diff options
| author | Josh Stone <jistone@redhat.com> | 2019-07-08 12:10:58 -0700 |
|---|---|---|
| committer | Josh Stone <jistone@redhat.com> | 2019-08-12 15:03:44 -0700 |
| commit | 6a04c762ff15a07d8aa3110a661aa8a3b106cbe8 (patch) | |
| tree | 5a20c150467cd97b8f68608dedd08ae5b3064e26 /src | |
| parent | af1bfbebe37ab6c3215b722c92d5b5a718553652 (diff) | |
| download | rust-6a04c762ff15a07d8aa3110a661aa8a3b106cbe8.tar.gz rust-6a04c762ff15a07d8aa3110a661aa8a3b106cbe8.zip | |
Explicitly test Iterator::position overflows
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/iter/traits/iterator.rs | 5 | ||||
| -rw-r--r-- | src/test/run-pass/iterators/iter-position-overflow-debug.rs | 22 | ||||
| -rw-r--r-- | src/test/run-pass/iterators/iter-position-overflow-ndebug.rs | 13 |
3 files changed, 37 insertions, 3 deletions
diff --git a/src/libcore/iter/traits/iterator.rs b/src/libcore/iter/traits/iterator.rs index 41b23c6ba5e..955d643fb69 100644 --- a/src/libcore/iter/traits/iterator.rs +++ b/src/libcore/iter/traits/iterator.rs @@ -2069,15 +2069,14 @@ pub trait Iterator { Self: Sized, P: FnMut(Self::Item) -> bool, { - // The addition might panic on overflow #[inline] - #[rustc_inherit_overflow_checks] fn check<T>( mut predicate: impl FnMut(T) -> bool, ) -> impl FnMut(usize, T) -> LoopState<usize, usize> { + // The addition might panic on overflow move |i, x| { if predicate(x) { LoopState::Break(i) } - else { LoopState::Continue(i + 1) } + else { LoopState::Continue(Add::add(i, 1)) } } } diff --git a/src/test/run-pass/iterators/iter-position-overflow-debug.rs b/src/test/run-pass/iterators/iter-position-overflow-debug.rs new file mode 100644 index 00000000000..68e4646fe2d --- /dev/null +++ b/src/test/run-pass/iterators/iter-position-overflow-debug.rs @@ -0,0 +1,22 @@ +// run-pass +// only-32bit too impatient for 2⁶⁴ items +// ignore-wasm32-bare compiled with panic=abort by default +// compile-flags: -C debug_assertions=yes + +use std::panic; +use std::usize::MAX; + +fn main() { + let n = MAX as u64; + assert_eq!((0..).by_ref().position(|i| i >= n), Some(MAX)); + + let r = panic::catch_unwind(|| { + (0..).by_ref().position(|i| i > n) + }); + assert!(r.is_err()); + + let r = panic::catch_unwind(|| { + (0..=n + 1).by_ref().position(|_| false) + }); + assert!(r.is_err()); +} diff --git a/src/test/run-pass/iterators/iter-position-overflow-ndebug.rs b/src/test/run-pass/iterators/iter-position-overflow-ndebug.rs new file mode 100644 index 00000000000..3bdedf2aff1 --- /dev/null +++ b/src/test/run-pass/iterators/iter-position-overflow-ndebug.rs @@ -0,0 +1,13 @@ +// run-pass +// only-32bit too impatient for 2⁶⁴ items +// compile-flags: -C debug_assertions=no + +use std::panic; +use std::usize::MAX; + +fn main() { + let n = MAX as u64; + assert_eq!((0..).by_ref().position(|i| i >= n), Some(MAX)); + assert_eq!((0..).by_ref().position(|i| i > n), Some(0)); + assert_eq!((0..=n + 1).by_ref().position(|_| false), None); +} |
