1- use rustc_ast as ast;
1+ use rustc_abi:: Size ;
2+ use rustc_ast:: { self as ast} ;
23use rustc_hir:: LangItem ;
34use rustc_middle:: bug;
45use rustc_middle:: mir:: interpret:: LitToConstInput ;
@@ -17,13 +18,12 @@ pub(crate) fn lit_to_const<'tcx>(
1718 return ty:: Const :: new_error ( tcx, guar) ;
1819 }
1920
20- let trunc = |n| {
21- let width = match tcx. layout_of ( ty:: TypingEnv :: fully_monomorphized ( ) . as_query_input ( ty) ) {
22- Ok ( layout) => layout. size ,
23- Err ( _) => {
24- tcx. dcx ( ) . bug ( format ! ( "couldn't compute width of literal: {:?}" , lit_input. lit) )
25- }
26- } ;
21+ let trunc = |n, width : ty:: UintTy | {
22+ let width = width
23+ . normalize ( tcx. data_layout . pointer_size . bits ( ) . try_into ( ) . unwrap ( ) )
24+ . bit_width ( )
25+ . unwrap ( ) ;
26+ let width = Size :: from_bits ( width) ;
2727 trace ! ( "trunc {} with size {} and shift {}" , n, width. bits( ) , 128 - width. bits( ) ) ;
2828 let result = width. truncate ( n) ;
2929 trace ! ( "trunc result: {}" , result) ;
@@ -55,9 +55,15 @@ pub(crate) fn lit_to_const<'tcx>(
5555 let bytes = data as & [ u8 ] ;
5656 ty:: ValTree :: from_raw_bytes ( tcx, bytes)
5757 }
58- ( ast:: LitKind :: Int ( n, _) , ty:: Uint ( _) ) | ( ast:: LitKind :: Int ( n, _) , ty:: Int ( _) ) => {
59- let scalar_int =
60- trunc ( if neg { ( n. get ( ) as i128 ) . overflowing_neg ( ) . 0 as u128 } else { n. get ( ) } ) ;
58+ ( ast:: LitKind :: Int ( n, _) , ty:: Uint ( ui) ) if !neg => {
59+ let scalar_int = trunc ( n. get ( ) , * ui) ;
60+ ty:: ValTree :: from_scalar_int ( scalar_int)
61+ }
62+ ( ast:: LitKind :: Int ( n, _) , ty:: Int ( i) ) => {
63+ let scalar_int = trunc (
64+ if neg { ( n. get ( ) as i128 ) . overflowing_neg ( ) . 0 as u128 } else { n. get ( ) } ,
65+ i. to_unsigned ( ) ,
66+ ) ;
6167 ty:: ValTree :: from_scalar_int ( scalar_int)
6268 }
6369 ( ast:: LitKind :: Bool ( b) , ty:: Bool ) => ty:: ValTree :: from_scalar_int ( ( * b) . into ( ) ) ,
0 commit comments