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