fix silent overflows on Step impls

r? @eddyb
This commit is contained in:
Matthew Piziak 2016-09-09 12:21:01 -04:00
parent f1f40f850e
commit f6c4fcf128
2 changed files with 20 additions and 0 deletions

View file

@ -95,11 +95,13 @@ macro_rules! step_impl_unsigned {
}
#[inline]
#[rustc_inherit_overflow_checks]
fn add_one(&self) -> Self {
*self + 1
}
#[inline]
#[rustc_inherit_overflow_checks]
fn sub_one(&self) -> Self {
*self - 1
}
@ -166,11 +168,13 @@ macro_rules! step_impl_signed {
}
#[inline]
#[rustc_inherit_overflow_checks]
fn add_one(&self) -> Self {
*self + 1
}
#[inline]
#[rustc_inherit_overflow_checks]
fn sub_one(&self) -> Self {
*self - 1
}
@ -215,11 +219,13 @@ macro_rules! step_impl_no_between {
}
#[inline]
#[rustc_inherit_overflow_checks]
fn add_one(&self) -> Self {
*self + 1
}
#[inline]
#[rustc_inherit_overflow_checks]
fn sub_one(&self) -> Self {
*self - 1
}

View file

@ -915,6 +915,20 @@ fn test_range_step() {
assert_eq!((isize::MIN..isize::MAX).step_by(1).size_hint(), (usize::MAX, Some(usize::MAX)));
}
#[test]
#[should_panic]
fn test_range_overflow_unsigned() {
let mut it = u8::MAX..;
it.next();
}
#[test]
#[should_panic]
fn test_range_overflow_signed() {
let mut it = i8::MAX..;
it.next();
}
#[test]
fn test_repeat() {
let mut it = repeat(42);