Skip to content

Commit 726c2b6

Browse files
committed
Don't ICE when a float can't be parsed
1 parent dd6e386 commit 726c2b6

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/librustc_const_eval/eval.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ pub fn eval_const_expr_partial<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
872872
debug!("const call({:?})", call_args);
873873
eval_const_expr_partial(tcx, &result, ty_hint, Some(&call_args))?
874874
},
875-
hir::ExprLit(ref lit) => match lit_to_const(&lit.node, tcx, ety, lit.span) {
875+
hir::ExprLit(ref lit) => match lit_to_const(&lit.node, tcx, ety) {
876876
Ok(val) => val,
877877
Err(err) => signal!(e, err),
878878
},
@@ -1208,8 +1208,7 @@ fn cast_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, val: ConstVal, ty: ty::Ty)
12081208

12091209
fn lit_to_const<'a, 'tcx>(lit: &ast::LitKind,
12101210
tcx: TyCtxt<'a, 'tcx, 'tcx>,
1211-
ty_hint: Option<Ty<'tcx>>,
1212-
span: Span)
1211+
ty_hint: Option<Ty<'tcx>>)
12131212
-> Result<ConstVal, ErrKind> {
12141213
use syntax::ast::*;
12151214
use syntax::ast::LitIntType::*;
@@ -1243,21 +1242,22 @@ fn lit_to_const<'a, 'tcx>(lit: &ast::LitKind,
12431242
},
12441243

12451244
LitKind::Float(ref n, fty) => {
1246-
Ok(Float(parse_float(n, Some(fty), span)))
1245+
parse_float(n, Some(fty)).map(Float)
12471246
}
12481247
LitKind::FloatUnsuffixed(ref n) => {
12491248
let fty_hint = match ty_hint.map(|t| &t.sty) {
12501249
Some(&ty::TyFloat(fty)) => Some(fty),
12511250
_ => None
12521251
};
1253-
Ok(Float(parse_float(n, fty_hint, span)))
1252+
parse_float(n, fty_hint).map(Float)
12541253
}
12551254
LitKind::Bool(b) => Ok(Bool(b)),
12561255
LitKind::Char(c) => Ok(Char(c)),
12571256
}
12581257
}
12591258

1260-
fn parse_float(num: &str, fty_hint: Option<ast::FloatTy>, span: Span) -> ConstFloat {
1259+
fn parse_float(num: &str, fty_hint: Option<ast::FloatTy>)
1260+
-> Result<ConstFloat, ErrKind> {
12611261
let val = match fty_hint {
12621262
Some(ast::FloatTy::F32) => num.parse::<f32>().map(F32),
12631263
Some(ast::FloatTy::F64) => num.parse::<f64>().map(F64),
@@ -1269,9 +1269,9 @@ fn parse_float(num: &str, fty_hint: Option<ast::FloatTy>, span: Span) -> ConstFl
12691269
})
12701270
}
12711271
};
1272-
val.unwrap_or_else(|_| {
1272+
val.map_err(|_| {
12731273
// FIXME(#31407) this is only necessary because float parsing is buggy
1274-
span_bug!(span, "could not evaluate float literal (see issue #31407)");
1274+
UnimplementedConstVal("could not evaluate float literal (see issue #31407)")
12751275
})
12761276
}
12771277

src/test/compile-fail/issue-31109.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ fn main() {
1212
// FIXME(#31407) this error should go away, but in the meantime we test that it
1313
// is accompanied by a somewhat useful error message.
1414
let _: f64 = 1234567890123456789012345678901234567890e-340; //~ ERROR could not evaluate float
15+
//~ ERROR unimplemented constant expression: could not evaluate float literal
1516
}

0 commit comments

Comments
 (0)