Skip to content

Commit 9e5ef87

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 b026d85 + 0944d8c commit 9e5ef87

File tree

6 files changed

+47
-15
lines changed

6 files changed

+47
-15
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+20-2
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,7 +1366,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13621366
);
13631367

13641368
if lookup_ident.span.at_least_rust_2018() {
1365-
for ident in self.extern_prelude.clone().into_keys() {
1369+
// `idents` is sorted before usage so ordering is not important here.
1370+
#[allow(rustc::potential_query_instability)]
1371+
let mut idents: Vec<_> = self.extern_prelude.clone().into_keys().collect();
1372+
idents.sort_by_key(|ident| ident.span);
1373+
1374+
for ident in idents {
13661375
if ident.span.from_expansion() {
13671376
// Idents are adjusted to the root context before being
13681377
// resolved in the extern prelude, so reporting this to the
@@ -1467,7 +1476,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14671476
return;
14681477
}
14691478

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

@@ -1954,6 +1967,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19541967
ident: Symbol,
19551968
current_module: Module<'ra>,
19561969
) -> Option<Symbol> {
1970+
// The candidates are sorted just below.
1971+
#[allow(rustc::potential_query_instability)]
19571972
let mut candidates = self
19581973
.extern_prelude
19591974
.keys()
@@ -2342,6 +2357,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23422357
// Sort extern crate names in *reverse* order to get
23432358
// 1) some consistent ordering for emitted diagnostics, and
23442359
// 2) `std` suggestions before `core` suggestions.
2360+
#[allow(rustc::potential_query_instability)]
23452361
let mut extern_crate_names =
23462362
self.extern_prelude.keys().map(|ident| ident.name).collect::<Vec<_>>();
23472363
extern_crate_names.sort_by(|a, b| b.as_str().partial_cmp(a.as_str()).unwrap());
@@ -2839,6 +2855,8 @@ fn show_candidates(
28392855
} else {
28402856
// Get the unique item kinds and if there's only one, we use the right kind name
28412857
// instead of the more generic "items".
2858+
// Ordering is not important if there's only one element in the set.
2859+
#[allow(rustc::potential_query_instability)]
28422860
let mut kinds = accessible_path_strings
28432861
.iter()
28442862
.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.swap_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.swap_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

@@ -1536,7 +1534,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
15361534
// Allow all following defaults to refer to this type parameter.
15371535
forward_ty_ban_rib
15381536
.bindings
1539-
.remove(&Ident::with_dummy_span(param.ident.name));
1537+
.swap_remove(&Ident::with_dummy_span(param.ident.name));
15401538
}
15411539
GenericParamKind::Const { ref ty, kw_span: _, ref default } => {
15421540
// Const parameters can't have param bounds.
@@ -1564,7 +1562,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
15641562
// Allow all following defaults to refer to this const parameter.
15651563
forward_const_ban_rib
15661564
.bindings
1567-
.remove(&Ident::with_dummy_span(param.ident.name));
1565+
.swap_remove(&Ident::with_dummy_span(param.ident.name));
15681566
}
15691567
}
15701568
}
@@ -2246,6 +2244,8 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
22462244
}
22472245
}));
22482246
}
2247+
// Ordering is not important if there's only one element in the set.
2248+
#[allow(rustc::potential_query_instability)]
22492249
let mut distinct_iter = distinct.into_iter();
22502250
if let Some(res) = distinct_iter.next() {
22512251
match elision_lifetime {
@@ -3854,7 +3854,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
38543854
}
38553855
}
38563856

3857-
fn innermost_rib_bindings(&mut self, ns: Namespace) -> &mut IdentMap<Res> {
3857+
fn innermost_rib_bindings(&mut self, ns: Namespace) -> &mut FxIndexMap<Ident, Res> {
38583858
&mut self.ribs[ns].last_mut().unwrap().bindings
38593859
}
38603860

@@ -5008,7 +5008,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
50085008
let mut late_resolution_visitor = LateResolutionVisitor::new(self);
50095009
late_resolution_visitor.resolve_doc_links(&krate.attrs, MaybeExported::Ok(CRATE_NODE_ID));
50105010
visit::walk_crate(&mut late_resolution_visitor, krate);
5011-
for (id, span) in late_resolution_visitor.diag_metadata.unused_labels.iter() {
5011+
// Make ordering consistent before iteration
5012+
#[allow(rustc::potential_query_instability)]
5013+
let mut unused_labels: Vec<_> =
5014+
late_resolution_visitor.diag_metadata.unused_labels.iter().collect();
5015+
unused_labels.sort_by_key(|&(key, _)| key);
5016+
for (id, span) in unused_labels {
50125017
self.lint_buffer.buffer_lint(
50135018
lint::builtin::UNUSED_LABELS,
50145019
*id,

compiler/rustc_resolve/src/late/diagnostics.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,8 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
19571957
let Some(default_trait) = default_trait else {
19581958
return;
19591959
};
1960+
// The ordering is not important because `any` is used on the iterator.
1961+
#[allow(rustc::potential_query_instability)]
19601962
if self
19611963
.r
19621964
.extern_crate_map
@@ -2195,6 +2197,8 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
21952197
// Items from the prelude
21962198
if !module.no_implicit_prelude {
21972199
let extern_prelude = self.r.extern_prelude.clone();
2200+
// The names are sorted at the bottom of this function.
2201+
#[allow(rustc::potential_query_instability)]
21982202
names.extend(extern_prelude.iter().flat_map(|(ident, _)| {
21992203
self.r
22002204
.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)