Skip to content
Permalink
Browse files

rustc: replace Res in hir::Upvar with only Local/Upvar data.

  • Loading branch information...
eddyb committed May 4, 2019
1 parent cd3f21b commit 26e61dd8265d5680f7ac40f34b0bd1c9c28baaf0
@@ -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>>>;
@@ -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)
}
}

@@ -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
}
@@ -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);
}
}
@@ -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 = ", ";
}
@@ -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
@@ -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(),
@@ -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 => {
@@ -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 {

0 comments on commit 26e61dd

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