Store def_id_to_hir_id as variant in hir_owner.
If hir_owner is Owner(_), the LocalDefId is pointing to an owner, so the ItemLocalId is 0. If the HIR node does not exist, we store Phantom. Otherwise, we store the HirId associated to the LocalDefId.
This commit is contained in:
parent
009c1d0248
commit
a0bcce4884
11 changed files with 123 additions and 112 deletions
|
@ -7,7 +7,6 @@
|
|||
pub use crate::def_id::DefPathHash;
|
||||
use crate::def_id::{CrateNum, DefIndex, LocalDefId, StableCrateId, CRATE_DEF_INDEX, LOCAL_CRATE};
|
||||
use crate::def_path_hash_map::DefPathHashMap;
|
||||
use crate::hir;
|
||||
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_data_structures::stable_hasher::StableHasher;
|
||||
|
@ -101,13 +100,6 @@ impl DefPathTable {
|
|||
pub struct Definitions {
|
||||
table: DefPathTable,
|
||||
|
||||
/// Only [`LocalDefId`]s for items and item-like are HIR owners.
|
||||
/// The associated `HirId` has a `local_id` of `0`.
|
||||
/// Generic parameters and closures are also assigned a `LocalDefId` but are not HIR owners.
|
||||
/// Their `HirId`s are defined by their position while lowering the enclosing owner.
|
||||
// FIXME(cjgillot) Some `LocalDefId`s from `use` items are dropped during lowering and lack a `HirId`.
|
||||
pub(super) def_id_to_hir_id: IndexVec<LocalDefId, Option<hir::HirId>>,
|
||||
|
||||
/// Item with a given `LocalDefId` was defined during macro expansion with ID `ExpnId`.
|
||||
expansions_that_defined: FxHashMap<LocalDefId, ExpnId>,
|
||||
|
||||
|
@ -322,12 +314,6 @@ impl Definitions {
|
|||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[track_caller]
|
||||
pub fn local_def_id_to_hir_id(&self, id: LocalDefId) -> hir::HirId {
|
||||
self.def_id_to_hir_id[id].unwrap()
|
||||
}
|
||||
|
||||
/// Adds a root definition (no parent) and a few other reserved definitions.
|
||||
pub fn new(stable_crate_id: StableCrateId, crate_span: Span) -> Definitions {
|
||||
let key = DefKey {
|
||||
|
@ -354,7 +340,6 @@ impl Definitions {
|
|||
|
||||
Definitions {
|
||||
table,
|
||||
def_id_to_hir_id: Default::default(),
|
||||
expansions_that_defined: Default::default(),
|
||||
def_id_to_span,
|
||||
stable_crate_id,
|
||||
|
@ -406,20 +391,6 @@ impl Definitions {
|
|||
def_id
|
||||
}
|
||||
|
||||
/// Initializes the `LocalDefId` to `HirId` mapping once it has been generated during
|
||||
/// AST to HIR lowering.
|
||||
pub fn init_def_id_to_hir_id_mapping(
|
||||
&mut self,
|
||||
mapping: IndexVec<LocalDefId, Option<hir::HirId>>,
|
||||
) {
|
||||
assert!(
|
||||
self.def_id_to_hir_id.is_empty(),
|
||||
"trying to initialize `LocalDefId` <-> `HirId` mappings twice"
|
||||
);
|
||||
|
||||
self.def_id_to_hir_id = mapping;
|
||||
}
|
||||
|
||||
pub fn expansion_that_defined(&self, id: LocalDefId) -> ExpnId {
|
||||
self.expansions_that_defined.get(&id).copied().unwrap_or_else(ExpnId::root)
|
||||
}
|
||||
|
@ -431,7 +402,7 @@ impl Definitions {
|
|||
}
|
||||
|
||||
pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
|
||||
self.def_id_to_hir_id.iter_enumerated().map(|(k, _)| k)
|
||||
self.table.def_path_hashes.indices().map(|local_def_index| LocalDefId { local_def_index })
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
|
|
@ -711,6 +711,15 @@ pub struct OwnerNodes<'tcx> {
|
|||
pub local_id_to_def_id: SortedMap<ItemLocalId, LocalDefId>,
|
||||
}
|
||||
|
||||
impl<'tcx> OwnerNodes<'tcx> {
|
||||
pub fn node(&self) -> OwnerNode<'tcx> {
|
||||
use rustc_index::vec::Idx;
|
||||
let node = self.nodes[ItemLocalId::new(0)].as_ref().unwrap().node;
|
||||
let node = node.as_owner().unwrap(); // Indexing must ensure it is an OwnerNode.
|
||||
node
|
||||
}
|
||||
}
|
||||
|
||||
/// Full information resulting from lowering an AST node.
|
||||
#[derive(Debug, HashStable_Generic)]
|
||||
pub struct OwnerInfo<'hir> {
|
||||
|
@ -728,10 +737,39 @@ pub struct OwnerInfo<'hir> {
|
|||
impl<'tcx> OwnerInfo<'tcx> {
|
||||
#[inline]
|
||||
pub fn node(&self) -> OwnerNode<'tcx> {
|
||||
use rustc_index::vec::Idx;
|
||||
let node = self.nodes.nodes[ItemLocalId::new(0)].as_ref().unwrap().node;
|
||||
let node = node.as_owner().unwrap(); // Indexing must ensure it is an OwnerNode.
|
||||
node
|
||||
self.nodes.node()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, HashStable_Generic)]
|
||||
pub enum MaybeOwner<T> {
|
||||
Owner(T),
|
||||
NonOwner(HirId),
|
||||
/// Used as a placeholder for unused LocalDefId.
|
||||
Phantom,
|
||||
}
|
||||
|
||||
impl<T> MaybeOwner<T> {
|
||||
pub fn as_owner(self) -> Option<T> {
|
||||
match self {
|
||||
MaybeOwner::Owner(i) => Some(i),
|
||||
MaybeOwner::NonOwner(_) | MaybeOwner::Phantom => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> MaybeOwner<U> {
|
||||
match self {
|
||||
MaybeOwner::Owner(i) => MaybeOwner::Owner(f(i)),
|
||||
MaybeOwner::NonOwner(hir_id) => MaybeOwner::NonOwner(hir_id),
|
||||
MaybeOwner::Phantom => MaybeOwner::Phantom,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unwrap(self) -> T {
|
||||
match self {
|
||||
MaybeOwner::Owner(i) => i,
|
||||
MaybeOwner::NonOwner(_) | MaybeOwner::Phantom => panic!("Not a HIR owner"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -743,7 +781,7 @@ impl<'tcx> OwnerInfo<'tcx> {
|
|||
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
|
||||
#[derive(Debug)]
|
||||
pub struct Crate<'hir> {
|
||||
pub owners: IndexVec<LocalDefId, Option<OwnerInfo<'hir>>>,
|
||||
pub owners: IndexVec<LocalDefId, MaybeOwner<&'hir OwnerInfo<'hir>>>,
|
||||
pub hir_hash: Fingerprint,
|
||||
}
|
||||
|
||||
|
|
|
@ -30,6 +30,11 @@ impl HirId {
|
|||
if self.local_id.index() == 0 { Some(self.owner) } else { None }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_owner(self) -> bool {
|
||||
self.local_id.index() == 0
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn make_owner(owner: LocalDefId) -> Self {
|
||||
Self { owner, local_id: ItemLocalId::from_u32(0) }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue