Skip to content

Commit

Permalink
Auto merge of rust-lang#131213 - ismailarilik:handle-potential-query-…
Browse files Browse the repository at this point in the history
…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`
  • Loading branch information
bors committed Oct 28, 2024
2 parents 66701c4 + 5d14c34 commit 4a889a9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
19 changes: 18 additions & 1 deletion compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
this.add_module_candidates(module, &mut suggestions, filter_fn, None);
}
Scope::MacroUsePrelude => {
// The suggestions are deterministically sorted at the bottom of this function.
#[allow(rustc::potential_query_instability)]
suggestions.extend(this.macro_use_prelude.iter().filter_map(
|(name, binding)| {
let res = binding.res();
Expand All @@ -1104,6 +1106,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
}
Scope::ExternPrelude => {
// The suggestions are deterministically sorted at the bottom of this function.
#[allow(rustc::potential_query_instability)]
suggestions.extend(this.extern_prelude.iter().filter_map(|(ident, _)| {
let res = Res::Def(DefKind::Mod, CRATE_DEF_ID.to_def_id());
filter_fn(res).then_some(TypoSuggestion::typo_from_ident(*ident, res))
Expand Down Expand Up @@ -1362,6 +1366,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
);

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

// Make sure error reporting is deterministic.
suggestions.sort_by_key(|suggestion| suggestion.did.unwrap().index);
suggestions
}

Expand Down Expand Up @@ -1467,7 +1475,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
return;
}

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

Expand Down Expand Up @@ -1954,6 +1966,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
ident: Symbol,
current_module: Module<'ra>,
) -> Option<Symbol> {
// The candidates are sorted just below.
#[allow(rustc::potential_query_instability)]
let mut candidates = self
.extern_prelude
.keys()
Expand Down Expand Up @@ -2342,6 +2356,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// Sort extern crate names in *reverse* order to get
// 1) some consistent ordering for emitted diagnostics, and
// 2) `std` suggestions before `core` suggestions.
#[allow(rustc::potential_query_instability)]
let mut extern_crate_names =
self.extern_prelude.keys().map(|ident| ident.name).collect::<Vec<_>>();
extern_crate_names.sort_by(|a, b| b.as_str().partial_cmp(a.as_str()).unwrap());
Expand Down Expand Up @@ -2839,6 +2854,8 @@ fn show_candidates(
} else {
// Get the unique item kinds and if there's only one, we use the right kind name
// instead of the more generic "items".
// Ordering is not important if there's only one element in the set.
#[allow(rustc::potential_query_instability)]
let mut kinds = accessible_path_strings
.iter()
.map(|(_, descr, _, _, _)| *descr)
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::cell::Cell;
use std::mem;

use rustc_ast::NodeId;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
use rustc_data_structures::intern::Interned;
use rustc_errors::codes::*;
use rustc_errors::{Applicability, MultiSpan, pluralize, struct_span_code_err};
Expand Down Expand Up @@ -220,7 +220,7 @@ impl<'ra> ImportData<'ra> {
pub(crate) struct NameResolution<'ra> {
/// Single imports that may define the name in the namespace.
/// Imports are arena-allocated, so it's ok to use pointers as keys.
pub single_imports: FxHashSet<Import<'ra>>,
pub single_imports: FxIndexSet<Import<'ra>>,
/// The least shadowable known binding for this name, or None if there are no known bindings.
pub binding: Option<NameBinding<'ra>>,
pub shadowed_glob: Option<NameBinding<'ra>>,
Expand Down Expand Up @@ -482,7 +482,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let key = BindingKey::new(target, ns);
let _ = this.try_define(import.parent_scope.module, key, dummy_binding, false);
this.update_resolution(import.parent_scope.module, key, false, |_, resolution| {
resolution.single_imports.remove(&import);
resolution.single_imports.shift_remove(&import);
})
});
self.record_use(target, dummy_binding, Used::Other);
Expand Down Expand Up @@ -837,7 +837,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
let key = BindingKey::new(target, ns);
this.update_resolution(parent, key, false, |_, resolution| {
resolution.single_imports.remove(&import);
resolution.single_imports.shift_remove(&import);
});
}
}
Expand Down
19 changes: 12 additions & 7 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ mod diagnostics;

type Res = def::Res<NodeId>;

type IdentMap<T> = FxHashMap<Ident, T>;

use diagnostics::{ElisionFnParameter, LifetimeElisionCandidate, MissingLifetime};

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

Expand Down Expand Up @@ -1550,7 +1548,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
// Allow all following defaults to refer to this type parameter.
forward_ty_ban_rib
.bindings
.remove(&Ident::with_dummy_span(param.ident.name));
.shift_remove(&Ident::with_dummy_span(param.ident.name));
}
GenericParamKind::Const { ref ty, kw_span: _, ref default } => {
// Const parameters can't have param bounds.
Expand Down Expand Up @@ -1578,7 +1576,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
// Allow all following defaults to refer to this const parameter.
forward_const_ban_rib
.bindings
.remove(&Ident::with_dummy_span(param.ident.name));
.shift_remove(&Ident::with_dummy_span(param.ident.name));
}
}
}
Expand Down Expand Up @@ -2260,6 +2258,8 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
}
}));
}
// Ordering is not important if there's only one element in the set.
#[allow(rustc::potential_query_instability)]
let mut distinct_iter = distinct.into_iter();
if let Some(res) = distinct_iter.next() {
match elision_lifetime {
Expand Down Expand Up @@ -3868,7 +3868,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
}
}

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

Expand Down Expand Up @@ -5046,7 +5046,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let mut late_resolution_visitor = LateResolutionVisitor::new(self);
late_resolution_visitor.resolve_doc_links(&krate.attrs, MaybeExported::Ok(CRATE_NODE_ID));
visit::walk_crate(&mut late_resolution_visitor, krate);
for (id, span) in late_resolution_visitor.diag_metadata.unused_labels.iter() {
// Make ordering consistent before iteration
#[allow(rustc::potential_query_instability)]
let mut unused_labels: Vec<_> =
late_resolution_visitor.diag_metadata.unused_labels.iter().collect();
unused_labels.sort_by_key(|&(key, _)| key);
for (id, span) in unused_labels {
self.lint_buffer.buffer_lint(
lint::builtin::UNUSED_LABELS,
*id,
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,8 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
let Some(default_trait) = default_trait else {
return;
};
// The ordering is not important because `any` is used on the iterator.
#[allow(rustc::potential_query_instability)]
if self
.r
.extern_crate_map
Expand Down Expand Up @@ -2166,6 +2168,8 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
// Items from the prelude
if !module.no_implicit_prelude {
let extern_prelude = self.r.extern_prelude.clone();
// The names are sorted at the bottom of this function.
#[allow(rustc::potential_query_instability)]
names.extend(extern_prelude.iter().flat_map(|(ident, _)| {
self.r
.crate_loader(|c| c.maybe_process_path_extern(ident.name))
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// tidy-alphabetical-start
#![allow(internal_features)]
#![allow(rustc::diagnostic_outside_of_impl)]
#![allow(rustc::potential_query_instability)]
#![allow(rustc::untranslatable_diagnostic)]
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![doc(rust_logo)]
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
}

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

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

Expand Down

0 comments on commit 4a889a9

Please sign in to comment.