Skip to content
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

Avoid nondeterminism in trimmed_def_paths #89408

Merged
merged 1 commit into from
Oct 2, 2021
Merged
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
25 changes: 23 additions & 2 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2408,7 +2408,7 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N
///
/// The implementation uses similar import discovery logic to that of 'use' suggestions.
fn trimmed_def_paths(tcx: TyCtxt<'_>, (): ()) -> FxHashMap<DefId, Symbol> {
let mut map = FxHashMap::default();
let mut map: FxHashMap<DefId, Symbol> = FxHashMap::default();

if let TrimmedDefPaths::GoodPath = tcx.sess.opts.trimmed_def_paths {
// For good paths causing this bug, the `rustc_middle::ty::print::with_no_trimmed_paths`
Expand Down Expand Up @@ -2446,8 +2446,29 @@ fn trimmed_def_paths(tcx: TyCtxt<'_>, (): ()) -> FxHashMap<DefId, Symbol> {
});

for ((_, symbol), opt_def_id) in unique_symbols_rev.drain() {
use std::collections::hash_map::Entry::{Occupied, Vacant};

if let Some(def_id) = opt_def_id {
map.insert(def_id, symbol);
match map.entry(def_id) {
Occupied(mut v) => {
// A single DefId can be known under multiple names (e.g.,
// with a `pub use ... as ...;`). We need to ensure that the
// name placed in this map is chosen deterministically, so
// if we find multiple names (`symbol`) resolving to the
// same `def_id`, we prefer the lexicographically smallest
// name.
//
// Any stable ordering would be fine here though.
if *v.get() != symbol {
if v.get().as_str() > symbol.as_str() {
v.insert(symbol);
}
}
}
Vacant(v) => {
v.insert(symbol);
}
}
}
}

Expand Down