1
Fork 0

Auto merge of #36365 - matthew-piziak:silent-overflow, r=eddyb

fix silent overflows on `Step` impls

Part of https://github.com/rust-lang/rust/issues/36110

r? @eddyb
This commit is contained in:
bors 2016-11-07 11:48:16 -08:00 committed by GitHub
commit 57f971bc16
3 changed files with 56 additions and 6 deletions

View file

@ -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]

View file

@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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());
}

View file

@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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());
}