Skip to content

Commit cf103e5

Browse files
committed
Reintroduce the float parsing error
1 parent 40b118c commit cf103e5

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

src/librustc_mir/hair/cx/mod.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
171171
) -> Literal<'tcx> {
172172
trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
173173

174+
let parse_float = |num, fty| -> Value {
175+
parse_float(num, fty, neg).unwrap_or_else(|_| {
176+
// FIXME(#31407) this is only necessary because float parsing is buggy
177+
self.tcx.sess.span_fatal(sp, "could not evaluate float literal (see issue #31407)");
178+
})
179+
};
180+
174181
let clamp = |n| {
175182
let size = self.integer_bit_width(ty);
176183
trace!("clamp {} with size {} and amt {}", n, size, 128 - size);
@@ -205,16 +212,14 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
205212
},
206213
LitKind::Int(n, _) => Value::ByVal(PrimVal::Bytes(clamp(n))),
207214
LitKind::Float(n, fty) => {
208-
let n = n.as_str();
209-
parse_float(&n, fty, neg).expect("apfloat parsing failed")
215+
parse_float(n, fty)
210216
}
211217
LitKind::FloatUnsuffixed(n) => {
212218
let fty = match ty.sty {
213219
ty::TyFloat(fty) => fty,
214220
_ => bug!()
215221
};
216-
let n = n.as_str();
217-
parse_float(&n, fty, neg).expect("apfloat parsing failed")
222+
parse_float(n, fty)
218223
}
219224
LitKind::Bool(b) => Value::ByVal(PrimVal::Bytes(b as u128)),
220225
LitKind::Char(c) => Value::ByVal(PrimVal::Bytes(c as u128)),

src/librustc_mir/hair/pattern/mod.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use std::fmt;
3434
use syntax::ast;
3535
use syntax::ptr::P;
3636
use syntax_pos::Span;
37+
use syntax_pos::symbol::Symbol;
3738

3839
#[derive(Clone, Debug)]
3940
pub enum PatternError {
@@ -1145,16 +1146,14 @@ fn lit_to_const<'a, 'tcx>(lit: &'tcx ast::LitKind,
11451146
Value::ByVal(PrimVal::Bytes(n))
11461147
},
11471148
LitKind::Float(n, fty) => {
1148-
let n = n.as_str();
1149-
parse_float(&n, fty, neg).map_err(|_| ())?
1149+
parse_float(n, fty, neg)?
11501150
}
11511151
LitKind::FloatUnsuffixed(n) => {
11521152
let fty = match ty.sty {
11531153
ty::TyFloat(fty) => fty,
11541154
_ => bug!()
11551155
};
1156-
let n = n.as_str();
1157-
parse_float(&n, fty, neg).map_err(|_| ())?
1156+
parse_float(n, fty, neg)?
11581157
}
11591158
LitKind::Bool(b) => Value::ByVal(PrimVal::Bytes(b as u128)),
11601159
LitKind::Char(c) => Value::ByVal(PrimVal::Bytes(c as u128)),
@@ -1163,26 +1162,29 @@ fn lit_to_const<'a, 'tcx>(lit: &'tcx ast::LitKind,
11631162
}
11641163

11651164
pub fn parse_float(
1166-
num: &str,
1165+
num: Symbol,
11671166
fty: ast::FloatTy,
11681167
neg: bool,
1169-
) -> Result<Value, String> {
1168+
) -> Result<Value, ()> {
1169+
let num = num.as_str();
11701170
use rustc_apfloat::ieee::{Single, Double};
11711171
use rustc_apfloat::Float;
11721172
let bits = match fty {
11731173
ast::FloatTy::F32 => {
1174-
let mut f = num.parse::<Single>().map_err(|e| {
1175-
format!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
1176-
})?;
1174+
num.parse::<f32>().map_err(|_| ())?;
1175+
let mut f = num.parse::<Single>().unwrap_or_else(|e| {
1176+
panic!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
1177+
});
11771178
if neg {
11781179
f = -f;
11791180
}
11801181
f.to_bits()
11811182
}
11821183
ast::FloatTy::F64 => {
1183-
let mut f = num.parse::<Double>().map_err(|e| {
1184-
format!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
1185-
})?;
1184+
num.parse::<f64>().map_err(|_| ())?;
1185+
let mut f = num.parse::<Double>().unwrap_or_else(|e| {
1186+
panic!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
1187+
});
11861188
if neg {
11871189
f = -f;
11881190
}

0 commit comments

Comments
 (0)