Skip to content
Permalink
Browse files

rustc: replace uses of with_freevars with the freevars query.

  • Loading branch information...
eddyb committed May 4, 2019
1 parent 5d8fd98 commit 125dc60dabfdc70a92a757cff9d7d93a5e5376a7
@@ -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,
@@ -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);
@@ -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 {
@@ -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);
@@ -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)
{
@@ -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
@@ -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,
@@ -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
@@ -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
@@ -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,17 +244,17 @@ 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| {
let var_hir_id = freevar.var_id();
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()
}
}

@@ -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

0 comments on commit 125dc60

Please sign in to comment.
You can’t perform that action at this time.