about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/librustc/middle/expr_use_visitor.rs6
-rw-r--r--src/librustc/middle/liveness.rs13
-rw-r--r--src/librustc/mir/mod.rs8
-rw-r--r--src/librustc/ty/mod.rs14
-rw-r--r--src/librustc/ty/print/pretty.rs4
-rw-r--r--src/librustc_mir/borrow_check/error_reporting.rs39
-rw-r--r--src/librustc_mir/hair/cx/expr.rs11
-rw-r--r--src/librustc_passes/rvalue_promotion.rs3
-rw-r--r--src/librustc_typeck/check/coercion.rs3
-rw-r--r--src/librustc_typeck/check/upvar.rs16
-rw-r--r--src/librustc_typeck/collect.rs6
11 files changed, 49 insertions, 74 deletions
diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs
index cf3f613b08e..9f24644d912 100644
--- a/src/librustc/middle/expr_use_visitor.rs
+++ b/src/librustc/middle/expr_use_visitor.rs
@@ -931,8 +931,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
         debug!("walk_captures({:?})", closure_expr);
 
         let closure_def_id = self.tcx().hir().local_def_id_from_hir_id(closure_expr.hir_id);
-        self.tcx().with_freevars(closure_expr.hir_id, |freevars| {
-            for freevar in freevars {
+        if let Some(freevars) = self.tcx().freevars(closure_def_id) {
+            for freevar in freevars.iter() {
                 let var_hir_id = freevar.var_id();
                 let upvar_id = ty::UpvarId {
                     var_path: ty::UpvarPath { hir_id: var_hir_id },
@@ -960,7 +960,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
                     }
                 }
             }
-        });
+        }
     }
 
     fn cat_captured_var(&mut self,
diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs
index 15736218a79..b1d60dd3a55 100644
--- a/src/librustc/middle/liveness.rs
+++ b/src/librustc/middle/liveness.rs
@@ -483,16 +483,17 @@ fn visit_expr<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, expr: &'tcx Expr) {
         // in better error messages than just pointing at the closure
         // construction site.
         let mut call_caps = Vec::new();
-        ir.tcx.with_freevars(expr.hir_id, |freevars| {
-            call_caps.extend(freevars.iter().filter_map(|fv| {
-                if let Res::Local(rv) = fv.res {
-                    let fv_ln = ir.add_live_node(FreeVarNode(fv.span));
-                    Some(CaptureInfo { ln: fv_ln, var_hid: rv })
+        let closure_def_id = ir.tcx.hir().local_def_id_from_hir_id(expr.hir_id);
+        if let Some(freevars) = ir.tcx.freevars(closure_def_id) {
+            call_caps.extend(freevars.iter().filter_map(|freevar| {
+                if let Res::Local(rv) = freevar.res {
+                    let freevar_ln = ir.add_live_node(FreeVarNode(freevar.span));
+                    Some(CaptureInfo { ln: freevar_ln, var_hid: rv })
                 } else {
                     None
                 }
             }));
-        });
+        }
         ir.set_captures(expr.hir_id, call_caps);
 
         intravisit::walk_expr(ir, expr);
diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs
index 09e2b523fae..3b60d0af736 100644
--- a/src/librustc/mir/mod.rs
+++ b/src/librustc/mir/mod.rs
@@ -2572,12 +2572,12 @@ impl<'tcx> Debug for Rvalue<'tcx> {
                             };
                             let mut struct_fmt = fmt.debug_struct(&name);
 
-                            tcx.with_freevars(hir_id, |freevars| {
+                            if let Some(freevars) = tcx.freevars(def_id) {
                                 for (freevar, place) in freevars.iter().zip(places) {
                                     let var_name = tcx.hir().name_by_hir_id(freevar.var_id());
                                     struct_fmt.field(&var_name.as_str(), place);
                                 }
-                            });
+                            }
 
                             struct_fmt.finish()
                         } else {
@@ -2591,12 +2591,12 @@ impl<'tcx> Debug for Rvalue<'tcx> {
                                                tcx.hir().span_by_hir_id(hir_id));
                             let mut struct_fmt = fmt.debug_struct(&name);
 
-                            tcx.with_freevars(hir_id, |freevars| {
+                            if let Some(freevars) = tcx.freevars(def_id) {
                                 for (freevar, place) in freevars.iter().zip(places) {
                                     let var_name = tcx.hir().name_by_hir_id(freevar.var_id());
                                     struct_fmt.field(&var_name.as_str(), place);
                                 }
-                            });
+                            }
 
                             struct_fmt.finish()
                         } else {
diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs
index feedf5741f6..9d3c4b5fba5 100644
--- a/src/librustc/ty/mod.rs
+++ b/src/librustc/ty/mod.rs
@@ -9,7 +9,7 @@ pub use self::IntVarValue::*;
 pub use self::fold::TypeFoldable;
 
 use crate::hir::{map as hir_map, FreevarMap, GlobMap, TraitMap};
-use crate::hir::{HirId, Node};
+use crate::hir::Node;
 use crate::hir::def::{Res, DefKind, CtorOf, CtorKind, ExportMap};
 use crate::hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
 use rustc_data_structures::svh::Svh;
@@ -3120,18 +3120,6 @@ impl Iterator for AssociatedItemsIterator<'_, '_, '_> {
     }
 }
 
-impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
-    pub fn with_freevars<T, F>(self, fid: HirId, f: F) -> T where
-        F: FnOnce(&[hir::Freevar]) -> T,
-    {
-        let def_id = self.hir().local_def_id_from_hir_id(fid);
-        match self.freevars(def_id) {
-            None => f(&[]),
-            Some(d) => f(&d),
-        }
-    }
-}
-
 fn associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> AssociatedItem {
     let id = tcx.hir().as_local_hir_id(def_id).unwrap();
     let parent_id = tcx.hir().get_parent_item(id);
diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs
index e09dcd16bd3..f131f193791 100644
--- a/src/librustc/ty/print/pretty.rs
+++ b/src/librustc/ty/print/pretty.rs
@@ -584,7 +584,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
                     let mut sep = " ";
                     for (freevar, upvar_ty) in self.tcx().freevars(did)
                         .as_ref()
-                        .map_or(&[][..], |fv| &fv[..])
+                        .map_or(&[][..], |v| &v[..])
                         .iter()
                         .zip(upvar_tys)
                     {
@@ -627,7 +627,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
                     let mut sep = " ";
                     for (freevar, upvar_ty) in self.tcx().freevars(did)
                         .as_ref()
-                        .map_or(&[][..], |fv| &fv[..])
+                        .map_or(&[][..], |v| &v[..])
                         .iter()
                         .zip(upvar_tys)
                     {
diff --git a/src/librustc_mir/borrow_check/error_reporting.rs b/src/librustc_mir/borrow_check/error_reporting.rs
index ed42326d7d5..64b32114ebc 100644
--- a/src/librustc_mir/borrow_check/error_reporting.rs
+++ b/src/librustc_mir/borrow_check/error_reporting.rs
@@ -1814,14 +1814,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
                 ty::Array(ty, _) | ty::Slice(ty) =>
                     self.describe_field_from_ty(&ty, field, variant_index),
                 ty::Closure(def_id, _) | ty::Generator(def_id, _, _) => {
-                    // Convert the def-id into a node-id. node-ids are only valid for
-                    // the local code in the current crate, so this returns an `Option` in case
+                    // `tcx.freevars(def_id)` returns an `Option`, which is `None` in case
                     // the closure comes from another crate. But in that case we wouldn't
                     // be borrowck'ing it, so we can just unwrap:
-                    let hir_id = self.infcx.tcx.hir().as_local_hir_id(def_id).unwrap();
-                    let freevar = self.infcx
-                        .tcx
-                        .with_freevars(hir_id, |fv| fv[field.index()]);
+                    let freevar = self.infcx.tcx.freevars(def_id).unwrap()[field.index()];
 
                     self.infcx.tcx.hir().name_by_hir_id(freevar.var_id()).to_string()
                 }
@@ -2613,28 +2609,19 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
         if let hir::ExprKind::Closure(
             .., args_span, _
         ) = expr {
-            let var_span = self.infcx.tcx.with_freevars(
-                hir_id,
-                |freevars| {
-                    for (v, place) in freevars.iter().zip(places) {
-                        match place {
-                            Operand::Copy(place) |
-                            Operand::Move(place) if target_place == place => {
-                                debug!("closure_span: found captured local {:?}", place);
-                                return Some(v.span);
-                            },
-                            _ => {}
-                        }
-                    }
-
-                    None
-                },
-            )?;
+            for (v, place) in self.infcx.tcx.freevars(def_id)?.iter().zip(places) {
+                match place {
+                    Operand::Copy(place) |
+                    Operand::Move(place) if target_place == place => {
+                        debug!("closure_span: found captured local {:?}", place);
+                        return Some((*args_span, v.span));
+                    },
+                    _ => {}
+                }
+            }
 
-            Some((*args_span, var_span))
-        } else {
-            None
         }
+        None
     }
 
     /// Helper to retrieve span(s) of given borrow from the current MIR
diff --git a/src/librustc_mir/hair/cx/expr.rs b/src/librustc_mir/hair/cx/expr.rs
index 5ac1ccd8fad..07557421779 100644
--- a/src/librustc_mir/hair/cx/expr.rs
+++ b/src/librustc_mir/hair/cx/expr.rs
@@ -516,12 +516,11 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
                     span_bug!(expr.span, "closure expr w/o closure type: {:?}", closure_ty);
                 }
             };
-            let upvars = cx.tcx.with_freevars(expr.hir_id, |freevars| {
-                freevars.iter()
-                    .zip(substs.upvar_tys(def_id, cx.tcx))
-                    .map(|(fv, ty)| capture_freevar(cx, expr, fv, ty))
-                    .collect()
-            });
+            let upvars = cx.tcx.freevars(def_id).iter()
+                .flat_map(|freevars| freevars.iter())
+                .zip(substs.upvar_tys(def_id, cx.tcx))
+                .map(|(freevar, ty)| capture_freevar(cx, expr, freevar, ty))
+                .collect();
             ExprKind::Closure {
                 closure_id: def_id,
                 substs,
diff --git a/src/librustc_passes/rvalue_promotion.rs b/src/librustc_passes/rvalue_promotion.rs
index b6e2aacd559..642144c3243 100644
--- a/src/librustc_passes/rvalue_promotion.rs
+++ b/src/librustc_passes/rvalue_promotion.rs
@@ -449,7 +449,8 @@ fn check_expr_kind<'a, 'tcx>(
             let nested_body_promotable = v.check_nested_body(body_id);
             // Paths in constant contexts cannot refer to local variables,
             // as there are none, and thus closures can't have upvars there.
-            if v.tcx.with_freevars(e.hir_id, |fv| !fv.is_empty()) {
+            let closure_def_id = v.tcx.hir().local_def_id_from_hir_id(e.hir_id);
+            if !v.tcx.freevars(closure_def_id).map_or(true, |v| v.is_empty()) {
                 NotPromotable
             } else {
                 nested_body_promotable
diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs
index 85eb0f9d499..9f3a9de92cc 100644
--- a/src/librustc_typeck/check/coercion.rs
+++ b/src/librustc_typeck/check/coercion.rs
@@ -721,9 +721,8 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
 
         let b = self.shallow_resolve(b);
 
-        let hir_id_a = self.tcx.hir().as_local_hir_id(def_id_a).unwrap();
         match b.sty {
-            ty::FnPtr(fn_ty) if self.tcx.with_freevars(hir_id_a, |v| v.is_empty()) => {
+            ty::FnPtr(fn_ty) if self.tcx.freevars(def_id_a).map_or(true, |v| v.is_empty()) => {
                 // We coerce the closure, which has fn type
                 //     `extern "rust-call" fn((arg0,arg1,...)) -> _`
                 // to
diff --git a/src/librustc_typeck/check/upvar.rs b/src/librustc_typeck/check/upvar.rs
index dc66c6c93d0..9cdfcad3721 100644
--- a/src/librustc_typeck/check/upvar.rs
+++ b/src/librustc_typeck/check/upvar.rs
@@ -121,9 +121,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
             None
         };
 
-        self.tcx.with_freevars(closure_hir_id, |freevars| {
+        if let Some(freevars) = self.tcx.freevars(closure_def_id) {
             let mut freevar_list: Vec<ty::UpvarId> = Vec::with_capacity(freevars.len());
-            for freevar in freevars {
+            for freevar in freevars.iter() {
                 let upvar_id = ty::UpvarId {
                     var_path: ty::UpvarPath {
                         hir_id: freevar.var_id(),
@@ -155,14 +155,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
             }
             // Add the vector of freevars to the map keyed with the closure id.
             // This gives us an easier access to them without having to call
-            // with_freevars again..
+            // tcx.freevars again..
             if !freevar_list.is_empty() {
                 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());
         let region_scope_tree = &self.tcx.region_scope_tree(body_owner_def_id);
@@ -244,9 +244,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
         // This may change if abstract return types of some sort are
         // implemented.
         let tcx = self.tcx;
-        let closure_def_index = tcx.hir().local_def_id_from_hir_id(closure_id);
+        let closure_def_id = tcx.hir().local_def_id_from_hir_id(closure_id);
 
-        tcx.with_freevars(closure_id, |freevars| {
+        tcx.freevars(closure_def_id).iter().flat_map(|freevars| {
             freevars
                 .iter()
                 .map(|freevar| {
@@ -254,7 +254,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
                     let freevar_ty = self.node_ty(var_hir_id);
                     let upvar_id = ty::UpvarId {
                         var_path: ty::UpvarPath { hir_id: var_hir_id },
-                        closure_expr_id: LocalDefId::from_def_id(closure_def_index),
+                        closure_expr_id: LocalDefId::from_def_id(closure_def_id),
                     };
                     let capture = self.tables.borrow().upvar_capture(upvar_id);
 
@@ -274,8 +274,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
                         ),
                     }
                 })
-                .collect()
         })
+            .collect()
     }
 }
 
diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs
index ed8ac89912c..a40012a4a79 100644
--- a/src/librustc_typeck/collect.rs
+++ b/src/librustc_typeck/collect.rs
@@ -1093,8 +1093,8 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx ty
                 }),
         );
 
-        tcx.with_freevars(hir_id, |fv| {
-            params.extend(fv.iter().zip((dummy_args.len() as u32)..).map(|(_, i)| {
+        if let Some(freevars) = tcx.freevars(def_id) {
+            params.extend(freevars.iter().zip((dummy_args.len() as u32)..).map(|(_, i)| {
                 ty::GenericParamDef {
                     index: type_start + i,
                     name: Symbol::intern("<upvar>").as_interned_str(),
@@ -1107,7 +1107,7 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx ty
                     },
                 }
             }));
-        });
+        }
     }
 
     let param_def_id_to_index = params