Skip to content

Commit 0142781

Browse files
committed
Auto merge of #46708 - pnkfelix:fix-issue-46112, r=arielb1
Fix visible_parent_map to choose globally minimal paths Fix #46112: visible_parent_map construction needs a BFS over whole crate forest to get globally minimal paths. (There are other latent bugs that were e.g. causing this test case to have weirdness like `<unnamed>` in the diagnostic output. Those bugs are not fixed here, since they are issues long-standing in the stable channel.)
2 parents f8af59d + 35bcd99 commit 0142781

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

src/librustc_metadata/cstore_impl.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,31 @@ pub fn provide<'tcx>(providers: &mut Providers<'tcx>) {
294294
assert_eq!(cnum, LOCAL_CRATE);
295295
let mut visible_parent_map: DefIdMap<DefId> = DefIdMap();
296296

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

303-
let bfs_queue = &mut VecDeque::new();
314+
bfs_queue.push_back(DefId {
315+
krate: cnum,
316+
index: CRATE_DEF_INDEX
317+
});
318+
}
319+
320+
// (restrict scope of mutable-borrow of `visible_parent_map`)
321+
{
304322
let visible_parent_map = &mut visible_parent_map;
305323
let mut add_child = |bfs_queue: &mut VecDeque<_>,
306324
child: &def::Export,
@@ -326,10 +344,6 @@ pub fn provide<'tcx>(providers: &mut Providers<'tcx>) {
326344
}
327345
};
328346

329-
bfs_queue.push_back(DefId {
330-
krate: cnum,
331-
index: CRATE_DEF_INDEX
332-
});
333347
while let Some(def) = bfs_queue.pop_front() {
334348
for child in tcx.item_children(def).iter() {
335349
add_child(bfs_queue, child, def);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type="lib"]
12+
13+
pub extern crate core;

src/test/ui/issue-46112.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Issue 46112: An extern crate pub reexporting libcore was causing
12+
// paths rooted from `std` to be misrendered in the diagnostic output.
13+
14+
// ignore-windows
15+
// aux-build:xcrate_issue_46112_rexport_core.rs
16+
17+
extern crate xcrate_issue_46112_rexport_core;
18+
fn test(r: Result<Option<()>, &'static str>) { }
19+
fn main() { test(Ok(())); }
20+
//~^ mismatched types

src/test/ui/issue-46112.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-46112.rs:19:21
3+
|
4+
19 | fn main() { test(Ok(())); }
5+
| ^^
6+
| |
7+
| expected enum `std::option::Option`, found ()
8+
| help: try using a variant of the expected type: `Some(())`
9+
|
10+
= note: expected type `std::option::Option<()>`
11+
found type `()`
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)