Skip to content

Commit 4a889a9

Browse files
committed
Auto merge of rust-lang#131213 - ismailarilik:handle-potential-query-instability-lint-for-rustc-resolve, r=<try>
Handle `rustc_resolve` cases of `rustc::potential_query_instability` lint This PR removes `#![allow(rustc::potential_query_instability)]` line from [`compiler/rustc_resolve/src/lib.rs`](https://github.com/rust-lang/rust/blob/master/compiler/rustc_resolve/src/lib.rs#L12) and converts `FxHash{Map,Set}` types into `FxIndex{Map,Set}` to suppress lint errors. A somewhat tracking issue: rust-lang#84447 r? `@compiler-errors`
2 parents 66701c4 + 5d14c34 commit 4a889a9

File tree

6 files changed

+45
-14
lines changed

6 files changed

+45
-14
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10861086
this.add_module_candidates(module, &mut suggestions, filter_fn, None);
10871087
}
10881088
Scope::MacroUsePrelude => {
1089+
// The suggestions are deterministically sorted at the bottom of this function.
1090+
#[allow(rustc::potential_query_instability)]
10891091
suggestions.extend(this.macro_use_prelude.iter().filter_map(
10901092
|(name, binding)| {
10911093
let res = binding.res();
@@ -1104,6 +1106,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11041106
}
11051107
}
11061108
Scope::ExternPrelude => {
1109+
// The suggestions are deterministically sorted at the bottom of this function.
1110+
#[allow(rustc::potential_query_instability)]
11071111
suggestions.extend(this.extern_prelude.iter().filter_map(|(ident, _)| {
11081112
let res = Res::Def(DefKind::Mod, CRATE_DEF_ID.to_def_id());
11091113
filter_fn(res).then_some(TypoSuggestion::typo_from_ident(*ident, res))
@@ -1362,6 +1366,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13621366
);
13631367

13641368
if lookup_ident.span.at_least_rust_2018() {
1369+
// Extended suggestions will be sorted at the end of this function.
1370+
#[allow(rustc::potential_query_instability)]
13651371
for ident in self.extern_prelude.clone().into_keys() {
13661372
if ident.span.from_expansion() {
13671373
// Idents are adjusted to the root context before being
@@ -1416,6 +1422,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14161422
}
14171423
}
14181424

1425+
// Make sure error reporting is deterministic.
1426+
suggestions.sort_by_key(|suggestion| suggestion.did.unwrap().index);
14191427
suggestions
14201428
}
14211429

@@ -1467,7 +1475,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14671475
return;
14681476
}
14691477

1470-
let unused_macro = self.unused_macros.iter().find_map(|(def_id, (_, unused_ident))| {
1478+
// Make ordering consistent before iteration
1479+
#[allow(rustc::potential_query_instability)]
1480+
let mut unused_macros: Vec<_> = self.unused_macros.iter().collect();
1481+
unused_macros.sort_by_key(|&(_, (key, _))| key);
1482+
let unused_macro = unused_macros.iter().find_map(|(def_id, (_, unused_ident))| {
14711483
if unused_ident.name == ident.name { Some((def_id, unused_ident)) } else { None }
14721484
});
14731485

@@ -1954,6 +1966,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19541966
ident: Symbol,
19551967
current_module: Module<'ra>,
19561968
) -> Option<Symbol> {
1969+
// The candidates are sorted just below.
1970+
#[allow(rustc::potential_query_instability)]
19571971
let mut candidates = self
19581972
.extern_prelude
19591973
.keys()
@@ -2342,6 +2356,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23422356
// Sort extern crate names in *reverse* order to get
23432357
// 1) some consistent ordering for emitted diagnostics, and
23442358
// 2) `std` suggestions before `core` suggestions.
2359+
#[allow(rustc::potential_query_instability)]
23452360
let mut extern_crate_names =
23462361
self.extern_prelude.keys().map(|ident| ident.name).collect::<Vec<_>>();
23472362
extern_crate_names.sort_by(|a, b| b.as_str().partial_cmp(a.as_str()).unwrap());
@@ -2839,6 +2854,8 @@ fn show_candidates(
28392854
} else {
28402855
// Get the unique item kinds and if there's only one, we use the right kind name
28412856
// instead of the more generic "items".
2857+
// Ordering is not important if there's only one element in the set.
2858+
#[allow(rustc::potential_query_instability)]
28422859
let mut kinds = accessible_path_strings
28432860
.iter()
28442861
.map(|(_, descr, _, _, _)| *descr)

compiler/rustc_resolve/src/imports.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::cell::Cell;
44
use std::mem;
55

66
use rustc_ast::NodeId;
7-
use rustc_data_structures::fx::FxHashSet;
7+
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
88
use rustc_data_structures::intern::Interned;
99
use rustc_errors::codes::*;
1010
use rustc_errors::{Applicability, MultiSpan, pluralize, struct_span_code_err};
@@ -220,7 +220,7 @@ impl<'ra> ImportData<'ra> {
220220
pub(crate) struct NameResolution<'ra> {
221221
/// Single imports that may define the name in the namespace.
222222
/// Imports are arena-allocated, so it's ok to use pointers as keys.
223-
pub single_imports: FxHashSet<Import<'ra>>,
223+
pub single_imports: FxIndexSet<Import<'ra>>,
224224
/// The least shadowable known binding for this name, or None if there are no known bindings.
225225
pub binding: Option<NameBinding<'ra>>,
226226
pub shadowed_glob: Option<NameBinding<'ra>>,
@@ -482,7 +482,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
482482
let key = BindingKey::new(target, ns);
483483
let _ = this.try_define(import.parent_scope.module, key, dummy_binding, false);
484484
this.update_resolution(import.parent_scope.module, key, false, |_, resolution| {
485-
resolution.single_imports.remove(&import);
485+
resolution.single_imports.shift_remove(&import);
486486
})
487487
});
488488
self.record_use(target, dummy_binding, Used::Other);
@@ -837,7 +837,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
837837
}
838838
let key = BindingKey::new(target, ns);
839839
this.update_resolution(parent, key, false, |_, resolution| {
840-
resolution.single_imports.remove(&import);
840+
resolution.single_imports.shift_remove(&import);
841841
});
842842
}
843843
}

compiler/rustc_resolve/src/late.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ mod diagnostics;
4444

4545
type Res = def::Res<NodeId>;
4646

47-
type IdentMap<T> = FxHashMap<Ident, T>;
48-
4947
use diagnostics::{ElisionFnParameter, LifetimeElisionCandidate, MissingLifetime};
5048

5149
#[derive(Copy, Clone, Debug)]
@@ -261,7 +259,7 @@ impl RibKind<'_> {
261259
/// resolving, the name is looked up from inside out.
262260
#[derive(Debug)]
263261
pub(crate) struct Rib<'ra, R = Res> {
264-
pub bindings: IdentMap<R>,
262+
pub bindings: FxIndexMap<Ident, R>,
265263
pub kind: RibKind<'ra>,
266264
}
267265

@@ -1550,7 +1548,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
15501548
// Allow all following defaults to refer to this type parameter.
15511549
forward_ty_ban_rib
15521550
.bindings
1553-
.remove(&Ident::with_dummy_span(param.ident.name));
1551+
.shift_remove(&Ident::with_dummy_span(param.ident.name));
15541552
}
15551553
GenericParamKind::Const { ref ty, kw_span: _, ref default } => {
15561554
// Const parameters can't have param bounds.
@@ -1578,7 +1576,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
15781576
// Allow all following defaults to refer to this const parameter.
15791577
forward_const_ban_rib
15801578
.bindings
1581-
.remove(&Ident::with_dummy_span(param.ident.name));
1579+
.shift_remove(&Ident::with_dummy_span(param.ident.name));
15821580
}
15831581
}
15841582
}
@@ -2260,6 +2258,8 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
22602258
}
22612259
}));
22622260
}
2261+
// Ordering is not important if there's only one element in the set.
2262+
#[allow(rustc::potential_query_instability)]
22632263
let mut distinct_iter = distinct.into_iter();
22642264
if let Some(res) = distinct_iter.next() {
22652265
match elision_lifetime {
@@ -3868,7 +3868,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
38683868
}
38693869
}
38703870

