Rollup merge of #88859 - RalfJung:size-helpers, r=oli-obk

interpreter PointerArithmetic: use new Size helper methods
This commit is contained in:
Guillaume Gomez 2021-09-13 21:20:40 +02:00 committed by GitHub
commit c0e7f7edea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,7 +3,7 @@ use super::{AllocId, InterpResult};
use rustc_macros::HashStable;
use rustc_target::abi::{HasDataLayout, Size};
use std::convert::TryFrom;
use std::convert::{TryFrom, TryInto};
use std::fmt;
////////////////////////////////////////////////////////////////////////////////
@ -20,29 +20,27 @@ pub trait PointerArithmetic: HasDataLayout {
#[inline]
fn machine_usize_max(&self) -> u64 {
let max_usize_plus_1 = 1u128 << self.pointer_size().bits();
u64::try_from(max_usize_plus_1 - 1).unwrap()
self.pointer_size().unsigned_int_max().try_into().unwrap()
}
#[inline]
fn machine_isize_min(&self) -> i64 {
let max_isize_plus_1 = 1i128 << (self.pointer_size().bits() - 1);
i64::try_from(-max_isize_plus_1).unwrap()
self.pointer_size().signed_int_min().try_into().unwrap()
}
#[inline]
fn machine_isize_max(&self) -> i64 {
let max_isize_plus_1 = 1u128 << (self.pointer_size().bits() - 1);
i64::try_from(max_isize_plus_1 - 1).unwrap()
self.pointer_size().signed_int_max().try_into().unwrap()
}
#[inline]
fn machine_usize_to_isize(&self, val: u64) -> i64 {
let val = val as i64;
// Now clamp into the machine_isize range.
// Now wrap-around into the machine_isize range.
if val > self.machine_isize_max() {
// This can only happen the the ptr size is < 64, so we know max_usize_plus_1 fits into
// i64.
debug_assert!(self.pointer_size().bits() < 64);
let max_usize_plus_1 = 1u128 << self.pointer_size().bits();
val - i64::try_from(max_usize_plus_1).unwrap()
} else {