Skip to content

Commit a2b6401

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 93c2f05 commit a2b6401

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/librustc/ty/context.rs

+10
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,12 @@ pub struct TypeckTables<'tcx> {
431431
/// All the existential types that are restricted to concrete types
432432
/// by this function
433433
pub concrete_existential_types: FxHashMap<DefId, Ty<'tcx>>,
434+
435+
/// Given the closure ID this map provides the list of UpvarIDs used by it.
436+
/// The upvarID contains the HIR node ID and it also contains the full path
437+
/// leading to the member of the struct or tuple that is used instead of the
438+
/// entire variable.
439+
pub upvar_list: ty::UpvarListMap<'tcx>,
434440
}
435441

436442
impl<'tcx> TypeckTables<'tcx> {
@@ -456,6 +462,7 @@ impl<'tcx> TypeckTables<'tcx> {
456462
tainted_by_errors: false,
457463
free_region_map: Default::default(),
458464
concrete_existential_types: Default::default(),
465+
upvar_list: Default::default(),
459466
}
460467
}
461468

@@ -765,6 +772,8 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TypeckTables<'gcx> {
765772
tainted_by_errors,
766773
ref free_region_map,
767774
ref concrete_existential_types,
775+
ref upvar_list,
776+
768777
} = *self;
769778

770779
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
@@ -808,6 +817,7 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TypeckTables<'gcx> {
808817
tainted_by_errors.hash_stable(hcx, hasher);
809818
free_region_map.hash_stable(hcx, hasher);
810819
concrete_existential_types.hash_stable(hcx, hasher);
820+
upvar_list.hash_stable(hcx, hasher);
811821
})
812822
}
813823
}

src/librustc/ty/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ty::TyS<'gcx> {
598598

599599
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
600600

601+
pub type UpvarListMap<'tcx> = FxHashMap<DefId, Vec<UpvarId>>;
602+
601603
impl<'tcx> serialize::UseSpecializedEncodable for Ty<'tcx> {}
602604
impl<'tcx> serialize::UseSpecializedDecodable for Ty<'tcx> {}
603605

src/librustc_typeck/check/upvar.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
132132
};
133133

134134
self.tcx.with_freevars(closure_node_id, |freevars| {
135+
let mut freevar_list: Vec<ty::UpvarId> = Vec::new();
135136
for freevar in freevars {
136137
let upvar_id = ty::UpvarId {
137138
var_path: ty::UpvarPath {
138-
hir_id : self.tcx.hir().node_to_hir_id(freevar.var_id()),
139+
hir_id: self.tcx.hir().node_to_hir_id(freevar.var_id()),
139140
},
140141
closure_expr_id: LocalDefId::from_def_id(closure_def_id),
141142
};
142143
debug!("seed upvar_id {:?}", upvar_id);
144+
freevar_list.push(upvar_id);
143145

144146
let capture_kind = match capture_clause {
145147
hir::CaptureByValue => ty::UpvarCapture::ByValue,
@@ -159,6 +161,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
159161
.upvar_capture_map
160162
.insert(upvar_id, capture_kind);
161163
}
164+
self.tables
165+
.borrow_mut()
166+
.upvar_list
167+
.insert(closure_def_id, freevar_list);
162168
});
163169

164170
let body_owner_def_id = self.tcx.hir().body_owner_def_id(body.id());
@@ -176,7 +182,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
176182
self.param_env,
177183
region_scope_tree,
178184
&self.tables.borrow(),
179-
).consume_body(body);
185+
)
186+
.consume_body(body);
180187

181188
if let Some(closure_substs) = infer_kind {
182189
// Unify the (as yet unbound) type variable in the closure
@@ -250,9 +257,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
250257
let var_hir_id = tcx.hir().node_to_hir_id(var_node_id);
251258
let freevar_ty = self.node_ty(var_hir_id);
252259
let upvar_id = ty::UpvarId {
253-
var_path: ty::UpvarPath {
254-
hir_id: var_hir_id,
255-
},
260+
var_path: ty::UpvarPath { hir_id: var_hir_id },
256261
closure_expr_id: LocalDefId::from_def_id(closure_def_index),
257262
};
258263
let capture = self.tables.borrow().upvar_capture(upvar_id);
@@ -272,7 +277,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
272277
},
273278
),
274279
}
275-
}).collect()
280+
})
281+
.collect()
276282
})
277283
}
278284
}

0 commit comments

Comments
 (0)