@@ -27,7 +27,7 @@ use util::num::ToPrimitive;
27
27
use util:: nodemap:: NodeMap ;
28
28
29
29
use graphviz:: IntoCow ;
30
- use syntax:: { ast, abi } ;
30
+ use syntax:: ast;
31
31
use rustc_front:: hir:: Expr ;
32
32
use rustc_front:: hir;
33
33
use rustc_front:: intravisit:: FnKind ;
@@ -1089,19 +1089,16 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
1089
1089
hir:: ExprCall ( ref callee, ref args) => {
1090
1090
let sub_ty_hint = ty_hint. erase_hint ( ) ;
1091
1091
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" ) ;
1105
1102
assert_eq ! ( decl. inputs. len( ) , args. len( ) ) ;
1106
1103
1107
1104
let mut call_args = NodeMap ( ) ;
@@ -1116,7 +1113,6 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
1116
1113
let old = call_args. insert ( arg. pat . id , arg_val) ;
1117
1114
assert ! ( old. is_none( ) ) ;
1118
1115
}
1119
- let result = block. expr . as_ref ( ) . unwrap ( ) ;
1120
1116
debug ! ( "const call({:?})" , call_args) ;
1121
1117
try!( eval_const_expr_partial ( tcx, & * * result, ty_hint, Some ( & call_args) ) )
1122
1118
} ,
@@ -1397,46 +1393,3 @@ pub fn compare_lit_exprs<'tcx>(tcx: &ty::ctxt<'tcx>,
1397
1393
} ;
1398
1394
compare_const_vals ( & a, & b)
1399
1395
}
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