Skip to content

Commit 53aa8a1

Browse files
authored
Rollup merge of rust-lang#56906 - blitzerr:master, r=nikomatsakis
Issue rust-lang#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.
2 parents e69a5cb + 69e4918 commit 53aa8a1

File tree

5 files changed

+81
-26
lines changed

5 files changed

+81
-26
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,
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ pub struct UpvarBorrow<'tcx> {
808808
pub region: ty::Region<'tcx>,
809809
}
810810

811+
pub type UpvarListMap = FxHashMap<DefId, Vec<UpvarId>>;
811812
pub type UpvarCaptureMap<'tcx> = FxHashMap<UpvarId, UpvarCapture<'tcx>>;
812813

813814
#[derive(Copy, Clone)]

src/librustc_mir/build/mod.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use hair::cx::Cx;
44
use hair::{LintLevel, BindingMode, PatternKind};
55
use rustc::hir;
66
use rustc::hir::Node;
7-
use rustc::hir::def_id::{DefId, LocalDefId};
7+
use rustc::hir::def_id::DefId;
88
use rustc::middle::region;
99
use rustc::mir::*;
1010
use rustc::mir::visit::{MutVisitor, TyContext};
@@ -640,32 +640,39 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
640640
let arguments: Vec<_> = arguments.collect();
641641

642642
let tcx = hir.tcx();
643-
let span = tcx.hir().span(fn_id);
643+
let tcx_hir = tcx.hir();
644+
let span = tcx_hir.span(fn_id);
645+
646+
let hir_tables = hir.tables();
647+
let fn_def_id = tcx_hir.local_def_id(fn_id);
644648

645649
// Gather the upvars of a closure, if any.
646-
let upvar_decls: Vec<_> = tcx.with_freevars(fn_id, |freevars| {
647-
freevars.iter().map(|fv| {
648-
let var_id = fv.var_id();
649-
let var_hir_id = tcx.hir().node_to_hir_id(var_id);
650-
let closure_expr_id = tcx.hir().local_def_id(fn_id);
651-
let capture = hir.tables().upvar_capture(ty::UpvarId {
652-
var_path: ty::UpvarPath {hir_id: var_hir_id},
653-
closure_expr_id: LocalDefId::from_def_id(closure_expr_id),
654-
});
650+
// In analyze_closure() in upvar.rs we gathered a list of upvars used by a
651+
// closure and we stored in a map called upvar_list in TypeckTables indexed
652+
// with the closure's DefId. Here, we run through that vec of UpvarIds for
653+
// the given closure and use the necessary information to create UpvarDecl.
654+
let upvar_decls: Vec<_> = hir_tables
655+
.upvar_list
656+
.get(&fn_def_id)
657+
.into_iter()
658+
.flatten()
659+
.map(|upvar_id| {
660+
let var_hir_id = upvar_id.var_path.hir_id;
661+
let var_node_id = tcx_hir.hir_to_node_id(var_hir_id);
662+
let capture = hir_tables.upvar_capture(*upvar_id);
655663
let by_ref = match capture {
656664
ty::UpvarCapture::ByValue => false,
657-
ty::UpvarCapture::ByRef(..) => true
665+
ty::UpvarCapture::ByRef(..) => true,
658666
};
659667
let mut decl = UpvarDecl {
660668
debug_name: keywords::Invalid.name(),
661669
var_hir_id: ClearCrossCrate::Set(var_hir_id),
662670
by_ref,
663671
mutability: Mutability::Not,
664672
};
665-
if let Some(Node::Binding(pat)) = tcx.hir().find(var_id) {
673+
if let Some(Node::Binding(pat)) = tcx_hir.find(var_node_id) {
666674
if let hir::PatKind::Binding(_, _, ident, _) = pat.node {
667675
decl.debug_name = ident.name;
668-
669676
if let Some(&bm) = hir.tables.pat_binding_modes().get(pat.hir_id) {
670677
if bm == ty::BindByValue(hir::MutMutable) {
671678
decl.mutability = Mutability::Mut;
@@ -678,8 +685,8 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
678685
}
679686
}
680687
decl
681-
}).collect()
682-
});
688+
})
689+
.collect();
683690

684691
let mut builder = Builder::new(hir,
685692
span,
@@ -689,7 +696,6 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
689696
return_ty_span,
690697
upvar_decls);
691698

692-
let fn_def_id = tcx.hir().local_def_id(fn_id);
693699
let call_site_scope = region::Scope {
694700
id: body.value.hir_id.local_id,
695701
data: region::ScopeData::CallSite
@@ -732,7 +738,7 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
732738
// RustCall pseudo-ABI untuples the last argument.
733739
spread_arg = Some(Local::new(arguments.len()));
734740
}
735-
let closure_expr_id = tcx.hir().local_def_id(fn_id);
741+
let closure_expr_id = tcx_hir.local_def_id(fn_id);
736742
info!("fn_id {:?} has attrs {:?}", closure_expr_id,
737743
tcx.get_attrs(closure_expr_id));
738744

src/librustc_typeck/check/upvar.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,18 @@ 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::with_capacity(freevars.len());
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+
// Adding the upvar Id to the list of Upvars, which will be added
135+
// to the map for the closure at the end of the for loop.
136+
freevar_list.push(upvar_id);
133137

134138
let capture_kind = match capture_clause {
135139
hir::CaptureByValue => ty::UpvarCapture::ByValue,
@@ -149,6 +153,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
149153
.upvar_capture_map
150154
.insert(upvar_id, capture_kind);
151155
}
156+
// Add the vector of freevars to the map keyed with the closure id.
157+
// This gives us an easier access to them without having to call
158+
// with_freevars again..
159+
if !freevar_list.is_empty() {
160+
self.tables
161+
.borrow_mut()
162+
.upvar_list
163+
.insert(closure_def_id, freevar_list);
164+
}
152165
});
153166

154167
let body_owner_def_id = self.tcx.hir().body_owner_def_id(body.id());
@@ -166,7 +179,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
166179
self.param_env,
167180
region_scope_tree,
168181
&self.tables.borrow(),
169-
).consume_body(body);
182+
)
183+
.consume_body(body);
170184

