Skip to content

Commit 47db51e

Browse files
committed
Issue 56905
Adding a map to TypeckTables to get the list of all the Upvars given a closureID. This is help us get rid of the recurring pattern in the codebase of iterating over the free vars using with_freevars.
1 parent 167ceff commit 47db51e

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/librustc/ty/context.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,12 @@ pub struct TypeckTables<'tcx> {
417417
/// All the existential types that are restricted to concrete types
418418
/// by this function
419419
pub concrete_existential_types: FxHashMap<DefId, Ty<'tcx>>,
420+
421+
/// Given the closure ID this map provides the list of UpvarIDs used by it.
422+
/// The upvarID contains the HIR node ID and it also contains the full path
423+
/// leading to the member of the struct or tuple that is used instead of the
424+
/// entire variable.
425+
pub upvar_list: ty::UpvarListMap<'tcx>,
420426
}
421427

422428
impl<'tcx> TypeckTables<'tcx> {
@@ -441,6 +447,7 @@ impl<'tcx> TypeckTables<'tcx> {
441447
tainted_by_errors: false,
442448
free_region_map: Default::default(),
443449
concrete_existential_types: Default::default(),
450+
upvar_list: Default::default(),
444451
}
445452
}
446453

@@ -741,6 +748,8 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TypeckTables<'gcx> {
741748
tainted_by_errors,
742749
ref free_region_map,
743750
ref concrete_existential_types,
751+
ref upvar_list,
752+
744753
} = *self;
745754

746755
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
@@ -783,6 +792,7 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TypeckTables<'gcx> {
783792
tainted_by_errors.hash_stable(hcx, hasher);
784793
free_region_map.hash_stable(hcx, hasher);
785794
concrete_existential_types.hash_stable(hcx, hasher);
795+
upvar_list.hash_stable(hcx, hasher);
786796
})
787797
}
788798
}

src/librustc/ty/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,8 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ty::TyS<'gcx> {
589589

590590
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
591591

592+
pub type UpvarListMap<'tcx> = FxHashMap<DefId, Vec<UpvarId>>;
593+
592594
impl<'tcx> serialize::UseSpecializedEncodable for Ty<'tcx> {}
593595
impl<'tcx> serialize::UseSpecializedDecodable for Ty<'tcx> {}
594596

src/librustc_typeck/check/upvar.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
122122
};
123123

124124
self.tcx.with_freevars(closure_node_id, |freevars| {
125+
let mut freevar_list: Vec<ty::UpvarId> = Vec::new();
125126
for freevar in freevars {
126127
let upvar_id = ty::UpvarId {
127128
var_path: ty::UpvarPath {
128-
hir_id : self.tcx.hir().node_to_hir_id(freevar.var_id()),
129+
hir_id: self.tcx.hir().node_to_hir_id(freevar.var_id()),
129130
},
130131
closure_expr_id: LocalDefId::from_def_id(closure_def_id),
131132
};
132133
debug!("seed upvar_id {:?}", upvar_id);
134+
freevar_list.push(upvar_id);
133135

134136
let capture_kind = match capture_clause {
135137
hir::CaptureByValue => ty::UpvarCapture::ByValue,
@@ -149,6 +151,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
149151
.upvar_capture_map
150152
.insert(upvar_id, capture_kind);
151153
}
154+
self.tables
155+
.borrow_mut()
156+
.upvar_list
157+
.insert(closure_def_id, freevar_list);
152158
});
153159

154160
let body_owner_def_id = self.tcx.hir().body_owner_def_id(body.id());
@@ -166,7 +172,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
166172
self.param_env,
167173
region_scope_tree,
168174
&self.tables.borrow(),
169-
).consume_body(body);
175+
)
176+
.consume_body(body);
170177

171178
if let Some(closure_substs) = infer_kind {
172179
// Unify the (as yet unbound) type variable in the closure
@@ -240,9 +247,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
240247
let var_hir_id = tcx.hir().node_to_hir_id(var_node_id);
241248
let freevar_ty = self.node_ty(var_hir_id);
242249
let upvar_id = ty::UpvarId {
243-
var_path: ty::UpvarPath {
244-
hir_id: var_hir_id,
245-
},
250+
var_path: ty::UpvarPath { hir_id: var_hir_id },
246251
closure_expr_id: LocalDefId::from_def_id(closure_def_index),
247252
};
248253
let capture = self.tables.borrow().upvar_capture(upvar_id);
@@ -262,7 +267,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
262267
},
263268
),
264269
}
265-
}).collect()
270+
})
271+
.collect()
266272
})
267273
}
268274
}

0 commit comments

Comments
 (0)