Skip to content

Fix visible_parent_map to choose globally minimal paths #46708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 13 additions & 0 deletions src/test/ui/auxiliary/xcrate_issue_46112_rexport_core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![crate_type="lib"]

pub extern crate core;
20 changes: 20 additions & 0 deletions src/test/ui/issue-46112.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Issue 46112: An extern crate pub reexporting libcore was causing
// paths rooted from `std` to be misrendered in the diagnostic output.

// ignore-windows
// aux-build:xcrate_issue_46112_rexport_core.rs

extern crate xcrate_issue_46112_rexport_core;
fn test(r: Result<Option<()>, &'static str>) { }
fn main() { test(Ok(())); }
//~^ mismatched types
14 changes: 14 additions & 0 deletions src/test/ui/issue-46112.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0308]: mismatched types
--> $DIR/issue-46112.rs:19:21
|
19 | fn main() { test(Ok(())); }
| ^^
| |
| expected enum `std::option::Option`, found ()
| help: try using a variant of the expected type: `Some(())`
|
= note: expected type `std::option::Option<()>`
found type `()`

error: aborting due to previous error