Skip to content

Commit b13d040

Browse files
committed
rustc: remove closure ID from Res::Upvar.
1 parent 1768030 commit b13d040

File tree

5 files changed

+30
-26
lines changed

5 files changed

+30
-26
lines changed

src/librustc/hir/def.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ pub enum Res<Id = hir::HirId> {
139139
// Value namespace
140140
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
141141
Local(Id),
142-
Upvar(Id, // `HirId` of closed over local
143-
ast::NodeId), // expr node that creates the closure
142+
Upvar(Id),
144143

145144
// Macro namespace
146145
NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
@@ -396,7 +395,7 @@ impl<Id> Res<Id> {
396395
Res::SelfCtor(id) => Res::SelfCtor(id),
397396
Res::PrimTy(id) => Res::PrimTy(id),
398397
Res::Local(id) => Res::Local(map(id)),
399-
Res::Upvar(id, closure) => Res::Upvar(map(id), closure),
398+
Res::Upvar(id) => Res::Upvar(map(id)),
400399
Res::SelfTy(a, b) => Res::SelfTy(a, b),
401400
Res::ToolMod => Res::ToolMod,
402401
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),

src/librustc/middle/expr_use_visitor.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -971,15 +971,13 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
971971
-> mc::McResult<mc::cmt_<'tcx>> {
972972
// Create the cmt for the variable being borrowed, from the
973973
// caller's perspective
974-
if upvar.has_parent {
975-
let closure_def_id = self.tcx().hir().local_def_id_from_hir_id(closure_hir_id);
976-
assert_eq!(self.mc.body_owner, self.tcx().parent(closure_def_id).unwrap());
977-
let var_nid = self.tcx().hir().hir_to_node_id(var_id);
978-
self.mc.cat_upvar(closure_hir_id, closure_span, var_nid)
974+
let res = if upvar.has_parent {
975+
Res::Upvar(var_id)
979976
} else {
980-
let var_ty = self.mc.node_ty(var_id)?;
981-
self.mc.cat_res(closure_hir_id, closure_span, var_ty, Res::Local(var_id))
982-
}
977+
Res::Local(var_id)
978+
};
979+
let var_ty = self.mc.node_ty(var_id)?;
980+
self.mc.cat_res(closure_hir_id, closure_span, var_ty, res)
983981
}
984982
}
985983

src/librustc/middle/mem_categorization.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ use syntax_pos::Span;
7878
use std::borrow::Cow;
7979
use std::fmt;
8080
use std::hash::{Hash, Hasher};
81+
use rustc_data_structures::fx::FxIndexMap;
8182
use rustc_data_structures::indexed_vec::Idx;
8283
use std::rc::Rc;
8384
use crate::util::nodemap::ItemLocalSet;
@@ -289,6 +290,7 @@ impl HirNode for hir::Pat {
289290
pub struct MemCategorizationContext<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
290291
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
291292
pub body_owner: DefId,
293+
pub upvars: Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>>,
292294
pub region_scope_tree: &'a region::ScopeTree,
293295
pub tables: &'a ty::TypeckTables<'tcx>,
294296
rvalue_promotable_map: Option<&'tcx ItemLocalSet>,
@@ -407,6 +409,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx, 'tcx> {
407409
MemCategorizationContext {
408410
tcx,
409411
body_owner,
412+
upvars: tcx.upvars(body_owner),
410413
region_scope_tree,
411414
tables,
412415
rvalue_promotable_map,
@@ -441,6 +444,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
441444
MemCategorizationContext {
442445
tcx,
443446
body_owner,
447+
upvars: tcx.upvars(body_owner),
444448
region_scope_tree,
445449
tables,
446450
rvalue_promotable_map,
@@ -742,21 +746,20 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
742746
})
743747
}
744748

745-
Res::Upvar(var_id, closure_node_id) => {
749+
Res::Upvar(var_id) => {
750+
assert!(self.upvars.map_or(false, |upvars| upvars.contains_key(&var_id)));
746751
let var_nid = self.tcx.hir().hir_to_node_id(var_id);
747-
let closure_def_id = self.tcx.hir().local_def_id(closure_node_id);
748-
assert_eq!(self.body_owner, closure_def_id);
749-
750752
self.cat_upvar(hir_id, span, var_nid)
751753
}
752754

753-
Res::Local(vid) => {
754-
let vnid = self.tcx.hir().hir_to_node_id(vid);
755+
Res::Local(var_id) => {
756+
assert!(!self.upvars.map_or(false, |upvars| upvars.contains_key(&var_id)));
757+
let var_nid = self.tcx.hir().hir_to_node_id(var_id);
755758
Ok(cmt_ {
756759
hir_id,
757760
span,
758-
cat: Categorization::Local(vid),
759-
mutbl: MutabilityCategory::from_local(self.tcx, self.tables, vnid),
761+
cat: Categorization::Local(var_id),
762+
mutbl: MutabilityCategory::from_local(self.tcx, self.tables, var_nid),
760763
ty: expr_ty,
761764
note: NoteNone
762765
})
@@ -768,7 +771,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
768771

769772
// Categorize an upvar, complete with invisible derefs of closure
770773
// environment and upvar reference as appropriate.
771-
pub fn cat_upvar(
774+
fn cat_upvar(
772775
&self,
773776
hir_id: hir::HirId,
774777
span: Span,

src/librustc_mir/hair/cx/expr.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -960,11 +960,15 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
960960

961961
Res::Def(DefKind::Static, id) => ExprKind::StaticRef { id },
962962

963-
Res::Local(var_hir_id) => convert_var(cx, expr, var_hir_id),
964-
Res::Upvar(var_hir_id, closure_node_id) => {
965-
let closure_def_id = cx.tcx.hir().local_def_id(closure_node_id);
966-
assert_eq!(cx.body_owner, closure_def_id);
967-
assert!(cx.tables().upvar_list[&cx.body_owner].contains_key(&var_hir_id));
963+
Res::Local(var_hir_id) => {
964+
assert!(!cx.tables().upvar_list.get(&cx.body_owner)
965+
.map_or(false, |upvars| upvars.contains_key(&var_hir_id)));
966+
967+
convert_var(cx, expr, var_hir_id)
968+
}
969+
Res::Upvar(var_hir_id) => {
970+
assert!(cx.tables().upvar_list.get(&cx.body_owner)
971+
.map_or(false, |upvars| upvars.contains_key(&var_hir_id)));
968972

969973
convert_var(cx, expr, var_hir_id)
970974
}

src/librustc_resolve/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4053,7 +4053,7 @@ impl<'a> Resolver<'a> {
40534053
Res::Upvar(..) => true,
40544054
_ => false,
40554055
};
4056-
res = Res::Upvar(var_id, function_id);
4056+
res = Res::Upvar(var_id);
40574057

40584058
match self.upvars.entry(function_id).or_default().entry(var_id) {
40594059
indexmap::map::Entry::Occupied(_) => continue,

0 commit comments

Comments
 (0)