diff options
| -rw-r--r-- | src/librustc/middle/const_eval.rs | 12 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-31109.rs | 15 |
2 files changed, 24 insertions, 3 deletions
diff --git a/src/librustc/middle/const_eval.rs b/src/librustc/middle/const_eval.rs index 07171e66077..e5fc83cc5f3 100644 --- a/src/librustc/middle/const_eval.rs +++ b/src/librustc/middle/const_eval.rs @@ -26,6 +26,7 @@ use middle::ty::{self, Ty}; use middle::astconv_util::ast_ty_to_prim_ty; use util::num::ToPrimitive; use util::nodemap::NodeMap; +use session::Session; use graphviz::IntoCow; use syntax::ast; @@ -1117,7 +1118,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>, debug!("const call({:?})", call_args); try!(eval_const_expr_partial(tcx, &**result, ty_hint, Some(&call_args))) }, - hir::ExprLit(ref lit) => lit_to_const(&**lit, ety), + hir::ExprLit(ref lit) => lit_to_const(tcx.sess, e.span, &**lit, ety), hir::ExprBlock(ref block) => { match block.expr { 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 { } } -fn lit_to_const(lit: &ast::Lit, ty_hint: Option<Ty>) -> ConstVal { +fn lit_to_const(sess: &Session, span: Span, lit: &ast::Lit, ty_hint: Option<Ty>) -> ConstVal { match lit.node { ast::LitStr(ref s, _) => Str((*s).clone()), ast::LitByteStr(ref data) => { @@ -1339,7 +1340,12 @@ fn lit_to_const(lit: &ast::Lit, ty_hint: Option<Ty>) -> ConstVal { ast::LitInt(n, ast::UnsignedIntLit(_)) => Uint(n), ast::LitFloat(ref n, _) | ast::LitFloatUnsuffixed(ref n) => { - Float(n.parse::<f64>().unwrap() as f64) + if let Ok(x) = n.parse::<f64>() { + Float(x) + } else { + // FIXME(#31407) this is only necessary because float parsing is buggy + sess.span_bug(span, "could not evaluate float literal (see issue #31407)"); + } } ast::LitBool(b) => Bool(b) } diff --git a/src/test/compile-fail/issue-31109.rs b/src/test/compile-fail/issue-31109.rs new file mode 100644 index 00000000000..63b3d58b823 --- /dev/null +++ b/src/test/compile-fail/issue-31109.rs @@ -0,0 +1,15 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + // FIXME(#31407) this error should go away, but in the meantime we test that it + // is accompanied by a somewhat useful error message. + let _: f64 = 1234567890123456789012345678901234567890e-340; //~ ERROR could not evaluate float +} |
