1
Fork 0

rustc: remove closure ID from Res::Upvar.

This commit is contained in:
Eduard-Mihai Burtescu 2019-05-20 21:51:55 +03:00
parent 1768030791
commit b13d0407d4
5 changed files with 30 additions and 26 deletions

View file

@ -139,8 +139,7 @@ pub enum Res<Id = hir::HirId> {
// Value namespace // Value namespace
SelfCtor(DefId /* impl */), // `DefId` refers to the impl SelfCtor(DefId /* impl */), // `DefId` refers to the impl
Local(Id), Local(Id),
Upvar(Id, // `HirId` of closed over local Upvar(Id),
ast::NodeId), // expr node that creates the closure
// Macro namespace // Macro namespace
NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]` NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
@ -396,7 +395,7 @@ impl<Id> Res<Id> {
Res::SelfCtor(id) => Res::SelfCtor(id), Res::SelfCtor(id) => Res::SelfCtor(id),
Res::PrimTy(id) => Res::PrimTy(id), Res::PrimTy(id) => Res::PrimTy(id),
Res::Local(id) => Res::Local(map(id)), Res::Local(id) => Res::Local(map(id)),
Res::Upvar(id, closure) => Res::Upvar(map(id), closure), Res::Upvar(id) => Res::Upvar(map(id)),
Res::SelfTy(a, b) => Res::SelfTy(a, b), Res::SelfTy(a, b) => Res::SelfTy(a, b),
Res::ToolMod => Res::ToolMod, Res::ToolMod => Res::ToolMod,
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind), Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),

View file

@ -971,15 +971,13 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
-> mc::McResult<mc::cmt_<'tcx>> { -> mc::McResult<mc::cmt_<'tcx>> {
// Create the cmt for the variable being borrowed, from the // Create the cmt for the variable being borrowed, from the
// caller's perspective // caller's perspective
if upvar.has_parent { let res = if upvar.has_parent {
let closure_def_id = self.tcx().hir().local_def_id_from_hir_id(closure_hir_id); Res::Upvar(var_id)
assert_eq!(self.mc.body_owner, self.tcx().parent(closure_def_id).unwrap());
let var_nid = self.tcx().hir().hir_to_node_id(var_id);
self.mc.cat_upvar(closure_hir_id, closure_span, var_nid)
} else { } else {
Res::Local(var_id)
};
let var_ty = self.mc.node_ty(var_id)?; let var_ty = self.mc.node_ty(var_id)?;
self.mc.cat_res(closure_hir_id, closure_span, var_ty, Res::Local(var_id)) self.mc.cat_res(closure_hir_id, closure_span, var_ty, res)
}
} }
} }

View file

@ -78,6 +78,7 @@ use syntax_pos::Span;
use std::borrow::Cow; use std::borrow::Cow;
use std::fmt; use std::fmt;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::indexed_vec::Idx; use rustc_data_structures::indexed_vec::Idx;
use std::rc::Rc; use std::rc::Rc;
use crate::util::nodemap::ItemLocalSet; use crate::util::nodemap::ItemLocalSet;
@ -289,6 +290,7 @@ impl HirNode for hir::Pat {
pub struct MemCategorizationContext<'a, 'gcx: 'a+'tcx, 'tcx: 'a> { pub struct MemCategorizationContext<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
pub tcx: TyCtxt<'a, 'gcx, 'tcx>, pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
pub body_owner: DefId, pub body_owner: DefId,
pub upvars: Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>>,
pub region_scope_tree: &'a region::ScopeTree, pub region_scope_tree: &'a region::ScopeTree,
pub tables: &'a ty::TypeckTables<'tcx>, pub tables: &'a ty::TypeckTables<'tcx>,
rvalue_promotable_map: Option<&'tcx ItemLocalSet>, rvalue_promotable_map: Option<&'tcx ItemLocalSet>,
@ -407,6 +409,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx, 'tcx> {
MemCategorizationContext { MemCategorizationContext {
tcx, tcx,
body_owner, body_owner,
upvars: tcx.upvars(body_owner),
region_scope_tree, region_scope_tree,
tables, tables,
rvalue_promotable_map, rvalue_promotable_map,
@ -441,6 +444,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
MemCategorizationContext { MemCategorizationContext {
tcx, tcx,
body_owner, body_owner,
upvars: tcx.upvars(body_owner),
region_scope_tree, region_scope_tree,
tables, tables,
rvalue_promotable_map, rvalue_promotable_map,
@ -742,21 +746,20 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
}) })
} }
Res::Upvar(var_id, closure_node_id) => { Res::Upvar(var_id) => {
assert!(self.upvars.map_or(false, |upvars| upvars.contains_key(&var_id)));
let var_nid = self.tcx.hir().hir_to_node_id(var_id); let var_nid = self.tcx.hir().hir_to_node_id(var_id);
let closure_def_id = self.tcx.hir().local_def_id(closure_node_id);
assert_eq!(self.body_owner, closure_def_id);
self.cat_upvar(hir_id, span, var_nid) self.cat_upvar(hir_id, span, var_nid)
} }
Res::Local(vid) => { Res::Local(var_id) => {
let vnid = self.tcx.hir().hir_to_node_id(vid); assert!(!self.upvars.map_or(false, |upvars| upvars.contains_key(&var_id)));
let var_nid = self.tcx.hir().hir_to_node_id(var_id);
Ok(cmt_ { Ok(cmt_ {
hir_id, hir_id,
span, span,
cat: Categorization::Local(vid), cat: Categorization::Local(var_id),
mutbl: MutabilityCategory::from_local(self.tcx, self.tables, vnid), mutbl: MutabilityCategory::from_local(self.tcx, self.tables, var_nid),
ty: expr_ty, ty: expr_ty,
note: NoteNone note: NoteNone
}) })
@ -768,7 +771,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
// Categorize an upvar, complete with invisible derefs of closure // Categorize an upvar, complete with invisible derefs of closure
// environment and upvar reference as appropriate. // environment and upvar reference as appropriate.
pub fn cat_upvar( fn cat_upvar(
&self, &self,
hir_id: hir::HirId, hir_id: hir::HirId,
span: Span, span: Span,

View file

@ -960,11 +960,15 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
Res::Def(DefKind::Static, id) => ExprKind::StaticRef { id }, Res::Def(DefKind::Static, id) => ExprKind::StaticRef { id },
Res::Local(var_hir_id) => convert_var(cx, expr, var_hir_id), Res::Local(var_hir_id) => {
Res::Upvar(var_hir_id, closure_node_id) => { assert!(!cx.tables().upvar_list.get(&cx.body_owner)
let closure_def_id = cx.tcx.hir().local_def_id(closure_node_id); .map_or(false, |upvars| upvars.contains_key(&var_hir_id)));
assert_eq!(cx.body_owner, closure_def_id);
assert!(cx.tables().upvar_list[&cx.body_owner].contains_key(&var_hir_id)); convert_var(cx, expr, var_hir_id)
}
Res::Upvar(var_hir_id) => {
assert!(cx.tables().upvar_list.get(&cx.body_owner)
.map_or(false, |upvars| upvars.contains_key(&var_hir_id)));
convert_var(cx, expr, var_hir_id) convert_var(cx, expr, var_hir_id)
} }

View file

@ -4053,7 +4053,7 @@ impl<'a> Resolver<'a> {
Res::Upvar(..) => true, Res::Upvar(..) => true,
_ => false, _ => false,
}; };
res = Res::Upvar(var_id, function_id); res = Res::Upvar(var_id);
match self.upvars.entry(function_id).or_default().entry(var_id) { match self.upvars.entry(function_id).or_default().entry(var_id) {
indexmap::map::Entry::Occupied(_) => continue, indexmap::map::Entry::Occupied(_) => continue,