@@ -19,11 +19,12 @@ pub(crate) use self::check_match::check_match;
1919use const_eval:: { const_field, const_variant_index} ;
2020
2121use hair:: util:: UserAnnotatedTyHelpers ;
22+ use hair:: constant:: * ;
2223
2324use rustc:: mir:: { fmt_const_val, Field , BorrowKind , Mutability } ;
2425use rustc:: mir:: { ProjectionElem , UserTypeAnnotation , UserTypeProjection , UserTypeProjections } ;
2526use rustc:: mir:: interpret:: { Scalar , GlobalId , ConstValue , sign_extend} ;
26- use rustc:: ty:: { self , Region , TyCtxt , AdtDef , Ty , ParamEnv } ;
27+ use rustc:: ty:: { self , Region , TyCtxt , AdtDef , Ty } ;
2728use rustc:: ty:: subst:: { Substs , Kind } ;
2829use rustc:: ty:: layout:: VariantIdx ;
2930use rustc:: hir:: { self , PatKind , RangeEnd } ;
@@ -37,7 +38,6 @@ use std::fmt;
3738use syntax:: ast;
3839use syntax:: ptr:: P ;
3940use syntax_pos:: Span ;
40- use syntax_pos:: symbol:: Symbol ;
4141
4242#[ derive( Clone , Debug ) ]
4343pub enum PatternError {
@@ -1292,101 +1292,3 @@ pub fn compare_const_vals<'a, 'tcx>(
12921292
12931293 fallback ( )
12941294}
1295-
1296- #[ derive( PartialEq ) ]
1297- pub enum LitToConstError {
1298- UnparseableFloat ,
1299- Reported ,
1300- }
1301-
1302- pub fn lit_to_const < ' a , ' gcx , ' tcx > (
1303- lit : & ' tcx ast:: LitKind ,
1304- tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
1305- ty : Ty < ' tcx > ,
1306- neg : bool ,
1307- ) -> Result < & ' tcx ty:: Const < ' tcx > , LitToConstError > {
1308- use syntax:: ast:: * ;
1309-
1310- let trunc = |n| {
1311- let param_ty = ParamEnv :: reveal_all ( ) . and ( tcx. lift_to_global ( & ty) . unwrap ( ) ) ;
1312- let width = tcx. layout_of ( param_ty) . map_err ( |_| LitToConstError :: Reported ) ?. size ;
1313- trace ! ( "trunc {} with size {} and shift {}" , n, width. bits( ) , 128 - width. bits( ) ) ;
1314- let shift = 128 - width. bits ( ) ;
1315- let result = ( n << shift) >> shift;
1316- trace ! ( "trunc result: {}" , result) ;
1317- Ok ( ConstValue :: Scalar ( Scalar :: Bits {
1318- bits : result,
1319- size : width. bytes ( ) as u8 ,
1320- } ) )
1321- } ;
1322-
1323- use rustc:: mir:: interpret:: * ;
1324- let lit = match * lit {
1325- LitKind :: Str ( ref s, _) => {
1326- let s = s. as_str ( ) ;
1327- let id = tcx. allocate_bytes ( s. as_bytes ( ) ) ;
1328- ConstValue :: new_slice ( Scalar :: Ptr ( id. into ( ) ) , s. len ( ) as u64 , & tcx)
1329- } ,
1330- LitKind :: ByteStr ( ref data) => {
1331- let id = tcx. allocate_bytes ( data) ;
1332- ConstValue :: Scalar ( Scalar :: Ptr ( id. into ( ) ) )
1333- } ,
1334- LitKind :: Byte ( n) => ConstValue :: Scalar ( Scalar :: Bits {
1335- bits : n as u128 ,
1336- size : 1 ,
1337- } ) ,
1338- LitKind :: Int ( n, _) if neg => {
1339- let n = n as i128 ;
1340- let n = n. overflowing_neg ( ) . 0 ;
1341- trunc ( n as u128 ) ?
1342- } ,
1343- LitKind :: Int ( n, _) => trunc ( n) ?,
1344- LitKind :: Float ( n, fty) => {
1345- parse_float ( n, fty, neg) . map_err ( |_| LitToConstError :: UnparseableFloat ) ?
1346- }
1347- LitKind :: FloatUnsuffixed ( n) => {
1348- let fty = match ty. sty {
1349- ty:: Float ( fty) => fty,
1350- _ => bug ! ( )
1351- } ;
1352- parse_float ( n, fty, neg) . map_err ( |_| LitToConstError :: UnparseableFloat ) ?
1353- }
1354- LitKind :: Bool ( b) => ConstValue :: Scalar ( Scalar :: from_bool ( b) ) ,
1355- LitKind :: Char ( c) => ConstValue :: Scalar ( Scalar :: from_char ( c) ) ,
1356- } ;
1357- Ok ( ty:: Const :: from_const_value ( tcx, lit, ty) )
1358- }
1359-
1360- pub fn parse_float < ' tcx > (
1361- num : Symbol ,
1362- fty : ast:: FloatTy ,
1363- neg : bool ,
1364- ) -> Result < ConstValue < ' tcx > , ( ) > {
1365- let num = num. as_str ( ) ;
1366- use rustc_apfloat:: ieee:: { Single , Double } ;
1367- use rustc_apfloat:: Float ;
1368- let ( bits, size) = match fty {
1369- ast:: FloatTy :: F32 => {
1370- num. parse :: < f32 > ( ) . map_err ( |_| ( ) ) ?;
1371- let mut f = num. parse :: < Single > ( ) . unwrap_or_else ( |e| {
1372- panic ! ( "apfloat::ieee::Single failed to parse `{}`: {:?}" , num, e)
1373- } ) ;
1374- if neg {
1375- f = -f;
1376- }
1377- ( f. to_bits ( ) , 4 )
1378- }
1379- ast:: FloatTy :: F64 => {
1380- num. parse :: < f64 > ( ) . map_err ( |_| ( ) ) ?;
1381- let mut f = num. parse :: < Double > ( ) . unwrap_or_else ( |e| {
1382- panic ! ( "apfloat::ieee::Single failed to parse `{}`: {:?}" , num, e)
1383- } ) ;
1384- if neg {
1385- f = -f;
1386- }
1387- ( f. to_bits ( ) , 8 )
1388- }
1389- } ;
1390-
1391- Ok ( ConstValue :: Scalar ( Scalar :: Bits { bits, size } ) )
1392- }
0 commit comments