11use rustc_middle:: mir:: visit:: * ;
22use rustc_middle:: mir:: * ;
3- use rustc_middle:: ty:: { self , ParamEnv , TyCtxt } ;
3+ use rustc_middle:: ty:: { self , ParamEnv , Ty , TyCtxt } ;
44
55const INSTR_COST : usize = 5 ;
66const CALL_PENALTY : usize = 25 ;
@@ -14,14 +14,14 @@ pub(crate) struct CostChecker<'b, 'tcx> {
1414 param_env : ParamEnv < ' tcx > ,
1515 cost : usize ,
1616 callee_body : & ' b Body < ' tcx > ,
17- instance : ty:: Instance < ' tcx > ,
17+ instance : Option < ty:: Instance < ' tcx > > ,
1818}
1919
2020impl < ' b , ' tcx > CostChecker < ' b , ' tcx > {
2121 pub fn new (
2222 tcx : TyCtxt < ' tcx > ,
2323 param_env : ParamEnv < ' tcx > ,
24- instance : ty:: Instance < ' tcx > ,
24+ instance : Option < ty:: Instance < ' tcx > > ,
2525 callee_body : & ' b Body < ' tcx > ,
2626 ) -> CostChecker < ' b , ' tcx > {
2727 CostChecker { tcx, param_env, callee_body, instance, cost : 0 }
@@ -30,6 +30,14 @@ impl<'b, 'tcx> CostChecker<'b, 'tcx> {
3030 pub fn cost ( & self ) -> usize {
3131 self . cost
3232 }
33+
34+ fn instantiate_ty ( & self , v : Ty < ' tcx > ) -> Ty < ' tcx > {
35+ if let Some ( instance) = self . instance {
36+ instance. instantiate_mir ( self . tcx , ty:: EarlyBinder :: bind ( & v) )
37+ } else {
38+ v
39+ }
40+ }
3341}
3442
3543impl < ' tcx > Visitor < ' tcx > for CostChecker < ' _ , ' tcx > {
@@ -49,10 +57,7 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
4957 match terminator. kind {
5058 TerminatorKind :: Drop { ref place, unwind, .. } => {
5159 // If the place doesn't actually need dropping, treat it like a regular goto.
52- let ty = self . instance . instantiate_mir (
53- tcx,
54- ty:: EarlyBinder :: bind ( & place. ty ( self . callee_body , tcx) . ty ) ,
55- ) ;
60+ let ty = self . instantiate_ty ( place. ty ( self . callee_body , tcx) . ty ) ;
5661 if ty. needs_drop ( tcx, self . param_env ) {
5762 self . cost += CALL_PENALTY ;
5863 if let UnwindAction :: Cleanup ( _) = unwind {
@@ -63,8 +68,7 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
6368 }
6469 }
6570 TerminatorKind :: Call { func : Operand :: Constant ( ref f) , unwind, .. } => {
66- let fn_ty =
67- self . instance . instantiate_mir ( tcx, ty:: EarlyBinder :: bind ( & f. const_ . ty ( ) ) ) ;
71+ let fn_ty = self . instantiate_ty ( f. const_ . ty ( ) ) ;
6872 self . cost += if let ty:: FnDef ( def_id, _) = * fn_ty. kind ( ) && tcx. is_intrinsic ( def_id) {
6973 // Don't give intrinsics the extra penalty for calls
7074 INSTR_COST
0 commit comments