Skip to content

Commit

Permalink
Fix rust-lang#46112: visible_parent_map construction needs a BFS over…
Browse files Browse the repository at this point in the history
… whole crate forest.
  • Loading branch information
pnkfelix committed Dec 13, 2017
1 parent 6a36019 commit 43406d2
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,31 @@ pub fn provide<'tcx>(providers: &mut Providers<'tcx>) {
assert_eq!(cnum, LOCAL_CRATE);
let mut visible_parent_map: DefIdMap<DefId> = DefIdMap();

// Issue 46112: We want the map to prefer the shortest
// paths when reporting the path to an item. Therefore we
// build up the map via a breadth-first search (BFS),
// which naturally yields minimal-length paths.
//
// Note that it needs to be a BFS over the whole forest of
// crates, not just each individual crate; otherwise you
// only get paths that are locally minimal with respect to
// whatever crate we happened to encounter first in this
// traversal, but not globally minimal across all crates.
let bfs_queue = &mut VecDeque::new();
for &cnum in tcx.crates().iter() {
// Ignore crates without a corresponding local `extern crate` item.
if tcx.missing_extern_crate_item(cnum) {
continue
}

let bfs_queue = &mut VecDeque::new();
bfs_queue.push_back(DefId {
krate: cnum,
index: CRATE_DEF_INDEX
});
}

// (restrict scope of mutable-borrow of `visible_parent_map`)
{
let visible_parent_map = &mut visible_parent_map;
let mut add_child = |bfs_queue: &mut VecDeque<_>,
child: &def::Export,
Expand All @@ -326,10 +344,6 @@ pub fn provide<'tcx>(providers: &mut Providers<'tcx>) {
}
};

bfs_queue.push_back(DefId {
krate: cnum,
index: CRATE_DEF_INDEX
});
while let Some(def) = bfs_queue.pop_front() {
for child in tcx.item_children(def).iter() {
add_child(bfs_queue, child, def);
Expand Down

0 comments on commit 43406d2

Please sign in to comment.