11//! See docs in build/expr/mod.rs
22
33use crate :: build:: Builder ;
4- use crate :: thir:: constant:: parse_float;
5- use rustc_ast:: ast;
64use rustc_hir:: def_id:: DefId ;
7- use rustc_middle:: mir:: interpret:: {
8- Allocation , ConstValue , LitToConstError , LitToConstInput , Scalar ,
9- } ;
5+ use rustc_middle:: mir:: interpret:: { ConstValue , LitToConstError , LitToConstInput , Scalar } ;
106use rustc_middle:: mir:: * ;
117use rustc_middle:: thir:: * ;
128use rustc_middle:: ty:: subst:: SubstsRef ;
139use rustc_middle:: ty:: { self , CanonicalUserTypeAnnotation , Ty , TyCtxt } ;
14- use rustc_target:: abi:: Size ;
1510
1611impl < ' a , ' tcx > Builder < ' a , ' tcx > {
1712 /// Compile `expr`, yielding a compile-time constant. Assumes that
@@ -32,11 +27,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3227 }
3328 ExprKind :: Literal { lit, neg } => {
3429 let literal =
35- match lit_to_constant ( tcx, LitToConstInput { lit : & lit. node , ty, neg } ) {
30+ match tcx. lit_to_mir_constant ( LitToConstInput { lit : & lit. node , ty, neg } ) {
3631 Ok ( c) => c,
3732 Err ( LitToConstError :: Reported ) => ConstantKind :: Ty ( tcx. const_error ( ty) ) ,
3833 Err ( LitToConstError :: TypeError ) => {
39- bug ! ( "encountered type error in `lit_to_constant " )
34+ bug ! ( "encountered type error in `lit_to_mir_constant " )
4035 }
4136 } ;
4237
@@ -85,60 +80,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8580
8681 Constant { span, user_ty : None , literal }
8782 }
88- ExprKind :: ConstBlock { value } => {
89- Constant { span : span, user_ty : None , literal : value }
90- }
9183 _ => span_bug ! ( span, "expression is not a valid constant {:?}" , kind) ,
9284 }
9385 }
9486}
95-
96- crate fn lit_to_constant < ' tcx > (
97- tcx : TyCtxt < ' tcx > ,
98- lit_input : LitToConstInput < ' tcx > ,
99- ) -> Result < ConstantKind < ' tcx > , LitToConstError > {
100- let LitToConstInput { lit, ty, neg } = lit_input;
101- let trunc = |n| {
102- let param_ty = ty:: ParamEnv :: reveal_all ( ) . and ( ty) ;
103- let width = tcx. layout_of ( param_ty) . map_err ( |_| LitToConstError :: Reported ) ?. size ;
104- trace ! ( "trunc {} with size {} and shift {}" , n, width. bits( ) , 128 - width. bits( ) ) ;
105- let result = width. truncate ( n) ;
106- trace ! ( "trunc result: {}" , result) ;
107- Ok ( ConstValue :: Scalar ( Scalar :: from_uint ( result, width) ) )
108- } ;
109-
110- let value = match ( lit, & ty. kind ( ) ) {
111- ( ast:: LitKind :: Str ( s, _) , ty:: Ref ( _, inner_ty, _) ) if inner_ty. is_str ( ) => {
112- let s = s. as_str ( ) ;
113- let allocation = Allocation :: from_bytes_byte_aligned_immutable ( s. as_bytes ( ) ) ;
114- let allocation = tcx. intern_const_alloc ( allocation) ;
115- ConstValue :: Slice { data : allocation, start : 0 , end : s. len ( ) }
116- }
117- ( ast:: LitKind :: ByteStr ( data) , ty:: Ref ( _, inner_ty, _) )
118- if matches ! ( inner_ty. kind( ) , ty:: Slice ( _) ) =>
119- {
120- let allocation = Allocation :: from_bytes_byte_aligned_immutable ( data as & [ u8 ] ) ;
121- let allocation = tcx. intern_const_alloc ( allocation) ;
122- ConstValue :: Slice { data : allocation, start : 0 , end : data. len ( ) }
123- }
124- ( ast:: LitKind :: ByteStr ( data) , ty:: Ref ( _, inner_ty, _) ) if inner_ty. is_array ( ) => {
125- let id = tcx. allocate_bytes ( data) ;
126- ConstValue :: Scalar ( Scalar :: from_pointer ( id. into ( ) , & tcx) )
127- }
128- ( ast:: LitKind :: Byte ( n) , ty:: Uint ( ty:: UintTy :: U8 ) ) => {
129- ConstValue :: Scalar ( Scalar :: from_uint ( * n, Size :: from_bytes ( 1 ) ) )
130- }
131- ( ast:: LitKind :: Int ( n, _) , ty:: Uint ( _) ) | ( ast:: LitKind :: Int ( n, _) , ty:: Int ( _) ) => {
132- trunc ( if neg { ( * n as i128 ) . overflowing_neg ( ) . 0 as u128 } else { * n } ) ?
133- }
134- ( ast:: LitKind :: Float ( n, _) , ty:: Float ( fty) ) => {
135- parse_float ( * n, * fty, neg) . ok_or ( LitToConstError :: Reported ) ?
136- }
137- ( ast:: LitKind :: Bool ( b) , ty:: Bool ) => ConstValue :: Scalar ( Scalar :: from_bool ( * b) ) ,
138- ( ast:: LitKind :: Char ( c) , ty:: Char ) => ConstValue :: Scalar ( Scalar :: from_char ( * c) ) ,
139- ( ast:: LitKind :: Err ( _) , _) => return Err ( LitToConstError :: Reported ) ,
140- _ => return Err ( LitToConstError :: TypeError ) ,
141- } ;
142-
143- Ok ( ConstantKind :: Val ( value, ty) )
144- }
0 commit comments