Skip to content

Handle rustc_resolve cases of rustc::potential_query_instability lint #131213

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

Closed
Show file tree
Hide file tree
Changes from 13 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
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
7 changes: 6 additions & 1 deletion compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,9 +984,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {

// --- From now on we either have a glob resolution or no resolution. ---

// It is sorted before usage so ordering is not important.
#[allow(rustc::potential_query_instability)]
let mut single_imports: Vec<_> = resolution.single_imports.clone().into_iter().collect();
single_imports.sort_by_key(|import| import.0.id());

// Check if one of single imports can still define the name,
// if it can then our result is not determined and can be invalidated.
for single_import in &resolution.single_imports {
for single_import in &single_imports {
if ignore_import == Some(*single_import) {
// This branch handles a cycle in single imports.
//
Expand Down
23 changes: 16 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: FxHashMap<Ident, R>,
pub kind: RibKind<'ra>,
}

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 @@ -2795,8 +2795,12 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
break;
}

seen_bindings
.extend(parent_rib.bindings.keys().map(|ident| (*ident, ident.span)));
// It is sorted before usage so ordering is not important here.
#[allow(rustc::potential_query_instability)]
let mut idents: Vec<_> = parent_rib.bindings.keys().into_iter().collect();
idents.sort_by_key(|&ident| ident.span);

seen_bindings.extend(idents.into_iter().map(|ident| (*ident, ident.span)));
}
}

Expand Down Expand Up @@ -3868,7 +3872,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 FxHashMap<Ident, Res> {
&mut self.ribs[ns].last_mut().unwrap().bindings
}

Expand Down Expand Up @@ -5046,7 +5050,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
34 changes: 30 additions & 4 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,12 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
if let Some(rib) = &self.last_block_rib
&& let RibKind::Normal = rib.kind
{
for (ident, &res) in &rib.bindings {
// It is sorted before usage so ordering is not important here.
#[allow(rustc::potential_query_instability)]
let mut bindings: Vec<_> = rib.bindings.clone().into_iter().collect();
bindings.sort_by_key(|(ident, _)| ident.span);

for (ident, res) in &bindings {
if let Res::Local(_) = res
&& path.len() == 1
&& ident.span.eq_ctxt(path[0].ident.span)
Expand Down Expand Up @@ -944,7 +949,12 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
if let Some(err_code) = err.code {
if err_code == E0425 {
for label_rib in &self.label_ribs {
for (label_ident, node_id) in &label_rib.bindings {
// It is sorted before usage so ordering is not important here.
#[allow(rustc::potential_query_instability)]
let mut bindings: Vec<_> = label_rib.bindings.clone().into_iter().collect();
bindings.sort_by_key(|(ident, _)| ident.span);

for (label_ident, node_id) in &bindings {
let ident = path.last().unwrap().ident;
if format!("'{ident}") == label_ident.to_string() {
err.span_label(label_ident.span, "a label with a similar name exists");
Expand Down Expand Up @@ -1928,6 +1938,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 @@ -2140,6 +2152,8 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
};

// Locals and type parameters
// `names` is sorted below so ordering is not important here.
#[allow(rustc::potential_query_instability)]
for (ident, &res) in &rib.bindings {
if filter_fn(res) && ident.span.ctxt() == rib_ctxt {
names.push(TypoSuggestion::typo_from_ident(*ident, res));
Expand All @@ -2166,6 +2180,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 Expand Up @@ -2601,18 +2617,28 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
let within_scope = self.is_label_valid_from_rib(rib_index);

let rib = &self.label_ribs[rib_index];
let names = rib
// `names` is sorted below so ordering is not important here.
#[allow(rustc::potential_query_instability)]
let mut names = rib
.bindings
.iter()
.filter(|(id, _)| id.span.eq_ctxt(label.span))
.map(|(id, _)| id.name)
.collect::<Vec<Symbol>>();

// Make sure error reporting is deterministic.
names.sort();

find_best_match_for_name(&names, label.name, None).map(|symbol| {
// It is sorted before usage so ordering is not important here.
#[allow(rustc::potential_query_instability)]
let mut bindings: Vec<_> = rib.bindings.clone().into_iter().collect();
bindings.sort_by_key(|(ident, _)| ident.span);

// Upon finding a similar name, get the ident that it was from - the span
// contained within helps make a useful diagnostic. In addition, determine
// whether this candidate is within scope.
let (ident, _) = rib.bindings.iter().find(|(ident, _)| ident.name == symbol).unwrap();
let (ident, _) = bindings.iter().find(|(ident, _)| ident.name == symbol).unwrap();
(*ident, within_scope)
})
}
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
Loading