diff options
| author | Oliver Schneider <git-spam-no-reply9815368754983@oli-obk.de> | 2018-04-26 10:39:04 +0200 |
|---|---|---|
| committer | Oliver Schneider <git-no-reply-9879165716479413131@oli-obk.de> | 2018-04-30 18:18:33 +0200 |
| commit | cf103e56bd0bfdc3ef7202ba774a1981adb36a46 (patch) | |
| tree | 49b7303f07b0f1282f55a0b4c74cd85e884d9713 | |
| parent | 40b118cf4767413f7676c97296c222167604485b (diff) | |
| download | rust-cf103e56bd0bfdc3ef7202ba774a1981adb36a46.tar.gz rust-cf103e56bd0bfdc3ef7202ba774a1981adb36a46.zip | |
Reintroduce the float parsing error
| -rw-r--r-- | src/librustc_mir/hair/cx/mod.rs | 13 | ||||
| -rw-r--r-- | src/librustc_mir/hair/pattern/mod.rs | 26 |
2 files changed, 23 insertions, 16 deletions
diff --git a/src/librustc_mir/hair/cx/mod.rs b/src/librustc_mir/hair/cx/mod.rs index ff5c8084591..5890ea5c9d0 100644 --- a/src/librustc_mir/hair/cx/mod.rs +++ b/src/librustc_mir/hair/cx/mod.rs @@ -171,6 +171,13 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> { ) -> 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 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> { }, 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)), diff --git a/src/librustc_mir/hair/pattern/mod.rs b/src/librustc_mir/hair/pattern/mod.rs index 590cc77c46d..619b4596b42 100644 --- a/src/librustc_mir/hair/pattern/mod.rs +++ b/src/librustc_mir/hair/pattern/mod.rs @@ -34,6 +34,7 @@ use std::fmt; 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 @@ fn lit_to_const<'a, 'tcx>(lit: &'tcx ast::LitKind, 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 @@ fn lit_to_const<'a, 'tcx>(lit: &'tcx ast::LitKind, } 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; } |
