Skip to content

Commit 78c4b4f

Browse files
committed
Fix saturating_shl for zero on signed integers
1 parent e6b0590 commit 78c4b4f

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

library/core/src/num/int_macros.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1002,11 +1002,12 @@ macro_rules! int_impl {
10021002
///
10031003
/// ```
10041004
/// #![feature(saturating_bit_shifts)]
1005+
#[doc = concat!("assert_eq!(0_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS), 0);")]
10051006
#[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS - 2), 1_", stringify!($SelfT), " << ", stringify!($SelfT), "::BITS - 2);")]
10061007
#[doc = concat!("assert_eq!(1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS - 1), ", stringify!($SelfT), "::MAX);")]
10071008
#[doc = concat!("assert_eq!(-1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS - 2), -1_", stringify!($SelfT), " << ", stringify!($SelfT), "::BITS - 2);")]
10081009
#[doc = concat!("assert_eq!(-1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS - 1), ", stringify!($SelfT), "::MIN);")]
1009-
#[doc = concat!("assert_eq!(-1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS), ", stringify!($SelfT), "::MIN);")]
1010+
#[doc = concat!("assert_eq!(-1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS), ", stringify!($SelfT), "::MIN, \"-1_", stringify!($SelfT), ".saturating_shl(", stringify!($SelfT), "::BITS), ", stringify!($SelfT), "::MIN\");")]
10101011
/// ```
10111012
#[unstable(feature = "saturating_bit_shifts", issue = "103440")]
10121013
#[rustc_const_unstable(feature = "saturating_bit_shifts", issue = "103440")]
@@ -1022,10 +1023,10 @@ macro_rules! int_impl {
10221023
let leading_ones = (self << 1).leading_ones();
10231024

10241025
// would overflow => MIN / MAX depending on whether the value is negative or not
1025-
if self >= 0 && leading_zeros < rhs {
1026-
<$SelfT>::MAX
1026+
if self > 0 && leading_zeros < rhs {
1027+
Self::MAX
10271028
} else if self < 0 && leading_ones < rhs {
1028-
<$SelfT>::MIN
1029+
Self::MIN
10291030
} else {
10301031
// normal shift left
10311032
self << rhs
@@ -1046,7 +1047,7 @@ macro_rules! int_impl {
10461047
///
10471048
/// ```
10481049
/// #![feature(saturating_bit_shifts)]
1049-
#[doc = concat!("assert_eq!(0b1000_0000_u8.saturating_shr(u8::BITS), 0b0000_0000_u8);")]
1050+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_shr(", stringify!($SelfT), "::BITS - 1), 0);")]
10501051
/// ```
10511052
#[unstable(feature = "saturating_bit_shifts", issue = "103440")]
10521053
#[rustc_const_unstable(feature = "saturating_bit_shifts", issue = "103440")]

0 commit comments

Comments
 (0)