@@ -3,7 +3,7 @@ use super::{AllocId, InterpResult};
3
3
use rustc_macros:: HashStable ;
4
4
use rustc_target:: abi:: { HasDataLayout , Size } ;
5
5
6
- use std:: convert:: TryFrom ;
6
+ use std:: convert:: { TryFrom , TryInto } ;
7
7
use std:: fmt;
8
8
9
9
////////////////////////////////////////////////////////////////////////////////
@@ -20,29 +20,27 @@ pub trait PointerArithmetic: HasDataLayout {
20
20
21
21
#[ inline]
22
22
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 ( )
25
24
}
26
25
27
26
#[ inline]
28
27
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 ( )
31
29
}
32
30
33
31
#[ inline]
34
32
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 ( )
37
34
}
38
35
39
36
#[ inline]
40
37
fn machine_usize_to_isize ( & self , val : u64 ) -> i64 {
41
38
let val = val as i64 ;
42
- // Now clamp into the machine_isize range.
39
+ // Now wrap-around into the machine_isize range.
43
40
if val > self . machine_isize_max ( ) {
44
41
// This can only happen the the ptr size is < 64, so we know max_usize_plus_1 fits into
45
42
// i64.
43
+ debug_assert ! ( self . pointer_size( ) . bits( ) < 64 ) ;
46
44
let max_usize_plus_1 = 1u128 << self . pointer_size ( ) . bits ( ) ;
47
45
val - i64:: try_from ( max_usize_plus_1) . unwrap ( )
48
46
} else {
0 commit comments