]> git.proxmox.com Git - rustc.git/blobdiff - compiler/rustc_mir_build/src/thir/constant.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / compiler / rustc_mir_build / src / thir / constant.rs
index d62fd161e2f86e0f84d7272faca42c0b52cbd6b3..f9e7b39f7049ce6ea86da0d9f73aac47fe02c62e 100644 (file)
@@ -38,7 +38,7 @@ crate fn lit_to_const<'tcx>(
         }
         (ast::LitKind::ByteStr(data), ty::Ref(_, inner_ty, _)) if inner_ty.is_array() => {
             let id = tcx.allocate_bytes(data);
-            ConstValue::Scalar(Scalar::Ptr(id.into()))
+            ConstValue::Scalar(Scalar::from_pointer(id.into(), &tcx))
         }
         (ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
             ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1)))
@@ -46,9 +46,7 @@ crate fn lit_to_const<'tcx>(
         (ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => {
             trunc(if neg { (*n as i128).overflowing_neg().0 as u128 } else { *n })?
         }
-        (ast::LitKind::Float(n, _), ty::Float(fty)) => {
-            parse_float(*n, *fty, neg).map_err(|_| LitToConstError::UnparseableFloat)?
-        }
+        (ast::LitKind::Float(n, _), ty::Float(fty)) => parse_float(*n, *fty, neg),
         (ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)),
         (ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)),
         (ast::LitKind::Err(_), _) => return Err(LitToConstError::Reported),
@@ -57,12 +55,14 @@ crate fn lit_to_const<'tcx>(
     Ok(ty::Const::from_value(tcx, lit, ty))
 }
 
-fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> Result<ConstValue<'tcx>, ()> {
+fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> ConstValue<'tcx> {
     let num = num.as_str();
     use rustc_apfloat::ieee::{Double, Single};
     let scalar = match fty {
         ty::FloatTy::F32 => {
-            let rust_f = num.parse::<f32>().map_err(|_| ())?;
+            let rust_f = num
+                .parse::<f32>()
+                .unwrap_or_else(|e| panic!("f32 failed to parse `{}`: {:?}", num, e));
             let mut f = num.parse::<Single>().unwrap_or_else(|e| {
                 panic!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
             });
@@ -82,7 +82,9 @@ fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> Result<ConstVa
             Scalar::from_f32(f)
         }
         ty::FloatTy::F64 => {
-            let rust_f = num.parse::<f64>().map_err(|_| ())?;
+            let rust_f = num
+                .parse::<f64>()
+                .unwrap_or_else(|e| panic!("f64 failed to parse `{}`: {:?}", num, e));
             let mut f = num.parse::<Double>().unwrap_or_else(|e| {
                 panic!("apfloat::ieee::Double failed to parse `{}`: {:?}", num, e)
             });
@@ -103,5 +105,5 @@ fn parse_float<'tcx>(num: Symbol, fty: ty::FloatTy, neg: bool) -> Result<ConstVa
         }
     };
 
-    Ok(ConstValue::Scalar(scalar))
+    ConstValue::Scalar(scalar)
 }