@@ -19,6 +19,7 @@ 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 } ;
@@ -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 {
@@ -891,12 +891,11 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
891891 ) ;
892892 * self . const_to_pat ( instance, val, expr. hir_id , lit. span ) . kind
893893 } ,
894- Err ( e) => {
895- if e == LitToConstError :: UnparseableFloat {
896- self . errors . push ( PatternError :: FloatBug ) ;
897- }
894+ Err ( LitToConstError :: UnparseableFloat ) => {
895+ self . errors . push ( PatternError :: FloatBug ) ;
898896 PatternKind :: Wild
899897 } ,
898+ Err ( LitToConstError :: Reported ) => PatternKind :: Wild ,
900899 }
901900 } ,
902901 hir:: ExprKind :: Path ( ref qpath) => * self . lower_path ( qpath, expr. hir_id , expr. span ) . kind ,
@@ -914,12 +913,11 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
914913 ) ;
915914 * self . const_to_pat ( instance, val, expr. hir_id , lit. span ) . kind
916915 } ,
917- Err ( e) => {
918- if e == LitToConstError :: UnparseableFloat {
919- self . errors . push ( PatternError :: FloatBug ) ;
920- }
916+ Err ( LitToConstError :: UnparseableFloat ) => {
917+ self . errors . push ( PatternError :: FloatBug ) ;
921918 PatternKind :: Wild
922919 } ,
920+ Err ( LitToConstError :: Reported ) => PatternKind :: Wild ,
923921 }
924922 }
925923 _ => span_bug ! ( expr. span, "not a literal: {:?}" , expr) ,
@@ -1294,124 +1292,3 @@ pub fn compare_const_vals<'a, 'tcx>(
12941292
12951293 fallback ( )
12961294}
1297-
1298- #[ derive( PartialEq ) ]
1299- enum LitToConstError {
1300- UnparseableFloat ,
1301- Propagated ,
1302- }
1303-
1304- // FIXME: Combine with rustc_mir::hair::cx::const_eval_literal
1305- fn lit_to_const < ' a , ' tcx > ( lit : & ' tcx ast:: LitKind ,
1306- tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
1307- ty : Ty < ' tcx > ,
1308- neg : bool )
1309- -> Result < & ' tcx ty:: Const < ' tcx > , LitToConstError > {
1310- use syntax:: ast:: * ;
1311-
1312- use rustc:: mir:: interpret:: * ;
1313- let lit = match * lit {
1314- LitKind :: Str ( ref s, _) => {
1315- let s = s. as_str ( ) ;
1316- let id = tcx. allocate_bytes ( s. as_bytes ( ) ) ;
1317- ConstValue :: new_slice ( Scalar :: Ptr ( id. into ( ) ) , s. len ( ) as u64 , & tcx)
1318- } ,
1319- LitKind :: ByteStr ( ref data) => {
1320- let id = tcx. allocate_bytes ( data) ;
1321- ConstValue :: Scalar ( Scalar :: Ptr ( id. into ( ) ) )
1322- } ,
1323- LitKind :: Byte ( n) => ConstValue :: Scalar ( Scalar :: Bits {
1324- bits : n as u128 ,
1325- size : 1 ,
1326- } ) ,
1327- LitKind :: Int ( n, _) => {
1328- enum Int {
1329- Signed ( IntTy ) ,
1330- Unsigned ( UintTy ) ,
1331- }
1332- let ity = match ty. sty {
1333- ty:: Int ( IntTy :: Isize ) => Int :: Signed ( tcx. sess . target . isize_ty ) ,
1334- ty:: Int ( other) => Int :: Signed ( other) ,
1335- ty:: Uint ( UintTy :: Usize ) => Int :: Unsigned ( tcx. sess . target . usize_ty ) ,
1336- ty:: Uint ( other) => Int :: Unsigned ( other) ,
1337- ty:: Error => { // Avoid ICE (#51963)
1338- return Err ( LitToConstError :: Propagated ) ;
1339- }
1340- _ => bug ! ( "literal integer type with bad type ({:?})" , ty. sty) ,
1341- } ;
1342- // This converts from LitKind::Int (which is sign extended) to
1343- // Scalar::Bytes (which is zero extended)
1344- let n = match ity {
1345- // FIXME(oli-obk): are these casts correct?
1346- Int :: Signed ( IntTy :: I8 ) if neg =>
1347- ( n as i8 ) . overflowing_neg ( ) . 0 as u8 as u128 ,
1348- Int :: Signed ( IntTy :: I16 ) if neg =>
1349- ( n as i16 ) . overflowing_neg ( ) . 0 as u16 as u128 ,
1350- Int :: Signed ( IntTy :: I32 ) if neg =>
1351- ( n as i32 ) . overflowing_neg ( ) . 0 as u32 as u128 ,
1352- Int :: Signed ( IntTy :: I64 ) if neg =>
1353- ( n as i64 ) . overflowing_neg ( ) . 0 as u64 as u128 ,
1354- Int :: Signed ( IntTy :: I128 ) if neg =>
1355- ( n as i128 ) . overflowing_neg ( ) . 0 as u128 ,
1356- Int :: Signed ( IntTy :: I8 ) | Int :: Unsigned ( UintTy :: U8 ) => n as u8 as u128 ,
1357- Int :: Signed ( IntTy :: I16 ) | Int :: Unsigned ( UintTy :: U16 ) => n as u16 as u128 ,
1358- Int :: Signed ( IntTy :: I32 ) | Int :: Unsigned ( UintTy :: U32 ) => n as u32 as u128 ,
1359- Int :: Signed ( IntTy :: I64 ) | Int :: Unsigned ( UintTy :: U64 ) => n as u64 as u128 ,
1360- Int :: Signed ( IntTy :: I128 ) | Int :: Unsigned ( UintTy :: U128 ) => n,
1361- _ => bug ! ( ) ,
1362- } ;
1363- let size = tcx. layout_of ( ty:: ParamEnv :: empty ( ) . and ( ty) ) . unwrap ( ) . size . bytes ( ) as u8 ;
1364- ConstValue :: Scalar ( Scalar :: Bits {
1365- bits : n,
1366- size,
1367- } )
1368- } ,
1369- LitKind :: Float ( n, fty) => {
1370- parse_float ( n, fty, neg) . map_err ( |_| LitToConstError :: UnparseableFloat ) ?
1371- }
1372- LitKind :: FloatUnsuffixed ( n) => {
1373- let fty = match ty. sty {
1374- ty:: Float ( fty) => fty,
1375- _ => bug ! ( )
1376- } ;
1377- parse_float ( n, fty, neg) . map_err ( |_| LitToConstError :: UnparseableFloat ) ?
1378- }
1379- LitKind :: Bool ( b) => ConstValue :: Scalar ( Scalar :: from_bool ( b) ) ,
1380- LitKind :: Char ( c) => ConstValue :: Scalar ( Scalar :: from_char ( c) ) ,
1381- } ;
1382- Ok ( ty:: Const :: from_const_value ( tcx, lit, ty) )
1383- }
1384-
1385- pub fn parse_float < ' tcx > (
1386- num : Symbol ,
1387- fty : ast:: FloatTy ,
1388- neg : bool ,
1389- ) -> Result < ConstValue < ' tcx > , ( ) > {
1390- let num = num. as_str ( ) ;
1391- use rustc_apfloat:: ieee:: { Single , Double } ;
1392- use rustc_apfloat:: Float ;
1393- let ( bits, size) = match fty {
1394- ast:: FloatTy :: F32 => {
1395- num. parse :: < f32 > ( ) . map_err ( |_| ( ) ) ?;
1396- let mut f = num. parse :: < Single > ( ) . unwrap_or_else ( |e| {
1397- panic ! ( "apfloat::ieee::Single failed to parse `{}`: {:?}" , num, e)
1398- } ) ;
1399- if neg {
1400- f = -f;
1401- }
1402- ( f. to_bits ( ) , 4 )
1403- }
1404- ast:: FloatTy :: F64 => {
1405- num. parse :: < f64 > ( ) . map_err ( |_| ( ) ) ?;
1406- let mut f = num. parse :: < Double > ( ) . unwrap_or_else ( |e| {
1407- panic ! ( "apfloat::ieee::Single failed to parse `{}`: {:?}" , num, e)
1408- } ) ;
1409- if neg {
1410- f = -f;
1411- }
1412- ( f. to_bits ( ) , 8 )
1413- }
1414- } ;
1415-
1416- Ok ( ConstValue :: Scalar ( Scalar :: Bits { bits, size } ) )
1417- }
0 commit comments