Skip to content

Migrate to numeric associated consts #72906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2020
Merged
Changes from all 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
4 changes: 2 additions & 2 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
@@ -2034,7 +2034,7 @@ trait RcBoxPtr<T: ?Sized> {
// The reference count will never be zero when this is called;
// nevertheless, we insert an abort here to hint LLVM at
// an otherwise missed optimization.
if strong == 0 || strong == usize::max_value() {
if strong == 0 || strong == usize::MAX {
abort();
}
self.inner().strong.set(strong + 1);
@@ -2058,7 +2058,7 @@ trait RcBoxPtr<T: ?Sized> {
// The reference count will never be zero when this is called;
// nevertheless, we insert an abort here to hint LLVM at
// an otherwise missed optimization.
if weak == 0 || weak == usize::max_value() {
if weak == 0 || weak == usize::MAX {
abort();
}
self.inner().weak.set(weak + 1);
4 changes: 2 additions & 2 deletions src/liballoc/rc/tests.rs
Original file line number Diff line number Diff line change
@@ -407,14 +407,14 @@ fn test_from_vec() {
fn test_downcast() {
use std::any::Any;

let r1: Rc<dyn Any> = Rc::new(i32::max_value());
let r1: Rc<dyn Any> = Rc::new(i32::MAX);
let r2: Rc<dyn Any> = Rc::new("abc");

assert!(r1.clone().downcast::<u32>().is_err());

let r1i32 = r1.downcast::<i32>();
assert!(r1i32.is_ok());
assert_eq!(r1i32.unwrap(), Rc::new(i32::max_value()));
assert_eq!(r1i32.unwrap(), Rc::new(i32::MAX));

assert!(r2.clone().downcast::<i32>().is_err());

4 changes: 2 additions & 2 deletions src/liballoc/sync/tests.rs
Original file line number Diff line number Diff line change
@@ -465,14 +465,14 @@ fn test_from_vec() {
fn test_downcast() {
use std::any::Any;

let r1: Arc<dyn Any + Send + Sync> = Arc::new(i32::max_value());
let r1: Arc<dyn Any + Send + Sync> = Arc::new(i32::MAX);
let r2: Arc<dyn Any + Send + Sync> = Arc::new("abc");

assert!(r1.clone().downcast::<u32>().is_err());

let r1i32 = r1.downcast::<i32>();
assert!(r1i32.is_ok());
assert_eq!(r1i32.unwrap(), Arc::new(i32::max_value()));
assert_eq!(r1i32.unwrap(), Arc::new(i32::MAX));

assert!(r2.clone().downcast::<i32>().is_err());

4 changes: 2 additions & 2 deletions src/liballoc/tests/str.rs
Original file line number Diff line number Diff line change
@@ -566,13 +566,13 @@ mod slice_index {
data: "hello";
// note: using 0 specifically ensures that the result of overflowing is 0..0,
// so that `get` doesn't simply return None for the wrong reason.
bad: data[0..=usize::max_value()];
bad: data[0..=usize::MAX];
message: "maximum usize";
}

in mod rangetoinclusive {
data: "hello";
bad: data[..=usize::max_value()];
bad: data[..=usize::MAX];
message: "maximum usize";
}
}
18 changes: 9 additions & 9 deletions src/liballoc/tests/vec.rs
Original file line number Diff line number Diff line change
@@ -68,7 +68,7 @@ fn test_reserve() {

#[test]
fn test_zst_capacity() {
assert_eq!(Vec::<()>::new().capacity(), usize::max_value());
assert_eq!(Vec::<()>::new().capacity(), usize::MAX);
}

#[test]
@@ -563,19 +563,19 @@ fn test_drain_inclusive_range() {

#[test]
fn test_drain_max_vec_size() {
let mut v = Vec::<()>::with_capacity(usize::max_value());
let mut v = Vec::<()>::with_capacity(usize::MAX);
unsafe {
v.set_len(usize::max_value());
v.set_len(usize::MAX);
}
for _ in v.drain(usize::max_value() - 1..) {}
assert_eq!(v.len(), usize::max_value() - 1);
for _ in v.drain(usize::MAX - 1..) {}
assert_eq!(v.len(), usize::MAX - 1);

let mut v = Vec::<()>::with_capacity(usize::max_value());
let mut v = Vec::<()>::with_capacity(usize::MAX);
unsafe {
v.set_len(usize::max_value());
v.set_len(usize::MAX);
}
for _ in v.drain(usize::max_value() - 1..=usize::max_value() - 1) {}
assert_eq!(v.len(), usize::max_value() - 1);
for _ in v.drain(usize::MAX - 1..=usize::MAX - 1) {}
assert_eq!(v.len(), usize::MAX - 1);
}

#[test]
10 changes: 5 additions & 5 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
@@ -1163,16 +1163,16 @@ impl<'b> BorrowRef<'b> {
// Incrementing borrow can result in a non-reading value (<= 0) in these cases:
// 1. It was < 0, i.e. there are writing borrows, so we can't allow a read borrow
// due to Rust's reference aliasing rules
// 2. It was isize::max_value() (the max amount of reading borrows) and it overflowed
// into isize::min_value() (the max amount of writing borrows) so we can't allow
// 2. It was isize::MAX (the max amount of reading borrows) and it overflowed
// into isize::MIN (the max amount of writing borrows) so we can't allow
// an additional read borrow because isize can't represent so many read borrows
// (this can only happen if you mem::forget more than a small constant amount of
// `Ref`s, which is not good practice)
None
} else {
// Incrementing borrow can result in a reading value (> 0) in these cases:
// 1. It was = 0, i.e. it wasn't borrowed, and we are taking the first read borrow
// 2. It was > 0 and < isize::max_value(), i.e. there were read borrows, and isize
// 2. It was > 0 and < isize::MAX, i.e. there were read borrows, and isize
// is large enough to represent having one more read borrow
borrow.set(b);
Some(BorrowRef { borrow })
@@ -1198,7 +1198,7 @@ impl Clone for BorrowRef<'_> {
debug_assert!(is_reading(borrow));
// Prevent the borrow counter from overflowing into
// a writing borrow.
assert!(borrow != isize::max_value());
assert!(borrow != isize::MAX);
self.borrow.set(borrow + 1);
BorrowRef { borrow: self.borrow }
}
@@ -1489,7 +1489,7 @@ impl<'b> BorrowRefMut<'b> {
let borrow = self.borrow.get();
debug_assert!(is_writing(borrow));
// Prevent the borrow counter from underflowing.
assert!(borrow != isize::min_value());
assert!(borrow != isize::MIN);
self.borrow.set(borrow - 1);
BorrowRefMut { borrow: self.borrow }
}
6 changes: 3 additions & 3 deletions src/libcore/convert/num.rs
Original file line number Diff line number Diff line change
@@ -217,7 +217,7 @@ macro_rules! try_from_upper_bounded {
/// is outside of the range of the target type.
#[inline]
fn try_from(u: $source) -> Result<Self, Self::Error> {
if u > (Self::max_value() as $source) {
if u > (Self::MAX as $source) {
Err(TryFromIntError(()))
} else {
Ok(u as Self)
@@ -239,8 +239,8 @@ macro_rules! try_from_both_bounded {
/// is outside of the range of the target type.
#[inline]
fn try_from(u: $source) -> Result<Self, Self::Error> {
let min = Self::min_value() as $source;
let max = Self::max_value() as $source;
let min = Self::MIN as $source;
let max = Self::MAX as $source;
if u < min || u > max {
Err(TryFromIntError(()))
} else {
66 changes: 33 additions & 33 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
@@ -750,9 +750,9 @@ $EndFeature, "
}

doc_comment! {
concat!("Unchecked integer addition. Computes `self + rhs, assuming overflow
concat!("Unchecked integer addition. Computes `self + rhs`, assuming overflow
cannot occur. This results in undefined behavior when `self + rhs > ", stringify!($SelfT),
"::max_value()` or `self + rhs < ", stringify!($SelfT), "::min_value()`."),
"::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`."),
#[unstable(
feature = "unchecked_math",
reason = "niche optimization path",
@@ -792,9 +792,9 @@ $EndFeature, "
}

doc_comment! {
concat!("Unchecked integer subtraction. Computes `self - rhs, assuming overflow
concat!("Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
cannot occur. This results in undefined behavior when `self - rhs > ", stringify!($SelfT),
"::max_value()` or `self - rhs < ", stringify!($SelfT), "::min_value()`."),
"::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`."),
#[unstable(
feature = "unchecked_math",
reason = "niche optimization path",
@@ -834,9 +834,9 @@ $EndFeature, "
}

doc_comment! {
concat!("Unchecked integer multiplication. Computes `self * rhs, assuming overflow
concat!("Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
cannot occur. This results in undefined behavior when `self * rhs > ", stringify!($SelfT),
"::max_value()` or `self * rhs < ", stringify!($SelfT), "::min_value()`."),
"::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`."),
#[unstable(
feature = "unchecked_math",
reason = "niche optimization path",
@@ -871,7 +871,7 @@ $EndFeature, "
without modifying the original"]
#[inline]
pub const fn checked_div(self, rhs: Self) -> Option<Self> {
if rhs == 0 || (self == Self::min_value() && rhs == -1) {
if rhs == 0 || (self == Self::MIN && rhs == -1) {
None
} else {
// SAFETY: div by zero and by INT_MIN have been checked above
@@ -900,7 +900,7 @@ assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None);
without modifying the original"]
#[inline]
pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
if rhs == 0 || (self == Self::min_value() && rhs == -1) {
if rhs == 0 || (self == Self::MIN && rhs == -1) {
None
} else {
Some(self.div_euclid(rhs))
@@ -929,7 +929,7 @@ $EndFeature, "
without modifying the original"]
#[inline]
pub const fn checked_rem(self, rhs: Self) -> Option<Self> {
if rhs == 0 || (self == Self::min_value() && rhs == -1) {
if rhs == 0 || (self == Self::MIN && rhs == -1) {
None
} else {
// SAFETY: div by zero and by INT_MIN have been checked above
@@ -957,7 +957,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None);
without modifying the original"]
#[inline]
pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
if rhs == 0 || (self == Self::min_value() && rhs == -1) {
if rhs == 0 || (self == Self::MIN && rhs == -1) {
None
} else {
Some(self.rem_euclid(rhs))
@@ -1236,9 +1236,9 @@ $EndFeature, "
match self.checked_mul(rhs) {
Some(x) => x,
None => if (self < 0) == (rhs < 0) {
Self::max_value()
Self::MAX
} else {
Self::min_value()
Self::MIN
}
}
}
@@ -1267,8 +1267,8 @@ $EndFeature, "
pub const fn saturating_pow(self, exp: u32) -> Self {
match self.checked_pow(exp) {
Some(x) => x,
None if self < 0 && exp % 2 == 1 => Self::min_value(),
None => Self::max_value(),
None if self < 0 && exp % 2 == 1 => Self::MIN,
None => Self::MAX,
}
}
}
@@ -1738,7 +1738,7 @@ $EndFeature, "
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) {
if self == Self::min_value() && rhs == -1 {
if self == Self::MIN && rhs == -1 {
(self, true)
} else {
(self / rhs, false)
@@ -1771,7 +1771,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringi
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
if self == Self::min_value() && rhs == -1 {
if self == Self::MIN && rhs == -1 {
(self, true)
} else {
(self.div_euclid(rhs), false)
@@ -1805,7 +1805,7 @@ $EndFeature, "
#[must_use = "this returns the result of the operation, \
without modifying the original"]
pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
if self == Self::min_value() && rhs == -1 {
if self == Self::MIN && rhs == -1 {
(0, true)
} else {
(self % rhs, false)
@@ -1838,7 +1838,7 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));
without modifying the original"]
#[inline]
pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
if self == Self::min_value() && rhs == -1 {
if self == Self::MIN && rhs == -1 {
(0, true)
} else {
(self.rem_euclid(rhs), false)
@@ -1869,8 +1869,8 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($Self
#[allow(unused_attributes)]
#[allow_internal_unstable(const_if_match)]
pub const fn overflowing_neg(self) -> (Self, bool) {
if self == Self::min_value() {
(Self::min_value(), true)
if self == Self::MIN {
(Self::MIN, true)
} else {
(-self, false)
}
@@ -1952,7 +1952,7 @@ $EndFeature, "
#[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")]
#[inline]
pub const fn overflowing_abs(self) -> (Self, bool) {
(self.wrapping_abs(), self == Self::min_value())
(self.wrapping_abs(), self == Self::MIN)
}
}

@@ -2986,9 +2986,9 @@ assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);", $EndFeat
}

doc_comment! {
concat!("Unchecked integer addition. Computes `self + rhs, assuming overflow
concat!("Unchecked integer addition. Computes `self + rhs`, assuming overflow
cannot occur. This results in undefined behavior when `self + rhs > ", stringify!($SelfT),
"::max_value()` or `self + rhs < ", stringify!($SelfT), "::min_value()`."),
"::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`."),
#[unstable(
feature = "unchecked_math",
reason = "niche optimization path",
@@ -3026,9 +3026,9 @@ assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, "
}

doc_comment! {
concat!("Unchecked integer subtraction. Computes `self - rhs, assuming overflow
concat!("Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
cannot occur. This results in undefined behavior when `self - rhs > ", stringify!($SelfT),
"::max_value()` or `self - rhs < ", stringify!($SelfT), "::min_value()`."),
"::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`."),
#[unstable(
feature = "unchecked_math",
reason = "niche optimization path",
@@ -3066,9 +3066,9 @@ assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);", $EndFeature, "
}

doc_comment! {
concat!("Unchecked integer multiplication. Computes `self * rhs, assuming overflow
concat!("Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
cannot occur. This results in undefined behavior when `self * rhs > ", stringify!($SelfT),
"::max_value()` or `self * rhs < ", stringify!($SelfT), "::min_value()`."),
"::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`."),
#[unstable(
feature = "unchecked_math",
reason = "niche optimization path",
@@ -3366,7 +3366,7 @@ assert_eq!((", stringify!($SelfT), "::MAX).saturating_mul(10), ", stringify!($Se
pub const fn saturating_mul(self, rhs: Self) -> Self {
match self.checked_mul(rhs) {
Some(x) => x,
None => Self::max_value(),
None => Self::MAX,
}
}
}
@@ -3393,7 +3393,7 @@ $EndFeature, "
pub const fn saturating_pow(self, exp: u32) -> Self {
match self.checked_pow(exp) {
Some(x) => x,
None => Self::max_value(),
None => Self::MAX,
}
}
}
@@ -4080,7 +4080,7 @@ Basic usage:
}
}

doc_comment! {
doc_comment! {
concat!("Performs Euclidean division.

Since, for the positive integers, all common
@@ -4178,7 +4178,7 @@ assert!(!10", stringify!($SelfT), ".is_power_of_two());", $EndFeature, "
// (such as intel pre-haswell) have more efficient ctlz
// intrinsics when the argument is non-zero.
let z = unsafe { intrinsics::ctlz_nonzero(p) };
<$SelfT>::max_value() >> z
<$SelfT>::MAX >> z
}

doc_comment! {
@@ -5160,9 +5160,9 @@ trait FromStrRadixHelper: PartialOrd + Copy {
macro_rules! doit {
($($t:ty)*) => ($(impl FromStrRadixHelper for $t {
#[inline]
fn min_value() -> Self { Self::min_value() }
fn min_value() -> Self { Self::MIN }
#[inline]
fn max_value() -> Self { Self::max_value() }
fn max_value() -> Self { Self::MAX }
#[inline]
fn from_u32(u: u32) -> Self { u as Self }
#[inline]
Loading