@@ -3,7 +3,7 @@ use super::{AllocId, InterpResult};
33use rustc_macros:: HashStable ;
44use rustc_target:: abi:: { HasDataLayout , Size } ;
55
6- use std:: convert:: TryFrom ;
6+ use std:: convert:: { TryFrom , TryInto } ;
77use std:: fmt;
88
99////////////////////////////////////////////////////////////////////////////////
@@ -20,29 +20,27 @@ pub trait PointerArithmetic: HasDataLayout {
2020
2121 #[ inline]
2222 fn machine_usize_max ( & self ) -> u64 {
23- let max_usize_plus_1 = 1u128 << self . pointer_size ( ) . bits ( ) ;
24- u64:: try_from ( max_usize_plus_1 - 1 ) . unwrap ( )
23+ self . pointer_size ( ) . unsigned_int_max ( ) . try_into ( ) . unwrap ( )
2524 }
2625
2726 #[ inline]
2827 fn machine_isize_min ( & self ) -> i64 {
29- let max_isize_plus_1 = 1i128 << ( self . pointer_size ( ) . bits ( ) - 1 ) ;
30- i64:: try_from ( -max_isize_plus_1) . unwrap ( )
28+ self . pointer_size ( ) . signed_int_min ( ) . try_into ( ) . unwrap ( )
3129 }
3230
3331 #[ inline]
3432 fn machine_isize_max ( & self ) -> i64 {
35- let max_isize_plus_1 = 1u128 << ( self . pointer_size ( ) . bits ( ) - 1 ) ;
36- i64:: try_from ( max_isize_plus_1 - 1 ) . unwrap ( )
33+ self . pointer_size ( ) . signed_int_max ( ) . try_into ( ) . unwrap ( )
3734 }
3835
3936 #[ inline]
4037 fn machine_usize_to_isize ( & self , val : u64 ) -> i64 {
4138 let val = val as i64 ;
42- // Now clamp into the machine_isize range.
39+ // Now wrap-around into the machine_isize range.
4340 if val > self . machine_isize_max ( ) {
4441 // This can only happen the the ptr size is < 64, so we know max_usize_plus_1 fits into
4542 // i64.
43+ debug_assert ! ( self . pointer_size( ) . bits( ) < 64 ) ;
4644 let max_usize_plus_1 = 1u128 << self . pointer_size ( ) . bits ( ) ;
4745 val - i64:: try_from ( max_usize_plus_1) . unwrap ( )
4846 } else {
0 commit comments