Skip to content

Commit b85c4d1

Browse files
committed
Fix bug in OverflowOps impl for unsigned integers.
Namely, the special case treatment for `div`/`rem` is only applicable to signed integer values. Clearly RFC 1027 would have saved us here! ;)
1 parent 1c11748 commit b85c4d1

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

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 {

0 commit comments

Comments
 (0)