) -> 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);
},
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)),
use syntax::ast;
use syntax::ptr::P;
use syntax_pos::Span;
+use syntax_pos::symbol::Symbol;
#[derive(Clone, Debug)]
pub enum PatternError {
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)),
}
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;
}