Skip to content

Commit

Permalink
Reintroduce the float parsing error
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Apr 30, 2018
1 parent 40b118c commit cf103e5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
13 changes: 9 additions & 4 deletions src/librustc_mir/hair/cx/mod.rs
Expand Up @@ -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);
Expand Down Expand Up @@ -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)),
Expand Down
26 changes: 14 additions & 12 deletions src/librustc_mir/hair/pattern/mod.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)),
Expand All @@ -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;
}
Expand Down

0 comments on commit cf103e5

Please sign in to comment.