Skip to content

Commit fed58f3

Browse files
committed
store the visit order in the Crate
1 parent f69ce16 commit fed58f3

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

src/librustc/dep_graph/visit.rs

+3-25
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use hir;
1212
use hir::def_id::DefId;
1313
use hir::itemlikevisit::ItemLikeVisitor;
14-
use hir::intravisit::{self, NestedVisitorMap, Visitor};
1514
use ty::TyCtxt;
1615

1716
use super::dep_node::DepNode;
@@ -79,30 +78,9 @@ pub fn visit_all_item_likes_in_krate<'a, 'tcx, V, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>
7978
pub fn visit_all_bodies_in_krate<'a, 'tcx, C>(tcx: TyCtxt<'a, 'tcx, 'tcx>, callback: C)
8079
where C: Fn(/* body_owner */ DefId, /* body id */ hir::BodyId),
8180
{
82-
// NB: we use a visitor here rather than walking the keys of the
83-
// hashmap so as to ensure we visit the bodies "in order".
84-
8581
let krate = tcx.hir.krate();
86-
intravisit::walk_crate(&mut V { tcx, callback }, krate);
87-
88-
struct V<'a, 'tcx: 'a, C> {
89-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
90-
callback: C
91-
}
92-
93-
impl<'a, 'tcx, C> Visitor<'tcx> for V<'a, 'tcx, C>
94-
where C: Fn(/* body_owner */ DefId, /* body id */ hir::BodyId),
95-
{
96-
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
97-
NestedVisitorMap::All(&self.tcx.hir)
98-
}
99-
100-
fn visit_body(&mut self, body: &'tcx hir::Body) {
101-
let body_id = body.id();
102-
let body_owner_def_id = self.tcx.hir.body_owner_def_id(body_id);
103-
(self.callback)(body_owner_def_id, body_id);
104-
105-
intravisit::walk_body(self, body);
106-
}
82+
for &body_id in &krate.body_ids {
83+
let body_owner_def_id = tcx.hir.body_owner_def_id(body_id);
84+
callback(body_owner_def_id, body_id);
10785
}
10886
}

src/librustc/hir/lowering.rs

+10
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ impl<'a> LoweringContext<'a> {
185185
let module = self.lower_mod(&c.module);
186186
let attrs = self.lower_attrs(&c.attrs);
187187
let exported_macros = c.exported_macros.iter().map(|m| self.lower_macro_def(m)).collect();
188+
let body_ids = body_ids(&self.bodies);
188189

189190
hir::Crate {
190191
module: module,
@@ -195,6 +196,7 @@ impl<'a> LoweringContext<'a> {
195196
trait_items: self.trait_items,
196197
impl_items: self.impl_items,
197198
bodies: self.bodies,
199+
body_ids: body_ids,
198200
}
199201
}
200202

@@ -2408,3 +2410,11 @@ impl<'a> LoweringContext<'a> {
24082410
}
24092411
}
24102412
}
2413+
2414+
fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body>) -> Vec<hir::BodyId> {
2415+
// Sorting by span ensures that we get things in order within a
2416+
// file, and also puts the files in a sensible order.
2417+
let mut body_ids: Vec<_> = bodies.keys().cloned().collect();
2418+
body_ids.sort_by_key(|b| bodies[b].value.span);
2419+
body_ids
2420+
}

src/librustc/hir/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,12 @@ pub struct Crate {
410410
pub trait_items: BTreeMap<TraitItemId, TraitItem>,
411411
pub impl_items: BTreeMap<ImplItemId, ImplItem>,
412412
pub bodies: BTreeMap<BodyId, Body>,
413+
414+
/// A list of the body ids written out in the order in which they
415+
/// appear in the crate. If you're going to process all the bodies
416+
/// in the crate, you should iterate over this list rather than the keys
417+
/// of bodies.
418+
pub body_ids: Vec<BodyId>,
413419
}
414420

415421
impl Crate {

src/librustc_incremental/calculate_svh/svh_visitor.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,7 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
11661166
trait_items: _,
11671167
impl_items: _,
11681168
bodies: _,
1169+
body_ids: _,
11691170
} = *krate;
11701171

11711172
visit::Visitor::visit_mod(self, module, span, ast::CRATE_NODE_ID);

0 commit comments

Comments
 (0)