diff --git a/src/libcore/iter/range.rs b/src/libcore/iter/range.rs index 7c96be2facb48..e6f21d6c17ae0 100644 --- a/src/libcore/iter/range.rs +++ b/src/libcore/iter/range.rs @@ -96,12 +96,12 @@ macro_rules! step_impl_unsigned { #[inline] fn add_one(&self) -> Self { - *self + 1 + Add::add(*self, 1) } #[inline] fn sub_one(&self) -> Self { - *self - 1 + Sub::sub(*self, 1) } #[inline] @@ -167,12 +167,12 @@ macro_rules! step_impl_signed { #[inline] fn add_one(&self) -> Self { - *self + 1 + Add::add(*self, 1) } #[inline] fn sub_one(&self) -> Self { - *self - 1 + Sub::sub(*self, 1) } #[inline] @@ -216,12 +216,12 @@ macro_rules! step_impl_no_between { #[inline] fn add_one(&self) -> Self { - *self + 1 + Add::add(*self, 1) } #[inline] fn sub_one(&self) -> Self { - *self - 1 + Sub::sub(*self, 1) } #[inline] diff --git a/src/test/run-pass/iter-step-overflow-debug.rs b/src/test/run-pass/iter-step-overflow-debug.rs new file mode 100644 index 0000000000000..5b9b58f028894 --- /dev/null +++ b/src/test/run-pass/iter-step-overflow-debug.rs @@ -0,0 +1,29 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: -C debug_assertions=yes + +use std::panic; + +fn main() { + let r = panic::catch_unwind(|| { + let mut it = u8::max_value()..; + it.next().unwrap(); // 255 + it.next().unwrap(); + }); + assert!(r.is_err()); + + let r = panic::catch_unwind(|| { + let mut it = i8::max_value()..; + it.next().unwrap(); // 127 + it.next().unwrap(); + }); + assert!(r.is_err()); +} diff --git a/src/test/run-pass/iter-step-overflow-ndebug.rs b/src/test/run-pass/iter-step-overflow-ndebug.rs new file mode 100644 index 0000000000000..8642f1643c11c --- /dev/null +++ b/src/test/run-pass/iter-step-overflow-ndebug.rs @@ -0,0 +1,21 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: -C debug_assertions=no + +fn main() { + let mut it = u8::max_value()..; + assert_eq!(it.next().unwrap(), 255); + assert_eq!(it.next().unwrap(), u8::min_value()); + + let mut it = i8::max_value()..; + assert_eq!(it.next().unwrap(), 127); + assert_eq!(it.next().unwrap(), i8::min_value()); +}