about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEduard-Mihai Burtescu <edy.burt@gmail.com>2019-05-04 05:06:52 +0300
committerEduard-Mihai Burtescu <edy.burt@gmail.com>2019-06-01 19:01:09 +0300
commit26e61dd8265d5680f7ac40f34b0bd1c9c28baaf0 (patch)
treef00f2d7e0c95c367552a58ab1fe7b1df7a787094
parentcd3f21bc7dc3d2d1a44367618940701ab308f7d2 (diff)
downloadrust-26e61dd8265d5680f7ac40f34b0bd1c9c28baaf0.tar.gz
rust-26e61dd8265d5680f7ac40f34b0bd1c9c28baaf0.zip
rustc: replace Res in hir::Upvar with only Local/Upvar data.
-rw-r--r--src/librustc/hir/mod.rs18
-rw-r--r--src/librustc/middle/expr_use_visitor.rs12
-rw-r--r--src/librustc/middle/liveness.rs4
-rw-r--r--src/librustc/mir/mod.rs4
-rw-r--r--src/librustc/ty/print/pretty.rs4
-rw-r--r--src/librustc_mir/borrow_check/error_reporting.rs2
-rw-r--r--src/librustc_mir/hair/cx/expr.rs11
-rw-r--r--src/librustc_resolve/lib.rs18
-rw-r--r--src/librustc_typeck/check/upvar.rs9
9 files changed, 44 insertions, 38 deletions
diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs
index f03a8ddc908..bdae04ae478 100644
--- a/src/librustc/hir/mod.rs
+++ b/src/librustc/hir/mod.rs
@@ -2495,26 +2495,24 @@ impl ForeignItemKind {
 #[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
 pub struct Upvar<Id = HirId> {
     /// The variable being captured.
-    pub res: Res<Id>,
+    pub var_id: Id,
+
+    /// The parent closure, if this is not a direct capture,
+    /// and the index within that closure's capture list.
+    pub parent: Option<(ast::NodeId, usize)>,
 
     // First span where it is accessed (there can be multiple).
     pub span: Span
 }
 
 impl<Id: fmt::Debug + Copy> Upvar<Id> {
-    pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Upvar<R> {
+    pub fn map_id<R>(self, map: impl FnOnce(Id) -> R) -> Upvar<R> {
         Upvar {
-            res: self.res.map_id(map),
+            var_id: map(self.var_id),
+            parent: self.parent,
             span: self.span,
         }
     }
-
-    pub fn var_id(&self) -> Id {
-        match self.res {
-            Res::Local(id) | Res::Upvar(id, ..) => id,
-            _ => bug!("Upvar::var_id: bad res ({:?})", self.res)
-        }
-    }
 }
 
 pub type UpvarMap = NodeMap<Vec<Upvar<ast::NodeId>>>;
diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs
index 35b6b76a395..fd9233acbae 100644
--- a/src/librustc/middle/expr_use_visitor.rs
+++ b/src/librustc/middle/expr_use_visitor.rs
@@ -925,9 +925,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
         let closure_def_id = self.tcx().hir().local_def_id_from_hir_id(closure_expr.hir_id);
         if let Some(upvars) = self.tcx().upvars(closure_def_id) {
             for upvar in upvars.iter() {
-                let var_hir_id = upvar.var_id();
                 let upvar_id = ty::UpvarId {
-                    var_path: ty::UpvarPath { hir_id: var_hir_id },
+                    var_path: ty::UpvarPath { hir_id: upvar.var_id },
                     closure_expr_id: closure_def_id.to_local(),
                 };
                 let upvar_capture = self.mc.tables.upvar_capture(upvar_id);
@@ -962,9 +961,12 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
                         -> mc::McResult<mc::cmt_<'tcx>> {
         // Create the cmt for the variable being borrowed, from the
         // caller's perspective
-        let var_hir_id = upvar.var_id();
-        let var_ty = self.mc.node_ty(var_hir_id)?;
-        self.mc.cat_res(closure_hir_id, closure_span, var_ty, upvar.res)
+        let var_ty = self.mc.node_ty(upvar.var_id)?;
+        let res = upvar.parent.map_or(
+            Res::Local(upvar.var_id),
+            |(closure_node_id, i)| Res::Upvar(upvar.var_id, i, closure_node_id),
+        );
+        self.mc.cat_res(closure_hir_id, closure_span, var_ty, res)
     }
 }
 
diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs
index 91a19852c6c..e19d713d561 100644
--- a/src/librustc/middle/liveness.rs
+++ b/src/librustc/middle/liveness.rs
@@ -486,9 +486,9 @@ fn visit_expr<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, expr: &'tcx Expr) {
         let closure_def_id = ir.tcx.hir().local_def_id_from_hir_id(expr.hir_id);
         if let Some(upvars) = ir.tcx.upvars(closure_def_id) {
             call_caps.extend(upvars.iter().filter_map(|upvar| {
-                if let Res::Local(rv) = upvar.res {
+                if upvar.parent.is_none() {
                     let upvar_ln = ir.add_live_node(UpvarNode(upvar.span));
-                    Some(CaptureInfo { ln: upvar_ln, var_hid: rv })
+                    Some(CaptureInfo { ln: upvar_ln, var_hid: upvar.var_id })
                 } else {
                     None
                 }
diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs
index d4ef134728e..1f29b370d27 100644
--- a/src/librustc/mir/mod.rs
+++ b/src/librustc/mir/mod.rs
@@ -2563,7 +2563,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
 
                             if let Some(upvars) = tcx.upvars(def_id) {
                                 for (upvar, place) in upvars.iter().zip(places) {
-                                    let var_name = tcx.hir().name_by_hir_id(upvar.var_id());
+                                    let var_name = tcx.hir().name_by_hir_id(upvar.var_id);
                                     struct_fmt.field(&var_name.as_str(), place);
                                 }
                             }
@@ -2582,7 +2582,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
 
                             if let Some(upvars) = tcx.upvars(def_id) {
                                 for (upvar, place) in upvars.iter().zip(places) {
-                                    let var_name = tcx.hir().name_by_hir_id(upvar.var_id());
+                                    let var_name = tcx.hir().name_by_hir_id(upvar.var_id);
                                     struct_fmt.field(&var_name.as_str(), place);
                                 }
                             }
diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs
index 300ea9bb497..03c7226c1af 100644
--- a/src/librustc/ty/print/pretty.rs
+++ b/src/librustc/ty/print/pretty.rs
@@ -594,7 +594,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
                         p!(
                             write("{}{}:",
                                     sep,
-                                    self.tcx().hir().name_by_hir_id(upvar.var_id())),
+                                    self.tcx().hir().name_by_hir_id(upvar.var_id)),
                             print(upvar_ty));
                         sep = ", ";
                     }
@@ -637,7 +637,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
                         p!(
                             write("{}{}:",
                                     sep,
-                                    self.tcx().hir().name_by_hir_id(upvar.var_id())),
+                                    self.tcx().hir().name_by_hir_id(upvar.var_id)),
                             print(upvar_ty));
                         sep = ", ";
                     }
diff --git a/src/librustc_mir/borrow_check/error_reporting.rs b/src/librustc_mir/borrow_check/error_reporting.rs
index 5a22c81a5d0..74601cc3891 100644
--- a/src/librustc_mir/borrow_check/error_reporting.rs
+++ b/src/librustc_mir/borrow_check/error_reporting.rs
@@ -350,7 +350,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
                     // be borrowck'ing it, so we can just unwrap:
                     let upvar = self.infcx.tcx.upvars(def_id).unwrap()[field.index()];
 
-                    self.infcx.tcx.hir().name_by_hir_id(upvar.var_id()).to_string()
+                    self.infcx.tcx.hir().name_by_hir_id(upvar.var_id).to_string()
                 }
                 _ => {
                     // Might need a revision when the fields in trait RFC is implemented
diff --git a/src/librustc_mir/hair/cx/expr.rs b/src/librustc_mir/hair/cx/expr.rs
index d0ea303a93c..354aec442ed 100644
--- a/src/librustc_mir/hair/cx/expr.rs
+++ b/src/librustc_mir/hair/cx/expr.rs
@@ -1181,19 +1181,22 @@ fn capture_upvar<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
                                    upvar: &hir::Upvar,
                                    upvar_ty: Ty<'tcx>)
                                    -> ExprRef<'tcx> {
-    let var_hir_id = upvar.var_id();
     let upvar_id = ty::UpvarId {
-        var_path: ty::UpvarPath { hir_id: var_hir_id },
+        var_path: ty::UpvarPath { hir_id: upvar.var_id },
         closure_expr_id: cx.tcx.hir().local_def_id_from_hir_id(closure_expr.hir_id).to_local(),
     };
     let upvar_capture = cx.tables().upvar_capture(upvar_id);
     let temp_lifetime = cx.region_scope_tree.temporary_scope(closure_expr.hir_id.local_id);
-    let var_ty = cx.tables().node_type(var_hir_id);
+    let var_ty = cx.tables().node_type(upvar.var_id);
+    let upvar_res = upvar.parent.map_or(
+        Res::Local(upvar.var_id),
+        |(closure_node_id, i)| Res::Upvar(upvar.var_id, i, closure_node_id),
+    );
     let captured_var = Expr {
         temp_lifetime,
         ty: var_ty,
         span: closure_expr.span,
-        kind: convert_var(cx, closure_expr, upvar.res),
+        kind: convert_var(cx, closure_expr, upvar_res),
     };
     match upvar_capture {
         ty::UpvarCapture::ByValue => captured_var.to_ref(),
diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs
index 59e5fc149fc..67ed6a77305 100644
--- a/src/librustc_resolve/lib.rs
+++ b/src/librustc_resolve/lib.rs
@@ -4040,7 +4040,7 @@ impl<'a> Resolver<'a> {
             Res::Upvar(..) => {
                 span_bug!(span, "unexpected {:?} in bindings", res)
             }
-            Res::Local(node_id) => {
+            Res::Local(var_id) => {
                 use ResolutionError::*;
                 let mut res_err = None;
 
@@ -4051,27 +4051,31 @@ impl<'a> Resolver<'a> {
                             // Nothing to do. Continue.
                         }
                         ClosureRibKind(function_id) => {
-                            let prev_res = res;
+                            let parent = match res {
+                                Res::Upvar(_, i, closure) => Some((closure, i)),
+                                _ => None,
+                            };
 
                             let seen = self.upvars_seen
                                            .entry(function_id)
                                            .or_default();
-                            if let Some(&index) = seen.get(&node_id) {
-                                res = Res::Upvar(node_id, index, function_id);
+                            if let Some(&index) = seen.get(&var_id) {
+                                res = Res::Upvar(var_id, index, function_id);
                                 continue;
                             }
                             let vec = self.upvars
                                           .entry(function_id)
                                           .or_default();
                             let depth = vec.len();
-                            res = Res::Upvar(node_id, depth, function_id);
+                            res = Res::Upvar(var_id, depth, function_id);
 
                             if record_used {
                                 vec.push(Upvar {
-                                    res: prev_res,
+                                    var_id,
+                                    parent,
                                     span,
                                 });
-                                seen.insert(node_id, depth);
+                                seen.insert(var_id, depth);
                             }
                         }
                         ItemRibKind | FnItemRibKind | AssocItemRibKind => {
diff --git a/src/librustc_typeck/check/upvar.rs b/src/librustc_typeck/check/upvar.rs
index c3861f964e4..a4f9ede37c9 100644
--- a/src/librustc_typeck/check/upvar.rs
+++ b/src/librustc_typeck/check/upvar.rs
@@ -126,7 +126,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
             for upvar in upvars.iter() {
                 let upvar_id = ty::UpvarId {
                     var_path: ty::UpvarPath {
-                        hir_id: upvar.var_id(),
+                        hir_id: upvar.var_id,
                     },
                     closure_expr_id: LocalDefId::from_def_id(closure_def_id),
                 };
@@ -250,17 +250,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
             upvars
                 .iter()
                 .map(|upvar| {
-                    let var_hir_id = upvar.var_id();
-                    let upvar_ty = self.node_ty(var_hir_id);
+                    let upvar_ty = self.node_ty(upvar.var_id);
                     let upvar_id = ty::UpvarId {
-                        var_path: ty::UpvarPath { hir_id: var_hir_id },
+                        var_path: ty::UpvarPath { hir_id: upvar.var_id },
                         closure_expr_id: LocalDefId::from_def_id(closure_def_id),
                     };
                     let capture = self.tables.borrow().upvar_capture(upvar_id);
 
                     debug!(
                         "var_id={:?} upvar_ty={:?} capture={:?}",
-                        var_hir_id, upvar_ty, capture
+                        upvar.var_id, upvar_ty, capture
                     );
 
                     match capture {