3871-
fn innermost_rib_bindings(&mut self, ns: Namespace) -> &mut IdentMap<Res> {
3871+
fn innermost_rib_bindings(&mut self, ns: Namespace) -> &mut FxIndexMap<Ident, Res> {
38723872
&mut self.ribs[ns].last_mut().unwrap().bindings
38733873
}
38743874

@@ -5046,7 +5046,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
50465046
let mut late_resolution_visitor = LateResolutionVisitor::new(self);
50475047
late_resolution_visitor.resolve_doc_links(&krate.attrs, MaybeExported::Ok(CRATE_NODE_ID));
50485048
visit::walk_crate(&mut late_resolution_visitor, krate);
5049-
for (id, span) in late_resolution_visitor.diag_metadata.unused_labels.iter() {
5049+
// Make ordering consistent before iteration
5050+
#[allow(rustc::potential_query_instability)]
5051+
let mut unused_labels: Vec<_> =
5052+
late_resolution_visitor.diag_metadata.unused_labels.iter().collect();
5053+
unused_labels.sort_by_key(|&(key, _)| key);
5054+
for (id, span) in unused_labels {
50505055
self.lint_buffer.buffer_lint(
50515056
lint::builtin::UNUSED_LABELS,
50525057
*id,

compiler/rustc_resolve/src/late/diagnostics.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,8 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
19281928
let Some(default_trait) = default_trait else {
19291929
return;
19301930
};
1931+
// The ordering is not important because `any` is used on the iterator.
1932+
#[allow(rustc::potential_query_instability)]
19311933
if self
19321934
.r
19331935
.extern_crate_map
@@ -2166,6 +2168,8 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
21662168
// Items from the prelude
21672169
if !module.no_implicit_prelude {
21682170
let extern_prelude = self.r.extern_prelude.clone();
2171+
// The names are sorted at the bottom of this function.
2172+
#[allow(rustc::potential_query_instability)]
21692173
names.extend(extern_prelude.iter().flat_map(|(ident, _)| {
21702174
self.r
21712175
.crate_loader(|c| c.maybe_process_path_extern(ident.name))

compiler/rustc_resolve/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// tidy-alphabetical-start
1010
#![allow(internal_features)]
1111
#![allow(rustc::diagnostic_outside_of_impl)]
12-
#![allow(rustc::potential_query_instability)]
1312
#![allow(rustc::untranslatable_diagnostic)]
1413
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
1514
#![doc(rust_logo)]

compiler/rustc_resolve/src/macros.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,11 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
346346
}
347347

348348
fn check_unused_macros(&mut self) {
349-
for (_, &(node_id, ident)) in self.unused_macros.iter() {
349+
// Make ordering consistent before iteration
350+
#[allow(rustc::potential_query_instability)]
351+
let mut unused_macros: Vec<_> = self.unused_macros.iter().collect();
352+
unused_macros.sort_by_key(|&(_, (key, _))| key);
353+
for (_, &(node_id, ident)) in unused_macros {
350354
self.lint_buffer.buffer_lint(
351355
UNUSED_MACROS,
352356
node_id,
@@ -356,6 +360,8 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
356360
}
357361

358362
for (&def_id, unused_arms) in self.unused_macro_rules.iter() {
363+
// It is already sorted below.
364+
#[allow(rustc::potential_query_instability)]
359365
let mut unused_arms = unused_arms.iter().collect::<Vec<_>>();
360366
unused_arms.sort_by_key(|&(&arm_i, _)| arm_i);
361367

0 commit comments

Comments
 (0)