diff --git a/Cargo.lock b/Cargo.lock index 901113bbff5..8fbb5e6b972 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4409,7 +4409,6 @@ dependencies = [ "rustc_feature", "rustc_fluent_macro", "rustc_hir", - "rustc_index", "rustc_macros", "rustc_metadata", "rustc_middle", diff --git a/compiler/rustc_data_structures/src/unord.rs b/compiler/rustc_data_structures/src/unord.rs index baa66cd7c85..3d44fb1fd48 100644 --- a/compiler/rustc_data_structures/src/unord.rs +++ b/compiler/rustc_data_structures/src/unord.rs @@ -109,6 +109,16 @@ impl> UnordItems { pub fn collect>>(self) -> C { self.into() } + + /// If the iterator has only one element, returns it, otherwise returns `None`. + #[track_caller] + pub fn get_only(mut self) -> Option { + let item = self.0.next(); + if self.0.next().is_some() { + return None; + } + item + } } impl UnordItems> { diff --git a/compiler/rustc_resolve/Cargo.toml b/compiler/rustc_resolve/Cargo.toml index 9ea9c58cfd1..0fcc3d8f6b3 100644 --- a/compiler/rustc_resolve/Cargo.toml +++ b/compiler/rustc_resolve/Cargo.toml @@ -18,7 +18,6 @@ rustc_expand = { path = "../rustc_expand" } rustc_feature = { path = "../rustc_feature" } rustc_fluent_macro = { path = "../rustc_fluent_macro" } rustc_hir = { path = "../rustc_hir" } -rustc_index = { path = "../rustc_index" } rustc_macros = { path = "../rustc_macros" } rustc_metadata = { path = "../rustc_metadata" } rustc_middle = { path = "../rustc_middle" } diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 56bc826c94f..363a75911ad 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -170,10 +170,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { fn report_with_use_injections(&mut self, krate: &Crate) { for UseError { mut err, candidates, def_id, instead, suggestion, path, is_call } in - self.use_injections.drain(..) + std::mem::take(&mut self.use_injections) { let (span, found_use) = if let Some(def_id) = def_id.as_local() { - UsePlacementFinder::check(krate, self.def_id_to_node_id[def_id]) + UsePlacementFinder::check(krate, self.def_id_to_node_id(def_id)) } else { (None, FoundUse::No) }; @@ -1435,7 +1435,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { let import_suggestions = self.lookup_import_candidates(ident, Namespace::MacroNS, parent_scope, is_expected); let (span, found_use) = match parent_scope.module.nearest_parent_mod().as_local() { - Some(def_id) => UsePlacementFinder::check(krate, self.def_id_to_node_id[def_id]), + Some(def_id) => UsePlacementFinder::check(krate, self.def_id_to_node_id(def_id)), None => (None, FoundUse::No), }; show_candidates( diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 0b3633a452c..73676849ecc 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -641,10 +641,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { if let Some(glob_binding) = resolution.shadowed_glob { let binding_id = match binding.kind { NameBindingKind::Res(res) => { - Some(self.def_id_to_node_id[res.def_id().expect_local()]) + Some(self.def_id_to_node_id(res.def_id().expect_local())) } NameBindingKind::Module(module) => { - Some(self.def_id_to_node_id[module.def_id().expect_local()]) + Some(self.def_id_to_node_id(module.def_id().expect_local())) } NameBindingKind::Import { import, .. } => import.id(), }; diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 1389e8c811e..73f4da8a7e2 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -5007,8 +5007,8 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { return false; } let Some(local_did) = did.as_local() else { return true }; - let Some(node_id) = self.r.def_id_to_node_id.get(local_did) else { return true }; - !self.r.proc_macros.contains(node_id) + let node_id = self.r.def_id_to_node_id(local_did); + !self.r.proc_macros.contains(&node_id) } fn resolve_doc_links(&mut self, attrs: &[Attribute], maybe_exported: MaybeExported<'_>) { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index e1476814d5c..8877a9e84ae 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -56,7 +56,6 @@ use rustc_hir::def::{ }; use rustc_hir::def_id::{CRATE_DEF_ID, CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalDefIdMap}; use rustc_hir::{PrimTy, TraitCandidate}; -use rustc_index::IndexVec; use rustc_metadata::creader::{CStore, CrateLoader}; use rustc_middle::metadata::ModChild; use rustc_middle::middle::privacy::EffectiveVisibilities; @@ -1184,7 +1183,6 @@ pub struct Resolver<'ra, 'tcx> { next_node_id: NodeId, node_id_to_def_id: NodeMap>, - def_id_to_node_id: IndexVec, /// Indices of unnamed struct or variant fields with unresolved attributes. placeholder_field_indices: FxHashMap, @@ -1369,7 +1367,6 @@ impl<'tcx> Resolver<'_, 'tcx> { debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id); self.node_id_to_def_id.insert(node_id, feed.downgrade()); } - assert_eq!(self.def_id_to_node_id.push(node_id), def_id); feed } @@ -1385,6 +1382,19 @@ impl<'tcx> Resolver<'_, 'tcx> { pub fn tcx(&self) -> TyCtxt<'tcx> { self.tcx } + + /// This function is very slow, as it iterates over the entire + /// [Resolver::node_id_to_def_id] map just to find the [NodeId] + /// that corresponds to the given [LocalDefId]. Only use this in + /// diagnostics code paths. + fn def_id_to_node_id(&self, def_id: LocalDefId) -> NodeId { + self.node_id_to_def_id + .items() + .filter(|(_, v)| v.key() == def_id) + .map(|(k, _)| *k) + .get_only() + .unwrap() + } } impl<'ra, 'tcx> Resolver<'ra, 'tcx> { @@ -1417,8 +1427,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { &mut Default::default(), ); - let mut def_id_to_node_id = IndexVec::default(); - assert_eq!(def_id_to_node_id.push(CRATE_NODE_ID), CRATE_DEF_ID); let mut node_id_to_def_id = NodeMap::default(); let crate_feed = tcx.create_local_crate_def_id(crate_span); @@ -1553,7 +1561,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { lint_buffer: LintBuffer::default(), next_node_id: CRATE_NODE_ID, node_id_to_def_id, - def_id_to_node_id, placeholder_field_indices: Default::default(), invocation_parents, legacy_const_generic_args: Default::default(), diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index e2f783d887e..1e744d4a64e 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -345,7 +345,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> { // We already lint the entire macro as unused continue; } - let node_id = self.def_id_to_node_id[def_id]; + let node_id = self.def_id_to_node_id(def_id); self.lint_buffer.buffer_lint( UNUSED_MACRO_RULES, node_id, @@ -932,7 +932,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { .invocation_parents .get(&parent_scope.expansion) .map_or(ast::CRATE_NODE_ID, |parent| { - self.def_id_to_node_id[parent.parent_def] + self.def_id_to_node_id(parent.parent_def) }); self.lint_buffer.buffer_lint( LEGACY_DERIVE_HELPERS,