Skip to content

Commit b58b950

Browse files
committed
Merge from rustc
2 parents 22a93b8 + 8850f4e commit b58b950

File tree

3 files changed

+378
-90
lines changed

3 files changed

+378
-90
lines changed

core/src/num/int_macros.rs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
11
macro_rules! int_impl {
2-
($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr, $BITS_MINUS_ONE:expr, $Min:expr, $Max:expr,
3-
$rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
4-
$reversed:expr, $le_bytes:expr, $be_bytes:expr,
5-
$to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr,
6-
$bound_condition:expr) => {
2+
(
3+
Self = $SelfT:ty,
4+
ActualT = $ActualT:ident,
5+
UnsignedT = $UnsignedT:ty,
6+
7+
// There are all for use *only* in doc comments.
8+
// As such, they're all passed as literals -- passing them as a string
9+
// literal is fine if they need to be multiple code tokens.
10+
// In non-comments, use the associated constants rather than these.
11+
BITS = $BITS:literal,
12+
BITS_MINUS_ONE = $BITS_MINUS_ONE:literal,
13+
Min = $Min:literal,
14+
Max = $Max:literal,
15+
rot = $rot:literal,
16+
rot_op = $rot_op:literal,
17+
rot_result = $rot_result:literal,
18+
swap_op = $swap_op:literal,
19+
swapped = $swapped:literal,
20+
reversed = $reversed:literal,
21+
le_bytes = $le_bytes:literal,
22+
be_bytes = $be_bytes:literal,
23+
to_xe_bytes_doc = $to_xe_bytes_doc:expr,
24+
from_xe_bytes_doc = $from_xe_bytes_doc:expr,
25+
bound_condition = $bound_condition:literal,
26+
) => {
727
/// The smallest value that can be represented by this integer type
828
#[doc = concat!("(&minus;2<sup>", $BITS_MINUS_ONE, "</sup>", $bound_condition, ").")]
929
///
@@ -15,7 +35,7 @@ macro_rules! int_impl {
1535
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN, ", stringify!($Min), ");")]
1636
/// ```
1737
#[stable(feature = "assoc_int_consts", since = "1.43.0")]
18-
pub const MIN: Self = !0 ^ ((!0 as $UnsignedT) >> 1) as Self;
38+
pub const MIN: Self = !Self::MAX;
1939

2040
/// The largest value that can be represented by this integer type
2141
#[doc = concat!("(2<sup>", $BITS_MINUS_ONE, "</sup> &minus; 1", $bound_condition, ").")]
@@ -28,7 +48,7 @@ macro_rules! int_impl {
2848
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($Max), ");")]
2949
/// ```
3050
#[stable(feature = "assoc_int_consts", since = "1.43.0")]
31-
pub const MAX: Self = !Self::MIN;
51+
pub const MAX: Self = (<$UnsignedT>::MAX >> 1) as Self;
3252

3353
/// The size of this integer type in bits.
3454
///
@@ -38,7 +58,7 @@ macro_rules! int_impl {
3858
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::BITS, ", stringify!($BITS), ");")]
3959
/// ```
4060
#[stable(feature = "int_bits_const", since = "1.53.0")]
41-
pub const BITS: u32 = $BITS;
61+
pub const BITS: u32 = <$UnsignedT>::BITS;
4262

4363
/// Converts a string slice in a given base to an integer.
4464
///
@@ -1365,7 +1385,7 @@ macro_rules! int_impl {
13651385
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
13661386
// out of bounds
13671387
unsafe {
1368-
self.unchecked_shl(rhs & ($BITS - 1))
1388+
self.unchecked_shl(rhs & (Self::BITS - 1))
13691389
}
13701390
}
13711391

@@ -1395,7 +1415,7 @@ macro_rules! int_impl {
13951415
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
13961416
// out of bounds
13971417
unsafe {
1398-
self.unchecked_shr(rhs & ($BITS - 1))
1418+
self.unchecked_shr(rhs & (Self::BITS - 1))
13991419
}
14001420
}
14011421

@@ -1901,7 +1921,7 @@ macro_rules! int_impl {
19011921
without modifying the original"]
19021922
#[inline]
19031923
pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
1904-
(self.wrapping_shl(rhs), (rhs > ($BITS - 1)))
1924+
(self.wrapping_shl(rhs), rhs >= Self::BITS)
19051925
}
19061926

19071927
/// Shifts self right by `rhs` bits.
@@ -1924,7 +1944,7 @@ macro_rules! int_impl {
19241944
without modifying the original"]
19251945
#[inline]
19261946
pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
1927-
(self.wrapping_shr(rhs), (rhs > ($BITS - 1)))
1947+
(self.wrapping_shr(rhs), rhs >= Self::BITS)
19281948
}
19291949

19301950
/// Computes the absolute value of `self`.

0 commit comments

Comments
 (0)