Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/libcore/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ macro_rules! step_impl_unsigned {
}

#[inline]
#[rustc_inherit_overflow_checks]
fn add_one(&self) -> Self {
*self + 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add::add(*self, 1) is what @alexcrichton meant. Similar situation on the rest.

}

#[inline]
#[rustc_inherit_overflow_checks]
fn sub_one(&self) -> Self {
*self - 1
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/run-fail/iter-step-overflow-debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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..;
it.next();
});
assert!(r.is_err());

let r = panic::catch_unwind(|| {
let mut it = i8::MAX..;
it.next();
});
assert!(r.is_err());
}
19 changes: 19 additions & 0 deletions src/test/run-fail/iter-step-overflow-ndebug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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..;
assert_eq!(it.next(), u8::MIN);

let mut it = i8::MAX..;
assert_eq!(it.next(), i8::MIN);
}