Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit fab202c

Browse files
committed
store the visit order in the Crate
1 parent 1bb1e16 commit fab202c

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

src/librustc/dep_graph/visit.rs

Lines changed: 3 additions & 25 deletions
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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ impl<'a> LoweringContext<'a> {
196196
let module = self.lower_mod(&c.module);
197197
let attrs = self.lower_attrs(&c.attrs);
198198
let exported_macros = c.exported_macros.iter().map(|m| self.lower_macro_def(m)).collect();
199+
let body_ids = body_ids(&self.bodies);
199200

200201
hir::Crate {
201202
module: module,
@@ -206,6 +207,7 @@ impl<'a> LoweringContext<'a> {
206207
trait_items: self.trait_items,
207208
impl_items: self.impl_items,
208209
bodies: self.bodies,
210+
body_ids: body_ids,
209211
trait_impls: self.trait_impls,
210212
trait_default_impl: self.trait_default_impl,
211213
}
@@ -2524,3 +2526,11 @@ impl<'a> LoweringContext<'a> {
25242526
}
25252527
}
25262528
}
2529+
2530+
fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body>) -> Vec<hir::BodyId> {
2531+
// Sorting by span ensures that we get things in order within a
2532+
// file, and also puts the files in a sensible order.
2533+
let mut body_ids: Vec<_> = bodies.keys().cloned().collect();
2534+
body_ids.sort_by_key(|b| bodies[b].value.span);
2535+
body_ids
2536+
}

src/librustc/hir/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,12 @@ pub struct Crate {
412412
pub bodies: BTreeMap<BodyId, Body>,
413413
pub trait_impls: BTreeMap<DefId, Vec<NodeId>>,
414414
pub trait_default_impl: BTreeMap<DefId, NodeId>,
415+
416+
/// A list of the body ids written out in the order in which they
417+
/// appear in the crate. If you're going to process all the bodies
418+
/// in the crate, you should iterate over this list rather than the keys
419+
/// of bodies.
420+
pub body_ids: Vec<BodyId>,
415421
}
416422

417423
impl Crate {

src/librustc_incremental/calculate_svh/svh_visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,9 +1167,9 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
11671167
trait_items: _,
11681168
impl_items: _,
11691169
bodies: _,
1170-
11711170
trait_impls: _,
11721171
trait_default_impl: _,
1172+
body_ids: _,
11731173
} = *krate;
11741174

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

0 commit comments

Comments
 (0)