Skip to content

Commit 5872d3e

Browse files
committed
Deterministic external crate suggestion.
This commit ensures that the external crate suggestion is deterministic by using a `BTreeMap` rather than a `FxHashMap`. This is particularly useful as `std` and `core` will often contain the same items and therefore the suggestion would previously suggest either for any given error - in this case, the suggestion will always prefer `std` now.
1 parent 9d408e0 commit 5872d3e

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/librustc_resolve/error_reporting.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
use {CrateLint, PathResult};
1212

13+
use std::collections::BTreeSet;
14+
1315
use syntax::ast::Ident;
14-
use syntax::symbol::keywords;
16+
use syntax::symbol::{keywords, Symbol};
1517
use syntax_pos::Span;
1618

1719
use resolve_imports::ImportResolver;
@@ -131,14 +133,19 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
131133
span: Span,
132134
mut path: Vec<Ident>
133135
) -> Option<Vec<Ident>> {
134-
// Need to clone else we can't call `resolve_path` without a borrow error.
135-
let external_crate_names = self.resolver.session.extern_prelude.clone();
136+
// Need to clone else we can't call `resolve_path` without a borrow error. We also store
137+
// into a `BTreeMap` so we can get consistent ordering (and therefore the same diagnostic)
138+
// each time.
139+
let external_crate_names: BTreeSet<Symbol> = self.resolver.session.extern_prelude
140+
.clone().drain().collect();
136141

137142
// Insert a new path segment that we can replace.
138143
let new_path_segment = path[0].clone();
139144
path.insert(1, new_path_segment);
140145

141-
for name in &external_crate_names {
146+
// Iterate in reverse so that we start with crates at the end of the alphabet. This means
147+
// that we'll always get `std` before `core`.
148+
for name in external_crate_names.iter().rev() {
142149
let ident = Ident::with_empty_ctxt(*name);
143150
// Calling `maybe_process_path_extern` ensures that we're only running `resolve_path`
144151
// on a crate name that won't ICE.

0 commit comments

Comments
 (0)