Skip to content

Commit a76cb45

Browse files
author
Robin Kruppe
committed
ICE more gracefully in constant evaluation when float parsing fails
Ideally float parsing wouldn't fail at all, but for the moment let's give a helpful message. Fixes #31109
1 parent 33713bc commit a76cb45

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/librustc/middle/const_eval.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use middle::ty::{self, Ty};
2626
use middle::astconv_util::ast_ty_to_prim_ty;
2727
use util::num::ToPrimitive;
2828
use util::nodemap::NodeMap;
29+
use session::Session;
2930

3031
use graphviz::IntoCow;
3132
use syntax::ast;
@@ -1117,7 +1118,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
11171118
debug!("const call({:?})", call_args);
11181119
try!(eval_const_expr_partial(tcx, &**result, ty_hint, Some(&call_args)))
11191120
},
1120-
hir::ExprLit(ref lit) => lit_to_const(&**lit, ety),
1121+
hir::ExprLit(ref lit) => lit_to_const(tcx.sess, e.span, &**lit, ety),
11211122
hir::ExprBlock(ref block) => {
11221123
match block.expr {
11231124
Some(ref expr) => try!(eval_const_expr_partial(tcx, &**expr, ty_hint, fn_args)),
@@ -1319,7 +1320,7 @@ fn cast_const<'tcx>(tcx: &ty::ctxt<'tcx>, val: ConstVal, ty: Ty) -> CastResult {
13191320
}
13201321
}
13211322

1322-
fn lit_to_const(lit: &ast::Lit, ty_hint: Option<Ty>) -> ConstVal {
1323+
fn lit_to_const(sess: &Session, span: Span, lit: &ast::Lit, ty_hint: Option<Ty>) -> ConstVal {
13231324
match lit.node {
13241325
ast::LitStr(ref s, _) => Str((*s).clone()),
13251326
ast::LitByteStr(ref data) => {
@@ -1339,7 +1340,12 @@ fn lit_to_const(lit: &ast::Lit, ty_hint: Option<Ty>) -> ConstVal {
13391340
ast::LitInt(n, ast::UnsignedIntLit(_)) => Uint(n),
13401341
ast::LitFloat(ref n, _) |
13411342
ast::LitFloatUnsuffixed(ref n) => {
1342-
Float(n.parse::<f64>().unwrap() as f64)
1343+
if let Ok(x) = n.parse::<f64>() {
1344+
Float(x)
1345+
} else {
1346+
// FIXME(#31407) this is only necessary because float parsing is buggy
1347+
sess.span_bug(span, "could not evaluate float literal (see issue #31407)");
1348+
}
13431349
}
13441350
ast::LitBool(b) => Bool(b)
13451351
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
// FIXME(#31407) this error should go away, but in the meantime we test that it
13+
// is accompanied by a somewhat useful error message.
14+
let _: f64 = 1234567890123456789012345678901234567890e-340; //~ ERROR could not evaluate float
15+
}

0 commit comments

Comments
 (0)