Don't hold the definitions' lock across index_hir

This commit is contained in:
John Kåre Alsaker 2023-08-31 23:14:47 +02:00
parent 0c96a9260b
commit 6881eed8f6
2 changed files with 16 additions and 17 deletions

View File

@ -2,19 +2,17 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sorted_map::SortedMap; use rustc_data_structures::sorted_map::SortedMap;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId; use rustc_hir::def_id::LocalDefId;
use rustc_hir::definitions;
use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::*; use rustc_hir::*;
use rustc_index::{Idx, IndexVec}; use rustc_index::{Idx, IndexVec};
use rustc_middle::span_bug; use rustc_middle::span_bug;
use rustc_session::Session; use rustc_middle::ty::TyCtxt;
use rustc_span::source_map::SourceMap;
use rustc_span::{Span, DUMMY_SP}; use rustc_span::{Span, DUMMY_SP};
/// A visitor that walks over the HIR and collects `Node`s into a HIR map. /// A visitor that walks over the HIR and collects `Node`s into a HIR map.
pub(super) struct NodeCollector<'a, 'hir> { pub(super) struct NodeCollector<'a, 'hir> {
/// Source map tcx: TyCtxt<'hir>,
source_map: &'a SourceMap,
bodies: &'a SortedMap<ItemLocalId, &'hir Body<'hir>>, bodies: &'a SortedMap<ItemLocalId, &'hir Body<'hir>>,
/// Outputs /// Outputs
@ -25,14 +23,11 @@ pub(super) struct NodeCollector<'a, 'hir> {
parent_node: hir::ItemLocalId, parent_node: hir::ItemLocalId,
owner: OwnerId, owner: OwnerId,
definitions: &'a definitions::Definitions,
} }
#[instrument(level = "debug", skip(sess, definitions, bodies))] #[instrument(level = "debug", skip(tcx, bodies))]
pub(super) fn index_hir<'hir>( pub(super) fn index_hir<'hir>(
sess: &Session, tcx: TyCtxt<'hir>,
definitions: &definitions::Definitions,
item: hir::OwnerNode<'hir>, item: hir::OwnerNode<'hir>,
bodies: &SortedMap<ItemLocalId, &'hir Body<'hir>>, bodies: &SortedMap<ItemLocalId, &'hir Body<'hir>>,
) -> (IndexVec<ItemLocalId, Option<ParentedNode<'hir>>>, FxHashMap<LocalDefId, ItemLocalId>) { ) -> (IndexVec<ItemLocalId, Option<ParentedNode<'hir>>>, FxHashMap<LocalDefId, ItemLocalId>) {
@ -42,8 +37,7 @@ pub(super) fn index_hir<'hir>(
// used. // used.
nodes.push(Some(ParentedNode { parent: ItemLocalId::INVALID, node: item.into() })); nodes.push(Some(ParentedNode { parent: ItemLocalId::INVALID, node: item.into() }));
let mut collector = NodeCollector { let mut collector = NodeCollector {
source_map: sess.source_map(), tcx,
definitions,
owner: item.def_id(), owner: item.def_id(),
parent_node: ItemLocalId::new(0), parent_node: ItemLocalId::new(0),
nodes, nodes,
@ -79,11 +73,17 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
span, span,
"inconsistent HirId at `{:?}` for `{:?}`: \ "inconsistent HirId at `{:?}` for `{:?}`: \
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})", current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
self.source_map.span_to_diagnostic_string(span), self.tcx.sess.source_map().span_to_diagnostic_string(span),
node, node,
self.definitions.def_path(self.owner.def_id).to_string_no_crate_verbose(), self.tcx
.definitions_untracked()
.def_path(self.owner.def_id)
.to_string_no_crate_verbose(),
self.owner, self.owner,
self.definitions.def_path(hir_id.owner.def_id).to_string_no_crate_verbose(), self.tcx
.definitions_untracked()
.def_path(hir_id.owner.def_id)
.to_string_no_crate_verbose(),
hir_id.owner, hir_id.owner,
) )
} }

View File

@ -671,8 +671,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
} else { } else {
(None, None) (None, None)
}; };
let (nodes, parenting) = let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies);
index::index_hir(self.tcx.sess, &*self.tcx.definitions_untracked(), node, &bodies);
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies }; let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies };
let attrs = hir::AttributeMap { map: attrs, opt_hash: attrs_hash }; let attrs = hir::AttributeMap { map: attrs, opt_hash: attrs_hash };