about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJosh Stone <jistone@redhat.com>2019-07-08 12:03:21 -0700
committerJosh Stone <jistone@redhat.com>2019-08-12 15:03:44 -0700
commitaf1bfbebe37ab6c3215b722c92d5b5a718553652 (patch)
tree9c054bdf11e9c843b574c1ecf5c95818084d0863
parent95e2a4f23df096ce61593b6a0910d67508228bc7 (diff)
downloadrust-af1bfbebe37ab6c3215b722c92d5b5a718553652.tar.gz
rust-af1bfbebe37ab6c3215b722c92d5b5a718553652.zip
Explicitly test Iterator::count overflows
-rw-r--r--src/libcore/iter/traits/iterator.rs7
-rw-r--r--src/test/run-pass/iterators/iter-count-overflow-debug.rs16
-rw-r--r--src/test/run-pass/iterators/iter-count-overflow-ndebug.rs11
3 files changed, 30 insertions, 4 deletions
diff --git a/src/libcore/iter/traits/iterator.rs b/src/libcore/iter/traits/iterator.rs
index 4220e85b8dd..41b23c6ba5e 100644
--- a/src/libcore/iter/traits/iterator.rs
+++ b/src/libcore/iter/traits/iterator.rs
@@ -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 @@ pub trait Iterator {
     #[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)
diff --git a/src/test/run-pass/iterators/iter-count-overflow-debug.rs b/src/test/run-pass/iterators/iter-count-overflow-debug.rs
new file mode 100644
index 00000000000..1e14142c5a6
--- /dev/null
+++ b/src/test/run-pass/iterators/iter-count-overflow-debug.rs
@@ -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());
+}
diff --git a/src/test/run-pass/iterators/iter-count-overflow-ndebug.rs b/src/test/run-pass/iterators/iter-count-overflow-ndebug.rs
new file mode 100644
index 00000000000..124aa8d2258
--- /dev/null
+++ b/src/test/run-pass/iterators/iter-count-overflow-ndebug.rs
@@ -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);
+}