From 2b000feba57a324534008356909e02394784cfcc Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Tue, 27 Oct 2015 09:39:07 +0100 Subject: [PATCH] the const evaluator might run before check_const So we cannot assume that the function call was marked NOT_CONST by check_const. --- src/librustc/middle/const_eval.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/librustc/middle/const_eval.rs b/src/librustc/middle/const_eval.rs index 00b4e34c18fa5..1651e71c49a2a 100644 --- a/src/librustc/middle/const_eval.rs +++ b/src/librustc/middle/const_eval.rs @@ -1031,10 +1031,11 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>, UncheckedExprNoHint // we cannot reason about UncheckedExprHint here }; let ( - decl, - unsafety, - abi, - block, + decl, + unsafety, + abi, + block, + constness, ) = match try!(eval_const_expr_partial(tcx, callee, sub_ty_hint, fn_args)) { Function(did) => if did.is_local() { match tcx.map.find(did.index.as_u32()) { @@ -1042,13 +1043,11 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>, hir::ItemFn( ref decl, unsafety, - _, // no need to check for constness... either check_const - // already forbids this or we const eval over whatever - // we want + constness, abi, _, // ducktype generics? types are funky in const_eval ref block, - ) => (decl, unsafety, abi, block), + ) => (decl, unsafety, abi, block, constness), _ => signal!(e, NonConstPath), }, _ => signal!(e, NonConstPath), @@ -1058,6 +1057,15 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>, }, _ => signal!(e, NonConstPath), }; + if let ExprTypeChecked = ty_hint { + // no need to check for constness... either check_const + // already forbids this or we const eval over whatever + // we want + } else { + // we don't know much about the function, so we force it to be a const fn + // so compilation will fail later in case the const fn's body is not const + assert_eq!(constness, hir::Constness::Const) + } assert_eq!(decl.inputs.len(), args.len()); assert_eq!(unsafety, hir::Unsafety::Normal); assert_eq!(abi, abi::Abi::Rust);