@@ -4,6 +4,7 @@ use crate::hir::def::Def;
44use crate :: hir:: def_id:: DefId ;
55use crate :: hir:: map:: DefPathData ;
66use crate :: hir:: { self , Node } ;
7+ use crate :: mir:: interpret:: { sign_extend, truncate} ;
78use crate :: ich:: NodeIdHashingMode ;
89use crate :: traits:: { self , ObligationCause } ;
910use crate :: ty:: { self , Ty , TyCtxt , GenericParamDefKind , TypeFoldable } ;
@@ -32,12 +33,12 @@ impl<'tcx> fmt::Display for Discr<'tcx> {
3233 fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
3334 match self . ty . sty {
3435 ty:: Int ( ity) => {
35- let bits = ty:: tls:: with ( |tcx| {
36- Integer :: from_attr ( & tcx, SignedInt ( ity) ) . size ( ) . bits ( )
36+ let size = ty:: tls:: with ( |tcx| {
37+ Integer :: from_attr ( & tcx, SignedInt ( ity) ) . size ( )
3738 } ) ;
38- let x = self . val as i128 ;
39+ let x = self . val ;
3940 // sign extend the raw representation to be an i128
40- let x = ( x << ( 128 - bits ) ) >> ( 128 - bits ) ;
41+ let x = sign_extend ( x , size ) as i128 ;
4142 write ! ( fmt, "{}" , x)
4243 } ,
4344 _ => write ! ( fmt, "{}" , self . val) ,
@@ -57,12 +58,12 @@ impl<'tcx> Discr<'tcx> {
5758 _ => bug ! ( "non integer discriminant" ) ,
5859 } ;
5960
61+ let size = int. size ( ) ;
6062 let bit_size = int. size ( ) . bits ( ) ;
6163 let shift = 128 - bit_size;
6264 if signed {
6365 let sext = |u| {
64- let i = u as i128 ;
65- ( i << shift) >> shift
66+ sign_extend ( u, size) as i128
6667 } ;
6768 let min = sext ( 1_u128 << ( bit_size - 1 ) ) ;
6869 let max = i128:: max_value ( ) >> shift;
@@ -77,7 +78,7 @@ impl<'tcx> Discr<'tcx> {
7778 } ;
7879 // zero the upper bits
7980 let val = val as u128 ;
80- let val = ( val << shift ) >> shift ;
81+ let val = truncate ( val, size ) ;
8182 ( Self {
8283 val : val as u128 ,
8384 ty : self . ty ,
0 commit comments