Skip to content

Commit e3bbc0d

Browse files
authored
Rollup merge of #66410 - RalfJung:miri-machine-max, r=oli-obk
miri: helper methods for max values of machine's usize/isize We recently wanted this in Miri. r? @oli-obk
2 parents 853af77 + 6c9ba97 commit e3bbc0d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/librustc/mir/interpret/pointer.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt::{self, Display};
2+
use std::convert::TryFrom;
23

34
use crate::mir;
45
use crate::ty::layout::{self, HasDataLayout, Size};
@@ -40,6 +41,18 @@ pub trait PointerArithmetic: layout::HasDataLayout {
4041
self.data_layout().pointer_size
4142
}
4243

44+
#[inline]
45+
fn usize_max(&self) -> u64 {
46+
let max_usize_plus_1 = 1u128 << self.pointer_size().bits();
47+
u64::try_from(max_usize_plus_1-1).unwrap()
48+
}
49+
50+
#[inline]
51+
fn isize_max(&self) -> i64 {
52+
let max_isize_plus_1 = 1u128 << (self.pointer_size().bits()-1);
53+
i64::try_from(max_isize_plus_1-1).unwrap()
54+
}
55+
4356
/// Helper function: truncate given value-"overflowed flag" pair to pointer size and
4457
/// update "overflowed flag" if there was an overflow.
4558
/// This should be called by all the other methods before returning!

0 commit comments

Comments
 (0)