@@ -28,7 +28,7 @@ use util::num::ToPrimitive;
2828use util:: nodemap:: NodeMap ;
2929
3030use graphviz:: IntoCow ;
31- use syntax:: { ast, abi } ;
31+ use syntax:: ast;
3232use rustc_front:: hir:: Expr ;
3333use rustc_front:: hir;
3434use rustc_front:: intravisit:: FnKind ;
@@ -1090,19 +1090,16 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
10901090 hir:: ExprCall ( ref callee, ref args) => {
10911091 let sub_ty_hint = ty_hint. erase_hint ( ) ;
10921092 let callee_val = try!( eval_const_expr_partial ( tcx, callee, sub_ty_hint, fn_args) ) ;
1093- let ( decl, block, constness) = try!( get_fn_def ( tcx, e, callee_val) ) ;
1094- match ( ty_hint, constness) {
1095- ( ExprTypeChecked , _) => {
1096- // no need to check for constness... either check_const
1097- // already forbids this or we const eval over whatever
1098- // we want
1099- } ,
1100- ( _, hir:: Constness :: Const ) => {
1101- // we don't know much about the function, so we force it to be a const fn
1102- // so compilation will fail later in case the const fn's body is not const
1103- } ,
1104- _ => signal ! ( e, NonConstPath ) ,
1105- }
1093+ let did = match callee_val {
1094+ Function ( did) => did,
1095+ callee => signal ! ( e, CallOn ( callee) ) ,
1096+ } ;
1097+ let ( decl, result) = if let Some ( fn_like) = lookup_const_fn_by_id ( tcx, did) {
1098+ ( fn_like. decl ( ) , & fn_like. body ( ) . expr )
1099+ } else {
1100+ signal ! ( e, NonConstPath )
1101+ } ;
1102+ let result = result. as_ref ( ) . expect ( "const fn has no result expression" ) ;
11061103 assert_eq ! ( decl. inputs. len( ) , args. len( ) ) ;
11071104
11081105 let mut call_args = NodeMap ( ) ;
@@ -1117,7 +1114,6 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
11171114 let old = call_args. insert ( arg. pat . id , arg_val) ;
11181115 assert ! ( old. is_none( ) ) ;
11191116 }
1120- let result = block. expr . as_ref ( ) . unwrap ( ) ;
11211117 debug ! ( "const call({:?})" , call_args) ;
11221118 try!( eval_const_expr_partial ( tcx, & * * result, ty_hint, Some ( & call_args) ) )
11231119 } ,
@@ -1389,46 +1385,3 @@ pub fn compare_lit_exprs<'tcx>(tcx: &ty::ctxt<'tcx>,
13891385 } ;
13901386 compare_const_vals ( & a, & b)
13911387}
1392-
1393-
1394- // returns Err if callee is not `Function`
1395- // `e` is only used for error reporting/spans
1396- fn get_fn_def < ' a > ( tcx : & ' a ty:: ctxt ,
1397- e : & hir:: Expr ,
1398- callee : ConstVal )
1399- -> Result < ( & ' a hir:: FnDecl , & ' a hir:: Block , hir:: Constness ) , ConstEvalErr > {
1400- let did = match callee {
1401- Function ( did) => did,
1402- callee => signal ! ( e, CallOn ( callee) ) ,
1403- } ;
1404- debug ! ( "fn call: {:?}" , tcx. map. get_if_local( did) ) ;
1405- match tcx. map . get_if_local ( did) {
1406- None => signal ! ( e, UnimplementedConstVal ( "calling non-local const fn" ) ) , // non-local
1407- Some ( ast_map:: NodeItem ( it) ) => match it. node {
1408- hir:: ItemFn (
1409- ref decl,
1410- hir:: Unsafety :: Normal ,
1411- constness,
1412- abi:: Abi :: Rust ,
1413- _, // ducktype generics? types are funky in const_eval
1414- ref block,
1415- ) => Ok ( ( & * * decl, & * * block, constness) ) ,
1416- _ => signal ! ( e, NonConstPath ) ,
1417- } ,
1418- Some ( ast_map:: NodeImplItem ( it) ) => match it. node {
1419- hir:: ImplItemKind :: Method (
1420- hir:: MethodSig {
1421- ref decl,
1422- unsafety : hir:: Unsafety :: Normal ,
1423- constness,
1424- abi : abi:: Abi :: Rust ,
1425- .. // ducktype generics? types are funky in const_eval
1426- } ,
1427- ref block,
1428- ) => Ok ( ( decl, block, constness) ) ,
1429- _ => signal ! ( e, NonConstPath ) ,
1430- } ,
1431- Some ( ast_map:: NodeTraitItem ( ..) ) => signal ! ( e, NonConstPath ) ,
1432- Some ( _) => signal ! ( e, UnimplementedConstVal ( "calling struct, tuple or variant" ) ) ,
1433- }
1434- }
0 commit comments