@@ -880,130 +880,26 @@ fn convert_path_expr<'a, 'tcx>(
880880            ExprKind :: Deref  {  arg :  Expr  {  ty,  temp_lifetime,  span :  expr. span ,  kind } . to_ref ( )  } 
881881        } 
882882
883-         Res :: Local ( var_hir_id)  => convert_var ( cx,  expr ,   var_hir_id) , 
883+         Res :: Local ( var_hir_id)  => convert_var ( cx,  var_hir_id) , 
884884
885885        _ => span_bug ! ( expr. span,  "res `{:?}` not yet implemented" ,  res) , 
886886    } 
887887} 
888888
889- fn  convert_var < ' tcx > ( 
890-     cx :  & mut  Cx < ' _ ,  ' tcx > , 
891-     expr :  & ' tcx  hir:: Expr < ' tcx > , 
892-     var_hir_id :  hir:: HirId , 
893- )  -> ExprKind < ' tcx >  { 
894-     let  upvar_index = cx
895-         . typeck_results ( ) 
896-         . closure_captures 
897-         . get ( & cx. body_owner ) 
898-         . and_then ( |upvars| upvars. get_full ( & var_hir_id) . map ( |( i,  _,  _) | i) ) ; 
899- 
900-     debug ! ( 
901-         "convert_var({:?}): upvar_index={:?}, body_owner={:?}" , 
902-         var_hir_id,  upvar_index,  cx. body_owner
903-     ) ; 
904- 
905-     let  temp_lifetime = cx. region_scope_tree . temporary_scope ( expr. hir_id . local_id ) ; 
906- 
907-     match  upvar_index { 
908-         None  => ExprKind :: VarRef  {  id :  var_hir_id } , 
889+ fn  convert_var < ' tcx > ( cx :  & mut  Cx < ' _ ,  ' tcx > ,  var_hir_id :  hir:: HirId )  -> ExprKind < ' tcx >  { 
890+     // We want upvars here not captures. 
891+     // Captures will be handled in MIR. 
892+     let  is_upvar = cx
893+         . tcx 
894+         . upvars_mentioned ( cx. body_owner ) 
895+         . map_or ( false ,  |upvars| upvars. contains_key ( & var_hir_id) ) ; 
909896
910-         Some ( upvar_index)  => { 
911-             let  closure_def_id = cx. body_owner ; 
912-             let  upvar_id = ty:: UpvarId  { 
913-                 var_path :  ty:: UpvarPath  {  hir_id :  var_hir_id } , 
914-                 closure_expr_id :  closure_def_id. expect_local ( ) , 
915-             } ; 
916-             let  var_ty = cx. typeck_results ( ) . node_type ( var_hir_id) ; 
897+     debug ! ( "convert_var({:?}): is_upvar={}, body_owner={:?}" ,  var_hir_id,  is_upvar,  cx. body_owner) ; 
917898
918-             // FIXME free regions in closures are not right 
919-             let  closure_ty = cx
920-                 . typeck_results ( ) 
921-                 . node_type ( cx. tcx . hir ( ) . local_def_id_to_hir_id ( upvar_id. closure_expr_id ) ) ; 
922- 
923-             // FIXME we're just hard-coding the idea that the 
924-             // signature will be &self or &mut self and hence will 
925-             // have a bound region with number 0 
926-             let  region = ty:: ReFree ( ty:: FreeRegion  { 
927-                 scope :  closure_def_id, 
928-                 bound_region :  ty:: BoundRegion :: BrAnon ( 0 ) , 
929-             } ) ; 
930-             let  region = cx. tcx . mk_region ( region) ; 
931- 
932-             let  self_expr = if  let  ty:: Closure ( _,  closure_substs)  = closure_ty. kind ( )  { 
933-                 match  cx. infcx . closure_kind ( closure_substs) . unwrap ( )  { 
934-                     ty:: ClosureKind :: Fn  => { 
935-                         let  ref_closure_ty = cx. tcx . mk_ref ( 
936-                             region, 
937-                             ty:: TypeAndMut  {  ty :  closure_ty,  mutbl :  hir:: Mutability :: Not  } , 
938-                         ) ; 
939-                         Expr  { 
940-                             ty :  closure_ty, 
941-                             temp_lifetime, 
942-                             span :  expr. span , 
943-                             kind :  ExprKind :: Deref  { 
944-                                 arg :  Expr  { 
945-                                     ty :  ref_closure_ty, 
946-                                     temp_lifetime, 
947-                                     span :  expr. span , 
948-                                     kind :  ExprKind :: SelfRef , 
949-                                 } 
950-                                 . to_ref ( ) , 
951-                             } , 
952-                         } 
953-                     } 
954-                     ty:: ClosureKind :: FnMut  => { 
955-                         let  ref_closure_ty = cx. tcx . mk_ref ( 
956-                             region, 
957-                             ty:: TypeAndMut  {  ty :  closure_ty,  mutbl :  hir:: Mutability :: Mut  } , 
958-                         ) ; 
959-                         Expr  { 
960-                             ty :  closure_ty, 
961-                             temp_lifetime, 
962-                             span :  expr. span , 
963-                             kind :  ExprKind :: Deref  { 
964-                                 arg :  Expr  { 
965-                                     ty :  ref_closure_ty, 
966-                                     temp_lifetime, 
967-                                     span :  expr. span , 
968-                                     kind :  ExprKind :: SelfRef , 
969-                                 } 
970-                                 . to_ref ( ) , 
971-                             } , 
972-                         } 
973-                     } 
974-                     ty:: ClosureKind :: FnOnce  => Expr  { 
975-                         ty :  closure_ty, 
976-                         temp_lifetime, 
977-                         span :  expr. span , 
978-                         kind :  ExprKind :: SelfRef , 
979-                     } , 
980-                 } 
981-             }  else  { 
982-                 Expr  {  ty :  closure_ty,  temp_lifetime,  span :  expr. span ,  kind :  ExprKind :: SelfRef  } 
983-             } ; 
984- 
985-             // at this point we have `self.n`, which loads up the upvar 
986-             let  field_kind =
987-                 ExprKind :: Field  {  lhs :  self_expr. to_ref ( ) ,  name :  Field :: new ( upvar_index)  } ; 
988- 
989-             // ...but the upvar might be an `&T` or `&mut T` capture, at which 
990-             // point we need an implicit deref 
991-             match  cx. typeck_results ( ) . upvar_capture ( upvar_id)  { 
992-                 ty:: UpvarCapture :: ByValue ( _)  => field_kind, 
993-                 ty:: UpvarCapture :: ByRef ( borrow)  => ExprKind :: Deref  { 
994-                     arg :  Expr  { 
995-                         temp_lifetime, 
996-                         ty :  cx. tcx . mk_ref ( 
997-                             borrow. region , 
998-                             ty:: TypeAndMut  {  ty :  var_ty,  mutbl :  borrow. kind . to_mutbl_lossy ( )  } , 
999-                         ) , 
1000-                         span :  expr. span , 
1001-                         kind :  field_kind, 
1002-                     } 
1003-                     . to_ref ( ) , 
1004-                 } , 
1005-             } 
1006-         } 
899+     if  is_upvar { 
900+         ExprKind :: UpvarRef  {  closure_def_id :  cx. body_owner ,  var_hir_id } 
901+     }  else  { 
902+         ExprKind :: VarRef  {  id :  var_hir_id } 
1007903    } 
1008904} 
1009905
@@ -1102,7 +998,7 @@ fn capture_upvar<'tcx>(
1102998        temp_lifetime, 
1103999        ty :  var_ty, 
11041000        span :  closure_expr. span , 
1105-         kind :  convert_var ( cx,  closure_expr ,   var_hir_id) , 
1001+         kind :  convert_var ( cx,  var_hir_id) , 
11061002    } ; 
11071003    match  upvar_capture { 
11081004        ty:: UpvarCapture :: ByValue ( _)  => captured_var. to_ref ( ) , 
0 commit comments