]> git.lizzy.rs Git - rust.git/commitdiff
Reintroduce the float parsing error
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Thu, 26 Apr 2018 08:39:04 +0000 (10:39 +0200)
committerOliver Schneider <git-no-reply-9879165716479413131@oli-obk.de>
Mon, 30 Apr 2018 16:18:33 +0000 (18:18 +0200)
src/librustc_mir/hair/cx/mod.rs
src/librustc_mir/hair/pattern/mod.rs

index ff5c80845919093ad167c219d483bdbb42a9aebe..5890ea5c9d0c653cc8d186986dbb1c4dd303ade1 100644 (file)
@@ -171,6 +171,13 @@ pub fn const_eval_literal(
     ) -> Literal<'tcx> {
         trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
 
+        let parse_float = |num, fty| -> Value {
+            parse_float(num, fty, neg).unwrap_or_else(|_| {
+                // FIXME(#31407) this is only necessary because float parsing is buggy
+                self.tcx.sess.span_fatal(sp, "could not evaluate float literal (see issue #31407)");
+            })
+        };
+
         let clamp = |n| {
             let size = self.integer_bit_width(ty);
             trace!("clamp {} with size {} and amt {}", n, size, 128 - size);
@@ -205,16 +212,14 @@ pub fn const_eval_literal(
             },
             LitKind::Int(n, _) => Value::ByVal(PrimVal::Bytes(clamp(n))),
             LitKind::Float(n, fty) => {
-                let n = n.as_str();
-                parse_float(&n, fty, neg).expect("apfloat parsing failed")
+                parse_float(n, fty)
             }
             LitKind::FloatUnsuffixed(n) => {
                 let fty = match ty.sty {
                     ty::TyFloat(fty) => fty,
                     _ => bug!()
                 };
-                let n = n.as_str();
-                parse_float(&n, fty, neg).expect("apfloat parsing failed")
+                parse_float(n, fty)
             }
             LitKind::Bool(b) => Value::ByVal(PrimVal::Bytes(b as u128)),
             LitKind::Char(c) => Value::ByVal(PrimVal::Bytes(c as u128)),
index 590cc77c46d7786fe6e5ab428a4d4a0c2e7b3a04..619b4596b42277c0649439b94ed9eef25f90be9e 100644 (file)
@@ -34,6 +34,7 @@
 use syntax::ast;
 use syntax::ptr::P;
 use syntax_pos::Span;
+use syntax_pos::symbol::Symbol;
 
 #[derive(Clone, Debug)]
 pub enum PatternError {
@@ -1145,16 +1146,14 @@ enum Int {
             Value::ByVal(PrimVal::Bytes(n))
         },
         LitKind::Float(n, fty) => {
-            let n = n.as_str();
-            parse_float(&n, fty, neg).map_err(|_| ())?
+            parse_float(n, fty, neg)?
         }
         LitKind::FloatUnsuffixed(n) => {
             let fty = match ty.sty {
                 ty::TyFloat(fty) => fty,
                 _ => bug!()
             };
-            let n = n.as_str();
-            parse_float(&n, fty, neg).map_err(|_| ())?
+            parse_float(n, fty, neg)?
         }
         LitKind::Bool(b) => Value::ByVal(PrimVal::Bytes(b as u128)),
         LitKind::Char(c) => Value::ByVal(PrimVal::Bytes(c as u128)),
@@ -1163,26 +1162,29 @@ enum Int {
 }
 
 pub fn parse_float(
-    num: &str,
+    num: Symbol,
     fty: ast::FloatTy,
     neg: bool,
-) -> Result<Value, String> {
+) -> Result<Value, ()> {
+    let num = num.as_str();
     use rustc_apfloat::ieee::{Single, Double};
     use rustc_apfloat::Float;
     let bits = match fty {
         ast::FloatTy::F32 => {
-            let mut f = num.parse::<Single>().map_err(|e| {
-                format!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
-            })?;
+            num.parse::<f32>().map_err(|_| ())?;
+            let mut f = num.parse::<Single>().unwrap_or_else(|e| {
+                panic!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
+            });
             if neg {
                 f = -f;
             }
             f.to_bits()
         }
         ast::FloatTy::F64 => {
-            let mut f = num.parse::<Double>().map_err(|e| {
-                format!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
-            })?;
+            num.parse::<f64>().map_err(|_| ())?;
+            let mut f = num.parse::<Double>().unwrap_or_else(|e| {
+                panic!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
+            });
             if neg {
                 f = -f;
             }