Explicitly test Iterator::count overflows
This commit is contained in:
parent
95e2a4f23d
commit
af1bfbebe3
@ -1,5 +1,5 @@
|
||||
use crate::cmp::Ordering;
|
||||
use crate::ops::Try;
|
||||
use crate::ops::{Add, Try};
|
||||
|
||||
use super::super::LoopState;
|
||||
use super::super::{Chain, Cycle, Copied, Cloned, Enumerate, Filter, FilterMap, Fuse};
|
||||
@ -236,11 +236,10 @@ fn size_hint(&self) -> (usize, Option<usize>) { (0, None) }
|
||||
#[inline]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
fn count(self) -> usize where Self: Sized {
|
||||
// Might overflow.
|
||||
#[inline]
|
||||
#[rustc_inherit_overflow_checks]
|
||||
fn add1<T>(count: usize, _: T) -> usize {
|
||||
count + 1
|
||||
// Might overflow.
|
||||
Add::add(count, 1)
|
||||
}
|
||||
|
||||
self.fold(0, add1)
|
||||
|
16
src/test/run-pass/iterators/iter-count-overflow-debug.rs
Normal file
16
src/test/run-pass/iterators/iter-count-overflow-debug.rs
Normal file
@ -0,0 +1,16 @@
|
||||
// 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() {
|
||||
assert_eq!((0..MAX).by_ref().count(), MAX);
|
||||
|
||||
let r = panic::catch_unwind(|| {
|
||||
(0..=MAX).by_ref().count()
|
||||
});
|
||||
assert!(r.is_err());
|
||||
}
|
11
src/test/run-pass/iterators/iter-count-overflow-ndebug.rs
Normal file
11
src/test/run-pass/iterators/iter-count-overflow-ndebug.rs
Normal file
@ -0,0 +1,11 @@
|
||||
// run-pass
|
||||
// only-32bit too impatient for 2⁶⁴ items
|
||||
// compile-flags: -C debug_assertions=no
|
||||
|
||||
use std::panic;
|
||||
use std::usize::MAX;
|
||||
|
||||
fn main() {
|
||||
assert_eq!((0..MAX).by_ref().count(), MAX);
|
||||
assert_eq!((0..=MAX).by_ref().count(), 0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user