Skip to content

Commit 4b9670a

Browse files
committed
rustc: remove the index field from Res::Upvar.
1 parent 26e61dd commit 4b9670a

File tree

7 files changed

+30
-21
lines changed

7 files changed

+30
-21
lines changed

src/librustc/hir/def.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ pub enum Res<Id = hir::HirId> {
140140
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
141141
Local(Id),
142142
Upvar(Id, // `HirId` of closed over local
143-
usize, // index in the `upvars` list of the closure
144143
ast::NodeId), // expr node that creates the closure
145144

146145
// Macro namespace
@@ -397,11 +396,7 @@ impl<Id> Res<Id> {
397396
Res::SelfCtor(id) => Res::SelfCtor(id),
398397
Res::PrimTy(id) => Res::PrimTy(id),
399398
Res::Local(id) => Res::Local(map(id)),
400-
Res::Upvar(id, index, closure) => Res::Upvar(
401-
map(id),
402-
index,
403-
closure
404-
),
399+
Res::Upvar(id, closure) => Res::Upvar(map(id), closure),
405400
Res::SelfTy(a, b) => Res::SelfTy(a, b),
406401
Res::ToolMod => Res::ToolMod,
407402
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),

src/librustc/hir/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,9 +2497,8 @@ pub struct Upvar<Id = HirId> {
24972497
/// The variable being captured.
24982498
pub var_id: Id,
24992499

2500-
/// The parent closure, if this is not a direct capture,
2501-
/// and the index within that closure's capture list.
2502-
pub parent: Option<(ast::NodeId, usize)>,
2500+
/// The parent closure, if this is not a direct capture.
2501+
pub parent: Option<ast::NodeId>,
25032502

25042503
// First span where it is accessed (there can be multiple).
25052504
pub span: Span

src/librustc/middle/expr_use_visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
964964
let var_ty = self.mc.node_ty(upvar.var_id)?;
965965
let res = upvar.parent.map_or(
966966
Res::Local(upvar.var_id),
967-
|(closure_node_id, i)| Res::Upvar(upvar.var_id, i, closure_node_id),
967+
|closure_node_id| Res::Upvar(upvar.var_id, closure_node_id),
968968
);
969969
self.mc.cat_res(closure_hir_id, closure_span, var_ty, res)
970970
}

src/librustc/middle/mem_categorization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
737737
})
738738
}
739739

