Skip to content

Commit 2ef7c86

Browse files
authored
Rollup merge of rust-lang#69698 - RalfJung:int_assoc, r=davidtwco
Use associated constants of integer types Take advantage of rust-lang#68952 in the interpreter and some nearby modules :)
2 parents c9f7758 + f0c3cf2 commit 2ef7c86

File tree

10 files changed

+21
-23
lines changed

10 files changed

+21
-23
lines changed

src/librustc/dep_graph/graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ impl DepGraph {
524524
edge_list_indices.push((start, end));
525525
}
526526

527-
debug_assert!(edge_list_data.len() <= ::std::u32::MAX as usize);
527+
debug_assert!(edge_list_data.len() <= u32::MAX as usize);
528528
debug_assert_eq!(edge_list_data.len(), total_edge_count);
529529

530530
SerializedDepGraph { nodes, fingerprints, edge_list_indices, edge_list_data }

src/librustc/mir/interpret/allocation.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,9 @@ impl UndefMask {
818818
// First set all bits except the first `bita`,
819819
// then unset the last `64 - bitb` bits.
820820
let range = if bitb == 0 {
821-
u64::max_value() << bita
821+
u64::MAX << bita
822822
} else {
823-
(u64::max_value() << bita) & (u64::max_value() >> (64 - bitb))
823+
(u64::MAX << bita) & (u64::MAX >> (64 - bitb))
824824
};
825825
if new_state {
826826
self.blocks[blocka] |= range;
@@ -832,21 +832,21 @@ impl UndefMask {
832832
// across block boundaries
833833
if new_state {
834834
// Set `bita..64` to `1`.
835-
self.blocks[blocka] |= u64::max_value() << bita;
835+
self.blocks[blocka] |= u64::MAX << bita;
836836
// Set `0..bitb` to `1`.
837837
if bitb != 0 {
838-
self.blocks[blockb] |= u64::max_value() >> (64 - bitb);
838+
self.blocks[blockb] |= u64::MAX >> (64 - bitb);
839839
}
840840
// Fill in all the other blocks (much faster than one bit at a time).
841841
for block in (blocka + 1)..blockb {
842-
self.blocks[block] = u64::max_value();
842+
self.blocks[block] = u64::MAX;
843843
}
844844
} else {
845845
// Set `bita..64` to `0`.
846-
self.blocks[blocka] &= !(u64::max_value() << bita);
846+
self.blocks[blocka] &= !(u64::MAX << bita);
847847
// Set `0..bitb` to `0`.
848848
if bitb != 0 {
849-
self.blocks[blockb] &= !(u64::max_value() >> (64 - bitb));
849+
self.blocks[blockb] &= !(u64::MAX >> (64 - bitb));
850850
}
851851
// Fill in all the other blocks (much faster than one bit at a time).
852852
for block in (blocka + 1)..blockb {

src/librustc/mir/interpret/pointer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ pub trait PointerArithmetic: layout::HasDataLayout {
7878
fn overflowing_signed_offset(&self, val: u64, i: i128) -> (u64, bool) {
7979
// FIXME: is it possible to over/underflow here?
8080
if i < 0 {
81-
// Trickery to ensure that `i64::min_value()` works fine: compute `n = -i`.
81+
// Trickery to ensure that `i64::MIN` works fine: compute `n = -i`.
8282
// This formula only works for true negative values; it overflows for zero!
83-
let n = u64::max_value() - (i as u64) + 1;
83+
let n = u64::MAX - (i as u64) + 1;
8484
let res = val.overflowing_sub(n);
8585
self.truncate_to_ptr(res)
8686
} else {

src/librustc/ty/layout.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_span::DUMMY_SP;
77

88
use std::cmp;
99
use std::fmt;
10-
use std::i128;
1110
use std::iter;
1211
use std::mem;
1312
use std::ops::Bound;
@@ -1001,7 +1000,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
10011000
}
10021001
}
10031002

1004-
let (mut min, mut max) = (i128::max_value(), i128::min_value());
1003+
let (mut min, mut max) = (i128::MAX, i128::MIN);
10051004
let discr_type = def.repr.discr_type();
10061005
let bits = Integer::from_attr(self, discr_type).size().bits();
10071006
for (i, discr) in def.discriminants(tcx) {
@@ -1021,7 +1020,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
10211020
}
10221021
}
10231022
// We might have no inhabited variants, so pretend there's at least one.
1024-
if (min, max) == (i128::max_value(), i128::min_value()) {
1023+
if (min, max) == (i128::MAX, i128::MIN) {
10251024
min = 0;
10261025
max = 0;
10271026
}

src/librustc/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ pub trait PrettyPrinter<'tcx>:
920920
}
921921
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Uint(ui)) => {
922922
let bit_size = Integer::from_attr(&self.tcx(), UnsignedInt(*ui)).size();
923-
let max = truncate(u128::max_value(), bit_size);
923+
let max = truncate(u128::MAX, bit_size);
924924

925925
let ui_str = ui.name_str();
926926
if data == max {

src/librustc/ty/query/on_disk_cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ struct AbsoluteBytePos(u32);
9292

9393
impl AbsoluteBytePos {
9494
fn new(pos: usize) -> AbsoluteBytePos {
95-
debug_assert!(pos <= ::std::u32::MAX as usize);
95+
debug_assert!(pos <= u32::MAX as usize);
9696
AbsoluteBytePos(pos as u32)
9797
}
9898

src/librustc/ty/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ fn signed_min(size: Size) -> i128 {
5050
}
5151

5252
fn signed_max(size: Size) -> i128 {
53-
i128::max_value() >> (128 - size.bits())
53+
i128::MAX >> (128 - size.bits())
5454
}
5555

5656
fn unsigned_max(size: Size) -> u128 {
57-
u128::max_value() >> (128 - size.bits())
57+
u128::MAX >> (128 - size.bits())
5858
}
5959

6060
fn int_size_and_signed<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> (Size, bool) {
@@ -77,7 +77,7 @@ impl<'tcx> Discr<'tcx> {
7777
let min = signed_min(size);
7878
let max = signed_max(size);
7979
let val = sign_extend(self.val, size) as i128;
80-
assert!(n < (i128::max_value() as u128));
80+
assert!(n < (i128::MAX as u128));
8181
let n = n as i128;
8282
let oflo = val > max - n;
8383
let val = if oflo { min + (n - (max - val) - 1) } else { val + n };

src/librustc_mir/dataflow/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use std::borrow::Borrow;
1616
use std::fmt;
1717
use std::io;
1818
use std::path::PathBuf;
19-
use std::usize;
2019

2120
pub use self::at_location::{FlowAtLocation, FlowsAtLocation};
2221
pub(crate) use self::drop_flag_effects::*;

src/librustc_mir/interpret/intrinsics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
203203
if is_add {
204204
// max unsigned
205205
Scalar::from_uint(
206-
u128::max_value() >> (128 - num_bits),
206+
u128::MAX >> (128 - num_bits),
207207
Size::from_bits(num_bits),
208208
)
209209
} else {
@@ -381,11 +381,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
381381
dest: PlaceTy<'tcx, M::PointerTag>,
382382
) -> InterpResult<'tcx> {
383383
// Performs an exact division, resulting in undefined behavior where
384-
// `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`.
384+
// `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`.
385385
// First, check x % y != 0 (or if that computation overflows).
386386
let (res, overflow, _ty) = self.overflowing_binary_op(BinOp::Rem, a, b)?;
387387
if overflow || res.assert_bits(a.layout.size) != 0 {
388-
// Then, check if `b` is -1, which is the "min_value / -1" case.
388+
// Then, check if `b` is -1, which is the "MIN / -1" case.
389389
let minus1 = Scalar::from_int(-1, dest.layout.size);
390390
let b_scalar = b.to_scalar().unwrap();
391391
if b_scalar == minus1 {

src/librustc_mir/interpret/validity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M
463463
let (lo, hi) = valid_range.clone().into_inner();
464464
// Determine the allowed range
465465
// `max_hi` is as big as the size fits
466-
let max_hi = u128::max_value() >> (128 - op.layout.size.bits());
466+
let max_hi = u128::MAX >> (128 - op.layout.size.bits());
467467
assert!(hi <= max_hi);
468468
// We could also write `(hi + 1) % (max_hi + 1) == lo` but `max_hi + 1` overflows for `u128`
469469
if (lo == 0 && hi == max_hi) || (hi + 1 == lo) {

0 commit comments

Comments
 (0)