11use rustc:: mir:: interpret:: {
22 truncate, Allocation , ConstValue , LitToConstError , LitToConstInput , Scalar ,
33} ;
4- use rustc:: ty:: { self , layout:: Size , ParamEnv , TyCtxt } ;
4+ use rustc:: ty:: { self , layout:: Size , ParamEnv , TyCtxt , TyS } ;
55use rustc_span:: symbol:: Symbol ;
66use syntax:: ast;
77
@@ -20,50 +20,35 @@ crate fn lit_to_const<'tcx>(
2020 Ok ( ConstValue :: Scalar ( Scalar :: from_uint ( result, width) ) )
2121 } ;
2222
23- let lit = match * lit {
24- ast:: LitKind :: Str ( ref s, _) => {
23+ let lit = match ( lit, & ty . kind ) {
24+ ( ast:: LitKind :: Str ( s, _) , ty :: Ref ( _ , TyS { kind : ty :: Str , .. } , _ ) ) => {
2525 let s = s. as_str ( ) ;
2626 let allocation = Allocation :: from_byte_aligned_bytes ( s. as_bytes ( ) ) ;
2727 let allocation = tcx. intern_const_alloc ( allocation) ;
2828 ConstValue :: Slice { data : allocation, start : 0 , end : s. len ( ) }
2929 }
30- ast:: LitKind :: ByteStr ( ref data) => {
31- if let ty:: Ref ( _, ref_ty, _) = ty. kind {
32- match ref_ty. kind {
33- ty:: Slice ( _) => {
34- let allocation = Allocation :: from_byte_aligned_bytes ( data as & Vec < u8 > ) ;
35- let allocation = tcx. intern_const_alloc ( allocation) ;
36- ConstValue :: Slice { data : allocation, start : 0 , end : data. len ( ) }
37- }
38- ty:: Array ( _, _) => {
39- let id = tcx. allocate_bytes ( data) ;
40- ConstValue :: Scalar ( Scalar :: Ptr ( id. into ( ) ) )
41- }
42- _ => {
43- bug ! ( "bytestring should have type of either &[u8] or &[u8; _], not {}" , ty)
44- }
45- }
46- } else {
47- bug ! ( "bytestring should have type of either &[u8] or &[u8; _], not {}" , ty)
48- }
30+ ( ast:: LitKind :: ByteStr ( data) , ty:: Ref ( _, TyS { kind : ty:: Slice ( _) , .. } , _) ) => {
31+ let allocation = Allocation :: from_byte_aligned_bytes ( data as & Vec < u8 > ) ;
32+ let allocation = tcx. intern_const_alloc ( allocation) ;
33+ ConstValue :: Slice { data : allocation, start : 0 , end : data. len ( ) }
34+ }
35+ ( ast:: LitKind :: ByteStr ( data) , ty:: Ref ( _, TyS { kind : ty:: Array ( _, _) , .. } , _) ) => {
36+ let id = tcx. allocate_bytes ( data) ;
37+ ConstValue :: Scalar ( Scalar :: Ptr ( id. into ( ) ) )
38+ }
39+ ( ast:: LitKind :: Byte ( n) , ty:: Uint ( ast:: UintTy :: U8 ) ) => {
40+ ConstValue :: Scalar ( Scalar :: from_uint ( * n, Size :: from_bytes ( 1 ) ) )
4941 }
50- ast:: LitKind :: Byte ( n) => ConstValue :: Scalar ( Scalar :: from_uint ( n, Size :: from_bytes ( 1 ) ) ) ,
51- ast:: LitKind :: Int ( n, _) if neg => {
52- let n = n as i128 ;
53- let n = n. overflowing_neg ( ) . 0 ;
54- trunc ( n as u128 ) ?
42+ ( ast:: LitKind :: Int ( n, _) , ty:: Uint ( _) ) | ( ast:: LitKind :: Int ( n, _) , ty:: Int ( _) ) => {
43+ trunc ( if neg { ( * n as i128 ) . overflowing_neg ( ) . 0 as u128 } else { * n } ) ?
5544 }
56- ast:: LitKind :: Int ( n, _) => trunc ( n) ?,
57- ast:: LitKind :: Float ( n, _) => {
58- let fty = match ty. kind {
59- ty:: Float ( fty) => fty,
60- _ => bug ! ( ) ,
61- } ;
62- parse_float ( n, fty, neg) . map_err ( |_| LitToConstError :: UnparseableFloat ) ?
45+ ( ast:: LitKind :: Float ( n, _) , ty:: Float ( fty) ) => {
46+ parse_float ( * n, * fty, neg) . map_err ( |_| LitToConstError :: UnparseableFloat ) ?
6347 }
64- ast:: LitKind :: Bool ( b) => ConstValue :: Scalar ( Scalar :: from_bool ( b) ) ,
65- ast:: LitKind :: Char ( c) => ConstValue :: Scalar ( Scalar :: from_char ( c) ) ,
66- ast:: LitKind :: Err ( _) => return Err ( LitToConstError :: Reported ) ,
48+ ( ast:: LitKind :: Bool ( b) , ty:: Bool ) => ConstValue :: Scalar ( Scalar :: from_bool ( * b) ) ,
49+ ( ast:: LitKind :: Char ( c) , ty:: Char ) => ConstValue :: Scalar ( Scalar :: from_char ( * c) ) ,
50+ ( ast:: LitKind :: Err ( _) , _) => return Err ( LitToConstError :: Reported ) ,
51+ _ => return Err ( LitToConstError :: TypeError ) ,
6752 } ;
6853 Ok ( ty:: Const :: from_value ( tcx, lit, ty) )
6954}
0 commit comments