Skip to content

Commit 9edbf42

Browse files
committed
rollup merge of #23945: pnkfelix/gate-u-negate
Feature-gate unsigned unary negate. Discussed in weekly meeting here: https://github.com/rust-lang/meeting-minutes/blob/master/weekly-meetings/2015-03-31.md#feature-gate--expr and also in the internals thread here: http://internals.rust-lang.org/t/forbid-unsigned-integer/752
2 parents 2e3b0c0 + f86318d commit 9edbf42

File tree

32 files changed

+146
-48
lines changed

32 files changed

+146
-48
lines changed

src/libcollections/slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ impl Iterator for ElementSwaps {
11351135
// #[inline]
11361136
fn next(&mut self) -> Option<(usize, usize)> {
11371137
fn new_pos_wrapping(i: usize, s: Direction) -> usize {
1138-
i.wrapping_add(match s { Pos => 1, Neg => -1 })
1138+
i.wrapping_add(match s { Pos => 1, Neg => !0 /* aka -1 */ })
11391139
}
11401140

11411141
fn new_pos(i: usize, s: Direction) -> usize {

src/libcollectionstest/slice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ fn test_bytes_set_memory() {
10891089
#[should_panic]
10901090
fn test_overflow_does_not_cause_segfault() {
10911091
let mut v = vec![];
1092-
v.reserve_exact(-1);
1092+
v.reserve_exact(!0);
10931093
v.push(1);
10941094
v.push(2);
10951095
}
@@ -1098,7 +1098,7 @@ fn test_overflow_does_not_cause_segfault() {
10981098
#[should_panic]
10991099
fn test_overflow_does_not_cause_segfault_managed() {
11001100
let mut v = vec![Rc::new(1)];
1101-
v.reserve_exact(-1);
1101+
v.reserve_exact(!0);
11021102
v.push(Rc::new(2));
11031103
}
11041104

src/libcore/atomic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub const ATOMIC_USIZE_INIT: AtomicUsize =
161161
AtomicUsize { v: UnsafeCell { value: 0, } };
162162

163163
// NB: Needs to be -1 (0b11111111...) to make fetch_nand work correctly
164-
const UINT_TRUE: usize = -1;
164+
const UINT_TRUE: usize = !0;
165165

166166
impl AtomicBool {
167167
/// Creates a new `AtomicBool`.

src/libcore/cell.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ pub enum BorrowState {
287287
// (will not outgrow its range since `usize` is the size of the address space)
288288
type BorrowFlag = usize;
289289
const UNUSED: BorrowFlag = 0;
290-
const WRITING: BorrowFlag = -1;
290+
const WRITING: BorrowFlag = !0;
291291

292292
impl<T> RefCell<T> {
293293
/// Creates a new `RefCell` containing `value`.

src/libcore/num/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ macro_rules! uint_impl {
516516
fn min_value() -> $T { 0 }
517517

518518
#[inline]
519-
fn max_value() -> $T { -1 }
519+
fn max_value() -> $T { !0 }
520520

521521
#[inline]
522522
fn count_ones(self) -> u32 {

src/libcore/num/wrapping.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use intrinsics::{i16_mul_with_overflow, u16_mul_with_overflow};
3030
use intrinsics::{i32_mul_with_overflow, u32_mul_with_overflow};
3131
use intrinsics::{i64_mul_with_overflow, u64_mul_with_overflow};
3232

33-
use ::{i8,i16,i32,i64,u8,u16,u32,u64};
33+
use ::{i8,i16,i32,i64};
3434

3535
#[unstable(feature = "core", reason = "may be removed, renamed, or relocated")]
3636
#[deprecated(since = "1.0.0", reason = "moved to inherent methods")]
@@ -206,7 +206,7 @@ mod shift_max {
206206
pub const u64: u32 = i64;
207207
}
208208

209-
macro_rules! overflowing_impl {
209+
macro_rules! signed_overflowing_impl {
210210
($($t:ident)*) => ($(
211211
impl OverflowingOps for $t {
212212
#[inline(always)]
@@ -259,7 +259,53 @@ macro_rules! overflowing_impl {
259259
)*)
260260
}
261261

262-
overflowing_impl! { u8 u16 u32 u64 i8 i16 i32 i64 }
262+
macro_rules! unsigned_overflowing_impl {
263+
($($t:ident)*) => ($(
264+
impl OverflowingOps for $t {
265+
#[inline(always)]
266+
fn overflowing_add(self, rhs: $t) -> ($t, bool) {
267+
unsafe {
268+
concat_idents!($t, _add_with_overflow)(self, rhs)
269+
}
270+
}
271+
#[inline(always)]
272+
fn overflowing_sub(self, rhs: $t) -> ($t, bool) {
273+
unsafe {
274+
concat_idents!($t, _sub_with_overflow)(self, rhs)
275+
}
276+
}
277+
#[inline(always)]
278+
fn overflowing_mul(self, rhs: $t) -> ($t, bool) {
279+
unsafe {
280+
concat_idents!($t, _mul_with_overflow)(self, rhs)
281+
}
282+
}
283+
284+
#[inline(always)]
285+
fn overflowing_div(self, rhs: $t) -> ($t, bool) {
286+
(self/rhs, false)
287+
}
288+
#[inline(always)]
289+
fn overflowing_rem(self, rhs: $t) -> ($t, bool) {
290+
(self % rhs, false)
291+
}
292+
293+
#[inline(always)]
294+
fn overflowing_shl(self, rhs: u32) -> ($t, bool) {
295+
(self << (rhs & self::shift_max::$t),
296+
(rhs > self::shift_max::$t))
297+
}
298+
#[inline(always)]
299+
fn overflowing_shr(self, rhs: u32) -> ($t, bool) {
300+
(self >> (rhs & self::shift_max::$t),
301+
(rhs > self::shift_max::$t))
302+
}
303+
}
304+
)*)
305+
}
306+
307+
signed_overflowing_impl! { i8 i16 i32 i64 }
308+
unsigned_overflowing_impl! { u8 u16 u32 u64 }
263309

264310
#[cfg(target_pointer_width = "64")]
265311
impl OverflowingOps for usize {

src/libcore/ops.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,10 @@ pub trait Neg {
482482
fn neg(self) -> Self::Output;
483483
}
484484

485-
macro_rules! neg_impl {
486-
($($t:ty)*) => ($(
485+
486+
487+
macro_rules! neg_impl_core {
488+
($id:ident => $body:expr, $($t:ty)*) => ($(
487489
#[stable(feature = "rust1", since = "1.0.0")]
488490
#[allow(unsigned_negation)]
489491
impl Neg for $t {
@@ -492,14 +494,28 @@ macro_rules! neg_impl {
492494

493495
#[inline]
494496
#[stable(feature = "rust1", since = "1.0.0")]
495-
fn neg(self) -> $t { -self }
497+
fn neg(self) -> $t { let $id = self; $body }
496498
}
497499

498500
forward_ref_unop! { impl Neg, neg for $t }
499501
)*)
500502
}
501503

502-
neg_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
504+
macro_rules! neg_impl_numeric {
505+
($($t:ty)*) => { neg_impl_core!{ x => -x, $($t)*} }
506+
}
507+
508+
macro_rules! neg_impl_unsigned {
509+
($($t:ty)*) => {
510+
neg_impl_core!{ x => {
511+
#[cfg(stage0)]
512+
use ::num::wrapping::WrappingOps;
513+
!x.wrapping_add(1)
514+
}, $($t)*} }
515+
}
516+
517+
// neg_impl_unsigned! { usize u8 u16 u32 u64 }
518+
neg_impl_numeric! { isize i8 i16 i32 i64 f32 f64 }
503519

504520
/// The `Not` trait is used to specify the functionality of unary `!`.
505521
///

src/libcore/str/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ impl TwoWaySearcher {
855855
#[allow(dead_code)]
856856
#[allow(deprecated)]
857857
fn maximal_suffix(arr: &[u8], reversed: bool) -> (usize, usize) {
858-
let mut left: usize = -1; // Corresponds to i in the paper
858+
let mut left: usize = !0; // Corresponds to i in the paper
859859
let mut right = 0; // Corresponds to j in the paper
860860
let mut offset = 1; // Corresponds to k in the paper
861861
let mut period = 1; // Corresponds to p in the paper

src/libcoretest/fmt/num.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ fn test_format_int_flags() {
125125
assert!(format!("{:>8x}", 10) == " a");
126126
assert!(format!("{:#08x}", 10) == "0x00000a");
127127
assert!(format!("{:08}", -10) == "-0000010");
128-
assert!(format!("{:x}", -1u8) == "ff");
129-
assert!(format!("{:X}", -1u8) == "FF");
130-
assert!(format!("{:b}", -1u8) == "11111111");
131-
assert!(format!("{:o}", -1u8) == "377");
132-
assert!(format!("{:#x}", -1u8) == "0xff");
133-
assert!(format!("{:#X}", -1u8) == "0xFF");
134-
assert!(format!("{:#b}", -1u8) == "0b11111111");
135-
assert!(format!("{:#o}", -1u8) == "0o377");
128+
assert!(format!("{:x}", !0u8) == "ff");
129+
assert!(format!("{:X}", !0u8) == "FF");
130+
assert!(format!("{:b}", !0u8) == "11111111");
131+
assert!(format!("{:o}", !0u8) == "377");
132+
assert!(format!("{:#x}", !0u8) == "0xff");
133+
assert!(format!("{:#X}", !0u8) == "0xFF");
134+
assert!(format!("{:#b}", !0u8) == "0b11111111");
135+
assert!(format!("{:#o}", !0u8) == "0o377");
136136
}
137137

138138
#[test]

src/liblibc/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2865,7 +2865,7 @@ pub mod consts {
28652865
pub const MAP_FIXED : c_int = 0x0010;
28662866
pub const MAP_ANON : c_int = 0x0020;
28672867

2868-
pub const MAP_FAILED : *mut c_void = -1 as *mut c_void;
2868+
pub const MAP_FAILED : *mut c_void = !0 as *mut c_void;
28692869

28702870
pub const MCL_CURRENT : c_int = 0x0001;
28712871
pub const MCL_FUTURE : c_int = 0x0002;
@@ -4696,7 +4696,7 @@ pub mod consts {
46964696
pub const MAP_FIXED : c_int = 0x0010;
46974697
pub const MAP_ANON : c_int = 0x1000;
46984698

4699-
pub const MAP_FAILED : *mut c_void = -1 as *mut c_void;
4699+
pub const MAP_FAILED : *mut c_void = !0 as *mut c_void;
47004700

47014701
pub const MCL_CURRENT : c_int = 0x0001;
47024702
pub const MCL_FUTURE : c_int = 0x0002;

0 commit comments

Comments
 (0)