@@ -132,14 +132,18 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
132132 } ;
133133
134134 self . tcx . with_freevars ( closure_node_id, |freevars| {
135+ let mut freevar_list: Vec < ty:: UpvarId > = Vec :: with_capacity ( freevars. len ( ) ) ;
135136 for freevar in freevars {
136137 let upvar_id = ty:: UpvarId {
137138 var_path : ty:: UpvarPath {
138- hir_id : self . tcx . hir ( ) . node_to_hir_id ( freevar. var_id ( ) ) ,
139+ hir_id : self . tcx . hir ( ) . node_to_hir_id ( freevar. var_id ( ) ) ,
139140 } ,
140141 closure_expr_id : LocalDefId :: from_def_id ( closure_def_id) ,
141142 } ;
142143 debug ! ( "seed upvar_id {:?}" , upvar_id) ;
144+ // Adding the upvar Id to the list of Upvars, which will be added
145+ // to the map for the closure at the end of the for loop.
146+ freevar_list. push ( upvar_id) ;
143147
144148 let capture_kind = match capture_clause {
145149 hir:: CaptureByValue => ty:: UpvarCapture :: ByValue ,
@@ -159,6 +163,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
159163 . upvar_capture_map
160164 . insert ( upvar_id, capture_kind) ;
161165 }
166+ // Add the vector of freevars to the map keyed with the closure id.
167+ // This gives us an easier access to them without having to call
168+ // with_freevars again..
169+ self . tables
170+ . borrow_mut ( )
171+ . upvar_list
172+ . insert ( closure_def_id, freevar_list) ;
162173 } ) ;
163174
164175 let body_owner_def_id = self . tcx . hir ( ) . body_owner_def_id ( body. id ( ) ) ;
@@ -176,7 +187,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
176187 self . param_env ,
177188 region_scope_tree,
178189 & self . tables . borrow ( ) ,
179- ) . consume_body ( body) ;
190+ )
191+ . consume_body ( body) ;
180192
181193 if let Some ( closure_substs) = infer_kind {
182194 // Unify the (as yet unbound) type variable in the closure
@@ -250,9 +262,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
250262 let var_hir_id = tcx. hir ( ) . node_to_hir_id ( var_node_id) ;
251263 let freevar_ty = self . node_ty ( var_hir_id) ;
252264 let upvar_id = ty:: UpvarId {
253- var_path : ty:: UpvarPath {
254- hir_id : var_hir_id,
255- } ,
265+ var_path : ty:: UpvarPath { hir_id : var_hir_id } ,
256266 closure_expr_id : LocalDefId :: from_def_id ( closure_def_index) ,
257267 } ;
258268 let capture = self . tables . borrow ( ) . upvar_capture ( upvar_id) ;
@@ -272,7 +282,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
272282 } ,
273283 ) ,
274284 }
275- } ) . collect ( )
285+ } )
286+ . collect ( )
276287 } )
277288 }
278289}
0 commit comments