171185
if let Some(closure_substs) = infer_kind {
172186
// Unify the (as yet unbound) type variable in the closure
@@ -240,9 +254,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
240254
let var_hir_id = tcx.hir().node_to_hir_id(var_node_id);
241255
let freevar_ty = self.node_ty(var_hir_id);
242256
let upvar_id = ty::UpvarId {
243-
var_path: ty::UpvarPath {
244-
hir_id: var_hir_id,
245-
},
257+
var_path: ty::UpvarPath { hir_id: var_hir_id },
246258
closure_expr_id: LocalDefId::from_def_id(closure_def_index),
247259
};
248260
let capture = self.tables.borrow().upvar_capture(upvar_id);
@@ -262,7 +274,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
262274
},
263275
),
264276
}
265-
}).collect()
277+
})
278+
.collect()
266279
})
267280
}
268281
}

src/librustc_typeck/check/writeback.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ use syntax_pos::Span;
2121
///////////////////////////////////////////////////////////////////////////
2222
// Entry point
2323

24+
// During type inference, partially inferred types are
25+
// represented using Type variables (ty::Infer). These don't appear in
26+
// the final TypeckTables since all of the types should have been
27+
// inferred once typeck_tables_of is done.
28+
// When type inference is running however, having to update the typeck
29+
// tables every time a new type is inferred would be unreasonably slow,
30+
// so instead all of the replacement happens at the end in
31+
// resolve_type_vars_in_body, which creates a new TypeTables which
32+
// doesn't contain any inference types.
2433
impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2534
pub fn resolve_type_vars_in_body(&self, body: &'gcx hir::Body) -> &'gcx ty::TypeckTables<'gcx> {
2635
let item_id = self.tcx.hir().body_owner(body.id());
@@ -35,7 +44,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
3544
wbcx.visit_node_id(arg.pat.span, arg.hir_id);
3645
}
3746
wbcx.visit_body(body);
38-
wbcx.visit_upvar_borrow_map();
47+
wbcx.visit_upvar_capture_map();
48+
wbcx.visit_upvar_list_map();
3949
wbcx.visit_closures();
4050
wbcx.visit_liberated_fn_sigs();
4151
wbcx.visit_fru_field_types();
@@ -291,7 +301,7 @@ impl<'cx, 'gcx, 'tcx> Visitor<'gcx> for WritebackCx<'cx, 'gcx, 'tcx> {
291301
}
292302

293303
impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
294-
fn visit_upvar_borrow_map(&mut self) {
304+
fn visit_upvar_capture_map(&mut self) {
295305
for (upvar_id, upvar_capture) in self.fcx.tables.borrow().upvar_capture_map.iter() {
296306
let new_upvar_capture = match *upvar_capture {
297307
ty::UpvarCapture::ByValue => ty::UpvarCapture::ByValue,
@@ -314,6 +324,21 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
314324
}
315325
}
316326

327+
/// Runs through the function context's upvar list map and adds the same to
328+
/// the TypeckTables. upvarlist is a hashmap of the list of upvars referred
329+
/// to in a closure..
330+
fn visit_upvar_list_map(&mut self) {
331+
for (closure_def_id, upvar_list) in self.fcx.tables.borrow().upvar_list.iter() {
332+
debug!(
333+
"UpvarIDs captured by closure {:?} are: {:?}",
334+
closure_def_id, upvar_list
335+
);
336+
self.tables
337+
.upvar_list
338+
.insert(*closure_def_id, upvar_list.to_vec());
339+
}
340+
}
341+
317342
fn visit_closures(&mut self) {
318343
let fcx_tables = self.fcx.tables.borrow();
319344
debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);

0 commit comments

Comments
 (0)