740-
Res::Upvar(var_id, _, fn_node_id) => {
740+
Res::Upvar(var_id, fn_node_id) => {
741741
let var_nid = self.tcx.hir().hir_to_node_id(var_id);
742742
self.cat_upvar(hir_id, span, var_nid, fn_node_id)
743743
}

src/librustc_mir/hair/cx/expr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,9 @@ fn convert_var<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
975975
match res {
976976
Res::Local(id) => ExprKind::VarRef { id },
977977

978-
Res::Upvar(var_hir_id, index, closure_expr_id) => {
978+
Res::Upvar(var_hir_id, closure_expr_id) => {
979+
let index = cx.upvar_indices[&var_hir_id];
980+
979981
debug!("convert_var(upvar({:?}, {:?}, {:?}))",
980982
var_hir_id,
981983
index,
@@ -1190,7 +1192,7 @@ fn capture_upvar<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
11901192
let var_ty = cx.tables().node_type(upvar.var_id);
11911193
let upvar_res = upvar.parent.map_or(
11921194
Res::Local(upvar.var_id),
1193-
|(closure_node_id, i)| Res::Upvar(upvar.var_id, i, closure_node_id),
1195+
|closure_node_id| Res::Upvar(upvar.var_id, closure_node_id),
11941196
);
11951197
let captured_var = Expr {
11961198
temp_lifetime,

src/librustc_mir/hair/cx/mod.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use crate::hair::*;
66
use crate::hair::util::UserAnnotatedTyHelpers;
77

8+
use rustc_data_structures::fx::FxHashMap;
89
use rustc_data_structures::indexed_vec::Idx;
910
use rustc::hir::def_id::DefId;
1011
use rustc::hir::Node;
@@ -46,13 +47,17 @@ pub struct Cx<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
4647

4748
/// See field with the same name on `mir::Body`.
4849
control_flow_destroyed: Vec<(Span, String)>,
50+
51+
/// Reverse map, from upvar variable `HirId`s to their indices.
52+
upvar_indices: FxHashMap<hir::HirId, usize>,
4953
}
5054

5155
impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
5256
pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
5357
src_id: hir::HirId) -> Cx<'a, 'gcx, 'tcx> {
5458
let tcx = infcx.tcx;
5559
let src_def_id = tcx.hir().local_def_id_from_hir_id(src_id);
60+
let tables = tcx.typeck_tables_of(src_def_id);
5661
let body_owner_kind = tcx.hir().body_owner_kind_by_hir_id(src_id);
5762

5863
let constness = match body_owner_kind {
@@ -75,18 +80,27 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
7580
// Constants always need overflow checks.
7681
check_overflow |= constness == hir::Constness::Const;
7782

83+
// Compute reverse mapping, of uvpars to their indices.
84+
let mut upvar_indices = FxHashMap::default();
85+
if let Some(upvars) = tables.upvar_list.get(&src_def_id) {
86+
upvar_indices.extend(
87+
upvars.iter().enumerate().map(|(i, upvar_id)| (upvar_id.var_path.hir_id, i)),
88+
);
89+
}
90+
7891
Cx {
7992
tcx,
8093
infcx,
8194
root_lint_level: src_id,
8295
param_env: tcx.param_env(src_def_id),
8396
identity_substs: InternalSubsts::identity_for_item(tcx.global_tcx(), src_def_id),
8497
region_scope_tree: tcx.region_scope_tree(src_def_id),
85-
tables: tcx.typeck_tables_of(src_def_id),
98+
tables,
8699
constness,
87100
body_owner_kind,
88101
check_overflow,
89102
control_flow_destroyed: Vec::new(),
103+
upvar_indices,
90104
}
91105
}
92106

src/librustc_resolve/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,7 +1669,7 @@ pub struct Resolver<'a> {
16691669
label_res_map: NodeMap<NodeId>,
16701670

16711671
pub upvars: UpvarMap,
1672-
upvars_seen: NodeMap<NodeMap<usize>>,
1672+
upvars_seen: NodeMap<NodeSet>,
16731673
pub export_map: ExportMap<NodeId>,
16741674
pub trait_map: TraitMap,
16751675

@@ -4052,30 +4052,29 @@ impl<'a> Resolver<'a> {
40524052
}
40534053
ClosureRibKind(function_id) => {
40544054
let parent = match res {
4055-
Res::Upvar(_, i, closure) => Some((closure, i)),
4055+
Res::Upvar(_, closure) => Some(closure),
40564056
_ => None,
40574057
};
40584058

40594059
let seen = self.upvars_seen
40604060
.entry(function_id)
40614061
.or_default();
4062-
if let Some(&index) = seen.get(&var_id) {
4063-
res = Res::Upvar(var_id, index, function_id);
4062+
if seen.contains(&var_id) {
4063+
res = Res::Upvar(var_id, function_id);
40644064
continue;
40654065
}
40664066
let vec = self.upvars
40674067
.entry(function_id)
40684068
.or_default();
4069-
let depth = vec.len();
4070-
res = Res::Upvar(var_id, depth, function_id);
4069+
res = Res::Upvar(var_id, function_id);
40714070

40724071
if record_used {
40734072
vec.push(Upvar {
40744073
var_id,
40754074
parent,
40764075
span,
40774076
});
4078-
seen.insert(var_id, depth);
4077+
seen.insert(var_id);
40794078
}
40804079
}
40814080
ItemRibKind | FnItemRibKind | AssocItemRibKind => {

0 commit comments

Comments
 (0)