Skip to content

Commit 1f2cf1e

Browse files
Prefer visibility paths where items are not named _
1 parent 48dab5c commit 1f2cf1e

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_session::utils::NativeLibKind;
1717
use rustc_session::{Session, StableCrateId};
1818
use rustc_span::hygiene::{ExpnHash, ExpnId};
1919
use rustc_span::source_map::{Span, Spanned};
20-
use rustc_span::symbol::Symbol;
20+
use rustc_span::symbol::{kw, Symbol};
2121

2222
use rustc_data_structures::sync::Lrc;
2323
use smallvec::SmallVec;
@@ -295,6 +295,10 @@ pub fn provide(providers: &mut Providers) {
295295
use std::collections::vec_deque::VecDeque;
296296

297297
let mut visible_parent_map: DefIdMap<DefId> = Default::default();
298+
// This is a secondary visible_parent_map, storing the DefId of parents that re-export
299+
// the child as `_`. Since we prefer parents that don't do this, merge this map at the
300+
// end, only if we're missing any keys from the former.
301+
let mut fallback_map: DefIdMap<DefId> = Default::default();
298302

299303
// Issue 46112: We want the map to prefer the shortest
300304
// paths when reporting the path to an item. Therefore we
@@ -317,12 +321,17 @@ pub fn provide(providers: &mut Providers) {
317321
bfs_queue.push_back(DefId { krate: cnum, index: CRATE_DEF_INDEX });
318322
}
319323

320-
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &Export, parent: DefId| {
321-
if !child.vis.is_public() {
324+
let mut add_child = |bfs_queue: &mut VecDeque<_>, export: &Export, parent: DefId| {
325+
if !export.vis.is_public() {
322326
return;
323327
}
324328

325-
if let Some(child) = child.res.opt_def_id() {
329+
if let Some(child) = export.res.opt_def_id() {
330+
if export.ident.name == kw::Underscore {
331+
fallback_map.insert(child, parent);
332+
return;
333+
}
334+
326335
match visible_parent_map.entry(child) {
327336
Entry::Occupied(mut entry) => {
328337
// If `child` is defined in crate `cnum`, ensure
@@ -345,6 +354,12 @@ pub fn provide(providers: &mut Providers) {
345354
}
346355
}
347356

357+
// Fill in any missing entries with the (less preferable) path ending in `::_`.
358+
// We still use this path in a diagnostic that suggests importing `::*`.
359+
for (child, parent) in fallback_map {
360+
visible_parent_map.entry(child).or_insert(parent);
361+
}
362+
348363
visible_parent_map
349364
},
350365

0 commit comments

Comments
 (0)