@@ -6,7 +6,7 @@ use crate::hair::util::UserAnnotatedTyHelpers;
66use rustc_data_structures:: indexed_vec:: Idx ;
77use rustc:: hir:: def:: { CtorOf , Res , DefKind , CtorKind } ;
88use rustc:: mir:: interpret:: { GlobalId , ErrorHandled , ConstValue } ;
9- use rustc:: ty:: { self , AdtKind , Ty } ;
9+ use rustc:: ty:: { self , AdtKind , DefIdTree , Ty } ;
1010use rustc:: ty:: adjustment:: { Adjustment , Adjust , AutoBorrow , AutoBorrowMutability , PointerCast } ;
1111use rustc:: ty:: subst:: { InternalSubsts , SubstsRef } ;
1212use rustc:: hir;
@@ -960,38 +960,50 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
960960
961961 Res :: Def ( DefKind :: Static , id) => ExprKind :: StaticRef { id } ,
962962
963- Res :: Local ( ..) | Res :: Upvar ( ..) => convert_var ( cx, expr, res) ,
963+ Res :: Local ( var_hir_id) => convert_var ( cx, expr, var_hir_id) ,
964+ Res :: Upvar ( var_hir_id, closure_node_id) => {
965+ let closure_def_id = cx. tcx . hir ( ) . local_def_id ( closure_node_id) ;
966+ assert_eq ! ( cx. body_owner, closure_def_id) ;
967+ assert ! ( cx. upvar_indices. contains_key( & var_hir_id) ) ;
968+
969+ convert_var ( cx, expr, var_hir_id)
970+ }
964971
965972 _ => span_bug ! ( expr. span, "res `{:?}` not yet implemented" , res) ,
966973 }
967974}
968975
969- fn convert_var < ' a , ' gcx , ' tcx > ( cx : & mut Cx < ' a , ' gcx , ' tcx > ,
970- expr : & ' tcx hir:: Expr ,
971- res : Res )
972- -> ExprKind < ' tcx > {
973- let temp_lifetime = cx. region_scope_tree . temporary_scope ( expr. hir_id . local_id ) ;
976+ fn convert_var (
977+ cx : & mut Cx < ' _ , ' _ , ' tcx > ,
978+ expr : & ' tcx hir:: Expr ,
979+ var_hir_id : hir:: HirId ,
980+ ) -> ExprKind < ' tcx > {
981+ let upvar_index = cx. upvar_indices . get ( & var_hir_id) . cloned ( ) ;
974982
975- match res {
976- Res :: Local ( id) => ExprKind :: VarRef { id } ,
983+ debug ! ( "convert_var({:?}): upvar_index={:?}, body_owner={:?}" ,
984+ var_hir_id, upvar_index, cx. body_owner) ;
985+
986+ let temp_lifetime = cx. region_scope_tree . temporary_scope ( expr. hir_id . local_id ) ;
977987
978- Res :: Upvar ( var_hir_id , closure_expr_id ) => {
979- let index = cx . upvar_indices [ & var_hir_id] ;
988+ match upvar_index {
989+ None => ExprKind :: VarRef { id : var_hir_id } ,
980990
981- debug ! ( "convert_var(upvar({:?}, {:?}, {:?}))" ,
982- var_hir_id,
983- index,
984- closure_expr_id) ;
991+ Some ( upvar_index) => {
992+ let closure_def_id = cx. body_owner ;
993+ let upvar_id = ty:: UpvarId {
994+ var_path : ty:: UpvarPath { hir_id : var_hir_id} ,
995+ closure_expr_id : LocalDefId :: from_def_id ( closure_def_id) ,
996+ } ;
985997 let var_ty = cx. tables ( ) . node_type ( var_hir_id) ;
986998
987999 // FIXME free regions in closures are not right
988- let closure_ty = cx. tables ( )
989- . node_type ( cx. tcx . hir ( ) . node_to_hir_id ( closure_expr_id) ) ;
1000+ let closure_ty = cx. tables ( ) . node_type (
1001+ cx. tcx . hir ( ) . local_def_id_to_hir_id ( upvar_id. closure_expr_id ) ,
1002+ ) ;
9901003
9911004 // FIXME we're just hard-coding the idea that the
9921005 // signature will be &self or &mut self and hence will
9931006 // have a bound region with number 0
994- let closure_def_id = cx. tcx . hir ( ) . local_def_id ( closure_expr_id) ;
9951007 let region = ty:: ReFree ( ty:: FreeRegion {
9961008 scope : closure_def_id,
9971009 bound_region : ty:: BoundRegion :: BrAnon ( 0 ) ,
@@ -1062,15 +1074,11 @@ fn convert_var<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
10621074 // at this point we have `self.n`, which loads up the upvar
10631075 let field_kind = ExprKind :: Field {
10641076 lhs : self_expr. to_ref ( ) ,
1065- name : Field :: new ( index ) ,
1077+ name : Field :: new ( upvar_index ) ,
10661078 } ;
10671079
10681080 // ...but the upvar might be an `&T` or `&mut T` capture, at which
10691081 // point we need an implicit deref
1070- let upvar_id = ty:: UpvarId {
1071- var_path : ty:: UpvarPath { hir_id : var_hir_id} ,
1072- closure_expr_id : LocalDefId :: from_def_id ( closure_def_id) ,
1073- } ;
10741082 match cx. tables ( ) . upvar_capture ( upvar_id) {
10751083 ty:: UpvarCapture :: ByValue => field_kind,
10761084 ty:: UpvarCapture :: ByRef ( borrow) => {
@@ -1089,8 +1097,6 @@ fn convert_var<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
10891097 }
10901098 }
10911099 }
1092-
1093- _ => span_bug ! ( expr. span, "type of & not region" ) ,
10941100 }
10951101}
10961102
@@ -1190,15 +1196,16 @@ fn capture_upvar<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
11901196 let upvar_capture = cx. tables ( ) . upvar_capture ( upvar_id) ;
11911197 let temp_lifetime = cx. region_scope_tree . temporary_scope ( closure_expr. hir_id . local_id ) ;
11921198 let var_ty = cx. tables ( ) . node_type ( upvar. var_id ) ;
1193- let upvar_res = upvar. parent . map_or (
1194- Res :: Local ( upvar. var_id ) ,
1195- |closure_node_id| Res :: Upvar ( upvar. var_id , closure_node_id) ,
1196- ) ;
1199+ if upvar. has_parent {
1200+ let closure_def_id = upvar_id. closure_expr_id . to_def_id ( ) ;
1201+ assert_eq ! ( cx. body_owner, cx. tcx. parent( closure_def_id) . unwrap( ) ) ;
1202+ }
1203+ assert_eq ! ( upvar. has_parent, cx. upvar_indices. contains_key( & upvar. var_id) ) ;
11971204 let captured_var = Expr {
11981205 temp_lifetime,
11991206 ty : var_ty,
12001207 span : closure_expr. span ,
1201- kind : convert_var ( cx, closure_expr, upvar_res ) ,
1208+ kind : convert_var ( cx, closure_expr, upvar . var_id ) ,
12021209 } ;
12031210 match upvar_capture {
12041211 ty:: UpvarCapture :: ByValue => captured_var. to_ref ( ) ,
0 commit comments