]> git.lizzy.rs Git - rust.git/commitdiff
Issue 56905
authorBlitzerr <rusty.blitzerr@gmail.com>
Mon, 17 Dec 2018 01:01:57 +0000 (17:01 -0800)
committerBlitzerr <rusty.blitzerr@gmail.com>
Wed, 9 Jan 2019 01:57:22 +0000 (17:57 -0800)
Adding a map to TypeckTables to get the list of all the Upvars
given a closureID. This is help us get rid of the recurring
pattern in the codebase of iterating over the free vars
using with_freevars.

src/librustc/ty/context.rs
src/librustc/ty/mod.rs
src/librustc_typeck/check/upvar.rs

index c0ba4329ae05c24d03b05696b3c8fac0ccf81256..aebf71791bc6061ba935f4adb754101f9878f6f0 100644 (file)
@@ -417,6 +417,12 @@ pub struct TypeckTables<'tcx> {
     /// All the existential types that are restricted to concrete types
     /// by this function
     pub concrete_existential_types: FxHashMap<DefId, Ty<'tcx>>,
+
+    /// Given the closure ID this map provides the list of UpvarIDs used by it.
+    /// The upvarID contains the HIR node ID and it also contains the full path
+    /// leading to the member of the struct or tuple that is used instead of the
+    /// entire variable.
+    pub upvar_list: ty::UpvarListMap<'tcx>,
 }
 
 impl<'tcx> TypeckTables<'tcx> {
@@ -441,6 +447,7 @@ pub fn empty(local_id_root: Option<DefId>) -> TypeckTables<'tcx> {
             tainted_by_errors: false,
             free_region_map: Default::default(),
             concrete_existential_types: Default::default(),
+            upvar_list: Default::default(),
         }
     }
 
@@ -741,6 +748,8 @@ fn hash_stable<W: StableHasherResult>(&self,
             tainted_by_errors,
             ref free_region_map,
             ref concrete_existential_types,
+            ref upvar_list,
+
         } = *self;
 
         hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
@@ -783,6 +792,7 @@ fn hash_stable<W: StableHasherResult>(&self,
             tainted_by_errors.hash_stable(hcx, hasher);
             free_region_map.hash_stable(hcx, hasher);
             concrete_existential_types.hash_stable(hcx, hasher);
+            upvar_list.hash_stable(hcx, hasher);
         })
     }
 }
index a2c96e7cf9f662d1897c7010002a878023c5cdca..f770f2bf7b1255fde5888ec76c16b30b5d561c79 100644 (file)
@@ -589,6 +589,8 @@ fn hash_stable<W: StableHasherResult>(&self,
 
 pub type Ty<'tcx> = &'tcx TyS<'tcx>;
 
+pub type UpvarListMap<'tcx> = FxHashMap<DefId, Vec<UpvarId>>;
+
 impl<'tcx> serialize::UseSpecializedEncodable for Ty<'tcx> {}
 impl<'tcx> serialize::UseSpecializedDecodable for Ty<'tcx> {}
 
index 86165d50b27e4366887070a27efd31ee3c35755a..c37e0e262cf5f357f515f1f2f53c816604c34f2d 100644 (file)
@@ -122,14 +122,16 @@ fn analyze_closure(
         };
 
         self.tcx.with_freevars(closure_node_id, |freevars| {
+            let mut freevar_list: Vec<ty::UpvarId> = Vec::new();
             for freevar in freevars {
                 let upvar_id = ty::UpvarId {
                     var_path: ty::UpvarPath {
-                        hir_id : self.tcx.hir().node_to_hir_id(freevar.var_id()),
+                        hir_id: self.tcx.hir().node_to_hir_id(freevar.var_id()),
                     },
                     closure_expr_id: LocalDefId::from_def_id(closure_def_id),
                 };
                 debug!("seed upvar_id {:?}", upvar_id);
+                freevar_list.push(upvar_id);
 
                 let capture_kind = match capture_clause {
                     hir::CaptureByValue => ty::UpvarCapture::ByValue,
@@ -149,6 +151,10 @@ fn analyze_closure(
                     .upvar_capture_map
                     .insert(upvar_id, capture_kind);
             }
+            self.tables
+                .borrow_mut()
+                .upvar_list
+                .insert(closure_def_id, freevar_list);
         });
 
         let body_owner_def_id = self.tcx.hir().body_owner_def_id(body.id());
@@ -166,7 +172,8 @@ fn analyze_closure(
             self.param_env,
             region_scope_tree,
             &self.tables.borrow(),
-        ).consume_body(body);
+        )
+        .consume_body(body);
 
         if let Some(closure_substs) = infer_kind {
             // Unify the (as yet unbound) type variable in the closure
@@ -240,9 +247,7 @@ fn final_upvar_tys(&self, closure_id: ast::NodeId) -> Vec<Ty<'tcx>> {
                     let var_hir_id = tcx.hir().node_to_hir_id(var_node_id);
                     let freevar_ty = self.node_ty(var_hir_id);
                     let upvar_id = ty::UpvarId {
-                        var_path: ty::UpvarPath {
-                            hir_id: var_hir_id,
-                        },
+                        var_path: ty::UpvarPath { hir_id: var_hir_id },
                         closure_expr_id: LocalDefId::from_def_id(closure_def_index),
                     };
                     let capture = self.tables.borrow().upvar_capture(upvar_id);
@@ -262,7 +267,8 @@ fn final_upvar_tys(&self, closure_id: ast::NodeId) -> Vec<Ty<'tcx>> {
                             },
                         ),
                     }
-                }).collect()
+                })
+                .collect()
         })
     }
 }