rustc: use LocalDefId instead of DefIndex in HirId.

This commit is contained in:
Eduard-Mihai Burtescu 2020-03-18 20:27:59 +02:00
parent 6130b9959c
commit f3ec069886
32 changed files with 138 additions and 201 deletions

View File

@ -477,7 +477,7 @@ impl<'tcx> DepNodeParams<'tcx> for HirId {
fn to_fingerprint(&self, tcx: TyCtxt<'_>) -> Fingerprint {
let HirId { owner, local_id } = *self;
let def_path_hash = tcx.def_path_hash(DefId::local(owner));
let def_path_hash = tcx.def_path_hash(owner.to_def_id());
let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());
def_path_hash.0.combine(local_id)

View File

@ -902,7 +902,7 @@ impl DepGraph {
fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
def_id.index == hir_id.owner
def_id.index == hir_id.owner.local_def_index
}
/// A "work product" is an intermediate result that we save into the

View File

@ -10,7 +10,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::svh::Svh;
use rustc_hir as hir;
use rustc_hir::def_id::CRATE_DEF_INDEX;
use rustc_hir::def_id::{DefIndex, LOCAL_CRATE};
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::*;
use rustc_index::vec::{Idx, IndexVec};
@ -30,12 +30,12 @@ pub(super) struct NodeCollector<'a, 'hir> {
/// Source map
source_map: &'a SourceMap,
map: IndexVec<DefIndex, HirOwnerData<'hir>>,
map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
/// The parent of this node
parent_node: hir::HirId,
current_dep_node_owner: DefIndex,
current_dep_node_owner: LocalDefId,
definitions: &'a definitions::Definitions,
@ -126,7 +126,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
krate,
source_map: sess.source_map(),
parent_node: hir::CRATE_HIR_ID,
current_dep_node_owner: CRATE_DEF_INDEX,
current_dep_node_owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
definitions,
hcx,
hir_body_nodes,
@ -148,7 +148,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
crate_disambiguator: CrateDisambiguator,
cstore: &dyn CrateStore,
commandline_args_hash: u64,
) -> (IndexVec<DefIndex, HirOwnerData<'hir>>, Svh) {
) -> (IndexVec<LocalDefId, HirOwnerData<'hir>>, Svh) {
// Insert bodies into the map
for (id, body) in self.krate.bodies.iter() {
let bodies = &mut self.map[id.hir_id.owner].with_bodies.as_mut().unwrap().bodies;
@ -261,9 +261,11 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?}){}",
self.source_map.span_to_string(span),
node_str,
self.definitions.def_path(self.current_dep_node_owner).to_string_no_crate(),
self.definitions
.def_path(self.current_dep_node_owner.local_def_index)
.to_string_no_crate(),
self.current_dep_node_owner,
self.definitions.def_path(hir_id.owner).to_string_no_crate(),
self.definitions.def_path(hir_id.owner.local_def_index).to_string_no_crate(),
hir_id.owner,
forgot_str,
)
@ -285,13 +287,13 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
F: FnOnce(&mut Self, Fingerprint),
>(
&mut self,
dep_node_owner: DefIndex,
dep_node_owner: LocalDefId,
item_like: &T,
f: F,
) {
let prev_owner = self.current_dep_node_owner;
let def_path_hash = self.definitions.def_path_hash(dep_node_owner);
let def_path_hash = self.definitions.def_path_hash(dep_node_owner.local_def_index);
let hash = hash_body(&mut self.hcx, def_path_hash, item_like, &mut self.hir_body_nodes);
@ -340,7 +342,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
fn visit_item(&mut self, i: &'hir Item<'hir>) {
debug!("visit_item: {:?}", i);
debug_assert_eq!(
i.hir_id.owner,
i.hir_id.owner.local_def_index,
self.definitions.opt_def_index(self.definitions.hir_to_node_id(i.hir_id)).unwrap()
);
self.with_dep_node_owner(i.hir_id.owner, i, |this, hash| {
@ -372,7 +374,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
debug_assert_eq!(
ti.hir_id.owner,
ti.hir_id.owner.local_def_index,
self.definitions.opt_def_index(self.definitions.hir_to_node_id(ti.hir_id)).unwrap()
);
self.with_dep_node_owner(ti.hir_id.owner, ti, |this, hash| {
@ -386,7 +388,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
debug_assert_eq!(
ii.hir_id.owner,
ii.hir_id.owner.local_def_index,
self.definitions.opt_def_index(self.definitions.hir_to_node_id(ii.hir_id)).unwrap()
);
self.with_dep_node_owner(ii.hir_id.owner, ii, |this, hash| {
@ -506,10 +508,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
}
fn visit_macro_def(&mut self, macro_def: &'hir MacroDef<'hir>) {
let node_id = self.definitions.hir_to_node_id(macro_def.hir_id);
let def_index = self.definitions.opt_def_index(node_id).unwrap();
self.with_dep_node_owner(def_index, macro_def, |this, hash| {
self.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this, hash| {
this.insert_with_hash(
macro_def.span,
macro_def.hir_id,

View File

@ -3,7 +3,7 @@ use crate::ty::TyCtxt;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::{par_iter, Lock, ParallelIterator};
use rustc_hir as hir;
use rustc_hir::def_id::{DefIndex, LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::intravisit;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::{HirId, ItemLocalId};
@ -32,7 +32,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
struct HirIdValidator<'a, 'hir> {
hir_map: Map<'hir>,
owner_def_index: Option<DefIndex>,
owner: Option<LocalDefId>,
hir_ids_seen: FxHashSet<ItemLocalId>,
errors: &'a Lock<Vec<String>>,
}
@ -46,7 +46,7 @@ impl<'a, 'hir> OuterVisitor<'a, 'hir> {
fn new_inner_visitor(&self, hir_map: Map<'hir>) -> HirIdValidator<'a, 'hir> {
HirIdValidator {
hir_map,
owner_def_index: None,
owner: None,
hir_ids_seen: Default::default(),
errors: self.errors,
}
@ -78,12 +78,12 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
}
fn check<F: FnOnce(&mut HirIdValidator<'a, 'hir>)>(&mut self, hir_id: HirId, walk: F) {
assert!(self.owner_def_index.is_none());
let owner_def_index = self.hir_map.local_def_id(hir_id).index;
self.owner_def_index = Some(owner_def_index);
assert!(self.owner.is_none());
let owner = self.hir_map.local_def_id(hir_id).expect_local();
self.owner = Some(owner);
walk(self);
if owner_def_index == CRATE_DEF_INDEX {
if owner.local_def_index == CRATE_DEF_INDEX {
return;
}
@ -105,31 +105,26 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
let mut missing_items = Vec::with_capacity(missing.len());
for local_id in missing {
let hir_id =
HirId { owner: owner_def_index, local_id: ItemLocalId::from_u32(local_id) };
let hir_id = HirId { owner, local_id: ItemLocalId::from_u32(local_id) };
trace!("missing hir id {:#?}", hir_id);
missing_items.push(format!(
"[local_id: {}, owner: {}]",
local_id,
self.hir_map
.def_path(LocalDefId { local_def_index: owner_def_index })
.to_string_no_crate()
self.hir_map.def_path(owner).to_string_no_crate()
));
}
self.error(|| {
format!(
"ItemLocalIds not assigned densely in {}. \
Max ItemLocalId = {}, missing IDs = {:?}; seens IDs = {:?}",
self.hir_map
.def_path(LocalDefId { local_def_index: owner_def_index })
.to_string_no_crate(),
self.hir_map.def_path(owner).to_string_no_crate(),
max,
missing_items,
self.hir_ids_seen
.iter()
.map(|&local_id| HirId { owner: owner_def_index, local_id })
.map(|&local_id| HirId { owner, local_id })
.map(|h| format!("({:?} {})", h, self.hir_map.node_to_string(h)))
.collect::<Vec<_>>()
)
@ -146,7 +141,7 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
}
fn visit_id(&mut self, hir_id: HirId) {
let owner = self.owner_def_index.expect("no owner_def_index");
let owner = self.owner.expect("no owner");
if hir_id == hir::DUMMY_HIR_ID {
self.error(|| {
@ -163,10 +158,8 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
format!(
"HirIdValidator: The recorded owner of {} is {} instead of {}",
self.hir_map.node_to_string(hir_id),
self.hir_map.def_path(hir_id.owner_local_def_id()).to_string_no_crate(),
self.hir_map
.def_path(LocalDefId { local_def_index: owner })
.to_string_no_crate()
self.hir_map.def_path(hir_id.owner).to_string_no_crate(),
self.hir_map.def_path(owner).to_string_no_crate()
)
});
}

View File

@ -138,7 +138,7 @@ pub struct IndexedHir<'hir> {
/// The SVH of the local crate.
pub crate_hash: Svh,
pub(super) map: IndexVec<DefIndex, HirOwnerData<'hir>>,
pub(super) map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
}
#[derive(Copy, Clone)]
@ -345,10 +345,10 @@ impl<'hir> Map<'hir> {
fn get_entry(&self, id: HirId) -> Entry<'hir> {
if id.local_id == ItemLocalId::from_u32(0) {
let owner = self.tcx.hir_owner(id.owner_def_id());
let owner = self.tcx.hir_owner(id.owner);
Entry { parent: owner.parent, node: owner.node }
} else {
let owner = self.tcx.hir_owner_items(id.owner_def_id());
let owner = self.tcx.hir_owner_items(id.owner);
let item = owner.items[id.local_id].as_ref().unwrap();
Entry { parent: HirId { owner: id.owner, local_id: item.parent }, node: item.node }
}
@ -376,11 +376,7 @@ impl<'hir> Map<'hir> {
}
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
self.tcx
.hir_owner_items(DefId::local(id.hir_id.owner))
.bodies
.get(&id.hir_id.local_id)
.unwrap()
self.tcx.hir_owner_items(id.hir_id.owner).bodies.get(&id.hir_id.local_id).unwrap()
}
pub fn fn_decl_by_hir_id(&self, hir_id: HirId) -> Option<&'hir FnDecl<'hir>> {
@ -494,7 +490,7 @@ impl<'hir> Map<'hir> {
where
V: ItemLikeVisitor<'hir>,
{
let module = self.tcx.hir_module_items(module);
let module = self.tcx.hir_module_items(module.expect_local());
for id in &module.items {
visitor.visit_item(self.expect_item(*id));

View File

@ -11,7 +11,7 @@ use crate::ty::TyCtxt;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
use rustc_hir::Body;
use rustc_hir::HirId;
use rustc_hir::ItemLocalId;
@ -60,27 +60,27 @@ impl<'tcx> TyCtxt<'tcx> {
map::Map { tcx: self }
}
pub fn parent_module(self, id: HirId) -> DefId {
self.parent_module_from_def_id(DefId::local(id.owner))
pub fn parent_module(self, id: HirId) -> LocalDefId {
self.parent_module_from_def_id(id.owner)
}
}
pub fn provide(providers: &mut Providers<'_>) {
providers.parent_module_from_def_id = |tcx, id| {
let hir = tcx.hir();
hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id).unwrap()))
hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id.to_def_id()).unwrap()))
.expect_local()
};
providers.hir_crate = |tcx, _| tcx.untracked_crate;
providers.index_hir = map::index_hir;
providers.hir_module_items = |tcx, id| {
assert_eq!(id.krate, LOCAL_CRATE);
let hir = tcx.hir();
let module = hir.as_local_hir_id(id).unwrap();
let module = hir.as_local_hir_id(id.to_def_id()).unwrap();
&tcx.untracked_crate.modules[&module]
};
providers.hir_owner = |tcx, id| tcx.index_hir(id.krate).map[id.index].signature.unwrap();
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature.unwrap();
providers.hir_owner_items = |tcx, id| {
tcx.index_hir(id.krate).map[id.index].with_bodies.as_ref().map(|items| &**items).unwrap()
tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_ref().map(|items| &**items).unwrap()
};
map::provide(providers);
}

View File

@ -21,7 +21,7 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
NodeIdHashingMode::HashDefPath => {
let hir::HirId { owner, local_id } = hir_id;
hcx.local_def_path_hash(owner).hash_stable(hcx, hasher);
hcx.local_def_path_hash(owner.local_def_index).hash_stable(hcx, hasher);
local_id.hash_stable(hcx, hasher);
}
}
@ -231,7 +231,7 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::TraitCandidate {
let import_keys = import_ids
.iter()
.map(|hir_id| (hcx.local_def_path_hash(hir_id.owner), hir_id.local_id))
.map(|hir_id| (hcx.local_def_path_hash(hir_id.owner.local_def_index), hir_id.local_id))
.collect();
(hcx.def_path_hash(*def_id), import_keys)
}

View File

@ -9,7 +9,6 @@
use crate::ich::{NodeIdHashingMode, StableHashingContext};
use crate::ty::{self, DefIdTree, TyCtxt};
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::Node;
use rustc_data_structures::fx::FxHashMap;
@ -594,7 +593,7 @@ impl<'tcx> ScopeTree {
region scope tree for {:?} / {:?}",
param_owner,
self.root_parent.map(|id| tcx.hir().local_def_id(id)),
self.root_body.map(|hir_id| DefId::local(hir_id.owner))
self.root_body.map(|hir_id| hir_id.owner)
),
);
}

View File

@ -66,24 +66,27 @@ rustc_queries! {
// The items in a module.
// This can be conveniently accessed by `tcx.hir().visit_item_likes_in_module`.
// Avoid calling this query directly.
query hir_module_items(key: DefId) -> &'tcx hir::ModuleItems {
query hir_module_items(key: LocalDefId) -> &'tcx hir::ModuleItems {
eval_always
desc { |tcx| "HIR module items in `{}`", tcx.def_path_str(key.to_def_id()) }
}
// An HIR item with a `DefId` that can own other HIR items which do not themselves have
// a `DefId`.
// An HIR item with a `LocalDefId` that can own other HIR items which do
// not themselves have a `LocalDefId`.
// This can be conveniently accessed by methods on `tcx.hir()`.
// Avoid calling this query directly.
query hir_owner(key: DefId) -> &'tcx HirOwner<'tcx> {
query hir_owner(key: LocalDefId) -> &'tcx HirOwner<'tcx> {
eval_always
desc { |tcx| "HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) }
}
// The HIR items which do not themselves have a `DefId` and are owned by another HIR item
// with a `DefId`.
// The HIR items which do not themselves have a `LocalDefId` and are
// owned by another HIR item with a `LocalDefId`.
// This can be conveniently accessed by methods on `tcx.hir()`.
// Avoid calling this query directly.
query hir_owner_items(key: DefId) -> &'tcx HirOwnerItems<'tcx> {
query hir_owner_items(key: LocalDefId) -> &'tcx HirOwnerItems<'tcx> {
eval_always
desc { |tcx| "HIR owner items in `{}`", tcx.def_path_str(key.to_def_id()) }
}
/// Records the type of every item.
@ -135,8 +138,9 @@ rustc_queries! {
desc { "computing the lint levels for items in this crate" }
}
query parent_module_from_def_id(_: DefId) -> DefId {
query parent_module_from_def_id(key: LocalDefId) -> LocalDefId {
eval_always
desc { |tcx| "parent module of `{}`", tcx.def_path_str(key.to_def_id()) }
}
}

View File

@ -205,13 +205,13 @@ fn validate_hir_id_for_typeck_tables(
mut_access: bool,
) {
if let Some(local_id_root) = local_id_root {
if hir_id.owner != local_id_root.index {
if hir_id.owner.to_def_id() != local_id_root {
ty::tls::with(|tcx| {
bug!(
"node {} with HirId::owner {:?} cannot be placed in \
TypeckTables with local_id_root {:?}",
tcx.hir().node_to_string(hir_id),
DefId::local(hir_id.owner),
hir_id.owner,
local_id_root
)
});
@ -732,8 +732,10 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for TypeckTables<'tcx> {
let local_id_root = local_id_root.expect("trying to hash invalid TypeckTables");
let var_owner_def_id =
DefId { krate: local_id_root.krate, index: var_path.hir_id.owner };
let var_owner_def_id = DefId {
krate: local_id_root.krate,
index: var_path.hir_id.owner.local_def_index,
};
let closure_def_id =
DefId { krate: local_id_root.krate, index: closure_expr_id.local_def_index };
(
@ -1153,7 +1155,7 @@ impl<'tcx> TyCtxt<'tcx> {
let mut trait_map: FxHashMap<_, FxHashMap<_, _>> = FxHashMap::default();
for (k, v) in resolutions.trait_map {
let hir_id = definitions.node_to_hir_id(k);
let map = trait_map.entry(hir_id.owner_local_def_id()).or_default();
let map = trait_map.entry(hir_id.owner).or_default();
let v = v
.into_iter()
.map(|tc| tc.map_import_ids(|id| definitions.node_to_hir_id(id)))
@ -2635,22 +2637,19 @@ impl<'tcx> TyCtxt<'tcx> {
}
pub fn in_scope_traits(self, id: HirId) -> Option<&'tcx StableVec<TraitCandidate>> {
self.in_scope_traits_map(id.owner_local_def_id()).and_then(|map| map.get(&id.local_id))
self.in_scope_traits_map(id.owner).and_then(|map| map.get(&id.local_id))
}
pub fn named_region(self, id: HirId) -> Option<resolve_lifetime::Region> {
self.named_region_map(id.owner_local_def_id())
.and_then(|map| map.get(&id.local_id).cloned())
self.named_region_map(id.owner).and_then(|map| map.get(&id.local_id).cloned())
}
pub fn is_late_bound(self, id: HirId) -> bool {
self.is_late_bound_map(id.owner_local_def_id())
.map(|set| set.contains(&id.local_id))
.unwrap_or(false)
self.is_late_bound_map(id.owner).map(|set| set.contains(&id.local_id)).unwrap_or(false)
}
pub fn object_lifetime_defaults(self, id: HirId) -> Option<&'tcx [ObjectLifetimeDefault]> {
self.object_lifetime_defaults_map(id.owner_local_def_id())
self.object_lifetime_defaults_map(id.owner)
.and_then(|map| map.get(&id.local_id).map(|v| &**v))
}
}

View File

@ -384,7 +384,9 @@ impl Visibility {
Res::Err => Visibility::Public,
def => Visibility::Restricted(def.def_id()),
},
hir::VisibilityKind::Inherited => Visibility::Restricted(tcx.parent_module(id)),
hir::VisibilityKind::Inherited => {
Visibility::Restricted(tcx.parent_module(id).to_def_id())
}
}
}
@ -3117,7 +3119,7 @@ impl<'tcx> TyCtxt<'tcx> {
Some(actual_expansion) => {
self.hir().definitions().parent_module_of_macro_def(actual_expansion)
}
None => self.parent_module(block),
None => self.parent_module(block).to_def_id(),
};
(ident, scope)
}

View File

@ -11,7 +11,6 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, Once};
use rustc_data_structures::thin_vec::ThinVec;
use rustc_errors::Diagnostic;
use rustc_hir as hir;
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE};
use rustc_index::vec::{Idx, IndexVec};
use rustc_serialize::{
@ -661,25 +660,6 @@ impl<'a, 'tcx> SpecializedDecoder<LocalDefId> for CacheDecoder<'a, 'tcx> {
}
}
impl<'a, 'tcx> SpecializedDecoder<hir::HirId> for CacheDecoder<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<hir::HirId, Self::Error> {
// Load the `DefPathHash` which is what we encoded the `DefIndex` as.
let def_path_hash = DefPathHash::decode(self)?;
// Use the `DefPathHash` to map to the current `DefId`.
let def_id = self.tcx().def_path_hash_to_def_id.as_ref().unwrap()[&def_path_hash];
debug_assert!(def_id.is_local());
// The `ItemLocalId` needs no remapping.
let local_id = hir::ItemLocalId::decode(self)?;
// Reconstruct the `HirId` and look up the corresponding `NodeId` in the
// context of the current session.
Ok(hir::HirId { owner: def_id.index, local_id })
}
}
impl<'a, 'tcx> SpecializedDecoder<Fingerprint> for CacheDecoder<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<Fingerprint, Self::Error> {
Fingerprint::decode_opaque(&mut self.opaque)
@ -873,21 +853,6 @@ where
}
}
impl<'a, 'tcx, E> SpecializedEncoder<hir::HirId> for CacheEncoder<'a, 'tcx, E>
where
E: 'a + TyEncoder,
{
#[inline]
fn specialized_encode(&mut self, id: &hir::HirId) -> Result<(), Self::Error> {
let hir::HirId { owner, local_id } = *id;
let def_path_hash = self.tcx.hir().definitions().def_path_hash(owner);
def_path_hash.encode(self)?;
local_id.encode(self)
}
}
impl<'a, 'tcx, E> SpecializedEncoder<DefId> for CacheEncoder<'a, 'tcx, E>
where
E: 'a + TyEncoder,

View File

@ -425,10 +425,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
UseTreeKind::Nested(ref trees) => {
for &(ref use_tree, id) in trees {
let hir_id = self.lctx.allocate_hir_id_counter(id);
self.allocate_use_tree_hir_id_counters(
use_tree,
hir_id.owner_local_def_id(),
);
self.allocate_use_tree_hir_id_counters(use_tree, hir_id.owner);
}
}
}
@ -479,10 +476,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.lctx.type_def_lifetime_params.insert(def_id.to_def_id(), count);
}
ItemKind::Use(ref use_tree) => {
self.allocate_use_tree_hir_id_counters(
use_tree,
hir_id.owner_local_def_id(),
);
self.allocate_use_tree_hir_id_counters(use_tree, hir_id.owner);
}
_ => {}
}
@ -626,14 +620,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
/// properly. Calling the method twice with the same `NodeId` is fine though.
fn lower_node_id(&mut self, ast_node_id: NodeId) -> hir::HirId {
self.lower_node_id_generic(ast_node_id, |this| {
let &mut (def_id, ref mut local_id_counter) =
let &mut (owner, ref mut local_id_counter) =
this.current_hir_id_owner.last_mut().unwrap();
let local_id = *local_id_counter;
*local_id_counter += 1;
hir::HirId {
owner: def_id.local_def_index,
local_id: hir::ItemLocalId::from_u32(local_id),
}
hir::HirId { owner, local_id: hir::ItemLocalId::from_u32(local_id) }
})
}
@ -651,12 +642,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
debug_assert!(local_id != HIR_ID_COUNTER_LOCKED);
*local_id_counter += 1;
let def_index = this.resolver.definitions().opt_def_index(owner).expect(
"you forgot to call `create_def_with_parent` or are lowering node-IDs \
that do not belong to the current owner",
);
let owner = this
.resolver
.definitions()
.opt_local_def_id(owner)
.expect(
"you forgot to call `create_def_with_parent` or are lowering node-IDs \
that do not belong to the current owner",
)
.expect_local();
hir::HirId { owner: def_index, local_id: hir::ItemLocalId::from_u32(local_id) }
hir::HirId { owner, local_id: hir::ItemLocalId::from_u32(local_id) }
})
}

View File

@ -1,9 +1,8 @@
use crate::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
use rustc_serialize::{self, Decodable, Decoder, Encodable, Encoder};
use crate::def_id::{LocalDefId, CRATE_DEF_INDEX};
use std::fmt;
/// Uniquely identifies a node in the HIR of the current crate. It is
/// composed of the `owner`, which is the `DefIndex` of the directly enclosing
/// composed of the `owner`, which is the `LocalDefId` of the directly enclosing
/// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"),
/// and the `local_id` which is unique within the given owner.
///
@ -12,41 +11,12 @@ use std::fmt;
/// the `local_id` part of the `HirId` changing, which is a very useful property in
/// incremental compilation where we have to persist things through changes to
/// the code base.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, RustcEncodable, RustcDecodable)]
pub struct HirId {
pub owner: DefIndex,
pub owner: LocalDefId,
pub local_id: ItemLocalId,
}
impl HirId {
pub fn owner_def_id(self) -> DefId {
DefId::local(self.owner)
}
pub fn owner_local_def_id(self) -> LocalDefId {
LocalDefId { local_def_index: self.owner }
}
}
impl rustc_serialize::UseSpecializedEncodable for HirId {
fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
let HirId { owner, local_id } = *self;
owner.encode(s)?;
local_id.encode(s)?;
Ok(())
}
}
impl rustc_serialize::UseSpecializedDecodable for HirId {
fn default_decode<D: Decoder>(d: &mut D) -> Result<HirId, D::Error> {
let owner = DefIndex::decode(d)?;
let local_id = ItemLocalId::decode(d)?;
Ok(HirId { owner, local_id })
}
}
impl fmt::Display for HirId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
@ -70,9 +40,12 @@ rustc_index::newtype_index! {
rustc_data_structures::impl_stable_hash_via_hash!(ItemLocalId);
/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`.
pub const CRATE_HIR_ID: HirId =
HirId { owner: CRATE_DEF_INDEX, local_id: ItemLocalId::from_u32(0) };
pub const CRATE_HIR_ID: HirId = HirId {
owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
local_id: ItemLocalId::from_u32(0),
};
pub const DUMMY_HIR_ID: HirId = HirId { owner: CRATE_DEF_INDEX, local_id: DUMMY_ITEM_LOCAL_ID };
pub const DUMMY_HIR_ID: HirId =
HirId { owner: LocalDefId { local_def_index: CRATE_DEF_INDEX }, local_id: DUMMY_ITEM_LOCAL_ID };
pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;

View File

@ -29,7 +29,7 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
#[inline]
fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
let def_path_hash = hcx.local_def_path_hash(self.owner);
let def_path_hash = hcx.local_def_path_hash(self.owner.local_def_index);
(def_path_hash, self.local_id)
}
}

View File

@ -140,7 +140,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind {
}
}
TyKind::Rptr(_, MutTy { ty: inner_ty, mutbl: Mutability::Not }) => {
if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner_def_id()) {
if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner.to_def_id()) {
if cx.tcx.impl_trait_ref(impl_did).is_some() {
return;
}

View File

@ -126,7 +126,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
) -> bool {
if ty.is_unit()
|| cx.tcx.is_ty_uninhabited_from(
cx.tcx.parent_module(expr.hir_id),
cx.tcx.parent_module(expr.hir_id).to_def_id(),
ty,
cx.param_env,
)

View File

@ -1350,7 +1350,7 @@ impl EncodeContext<'tcx> {
let is_proc_macro = self.tcx.sess.crate_types.borrow().contains(&CrateType::ProcMacro);
if is_proc_macro {
let tcx = self.tcx;
Some(self.lazy(tcx.hir().krate().proc_macros.iter().map(|p| p.owner)))
Some(self.lazy(tcx.hir().krate().proc_macros.iter().map(|p| p.owner.local_def_index)))
} else {
None
}

View File

@ -776,7 +776,7 @@ fn for_each_late_bound_region_defined_on<'tcx>(
) {
if let Some(late_bounds) = tcx.is_late_bound_map(fn_def_id.expect_local()) {
for late_bound in late_bounds.iter() {
let hir_id = HirId { owner: fn_def_id.index, local_id: *late_bound };
let hir_id = HirId { owner: fn_def_id.expect_local(), local_id: *late_bound };
let name = tcx.hir().name(hir_id);
let region_def_id = tcx.hir().local_def_id(hir_id);
let liberated_region = tcx.mk_region(ty::ReFree(ty::FreeRegion {

View File

@ -145,7 +145,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
fn check_in_cx(&self, hir_id: HirId, f: impl FnOnce(MatchCheckCtxt<'_, 'tcx>)) {
let module = self.tcx.parent_module(hir_id);
MatchCheckCtxt::create_and_enter(self.tcx, self.param_env, module, |cx| f(cx));
MatchCheckCtxt::create_and_enter(self.tcx, self.param_env, module.to_def_id(), |cx| f(cx));
}
fn check_match(

View File

@ -1128,7 +1128,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
}
hir::ExprKind::Call(ref f, ref args) => {
let m = self.ir.tcx.parent_module(expr.hir_id);
let m = self.ir.tcx.parent_module(expr.hir_id).to_def_id();
let succ = if self.ir.tcx.is_ty_uninhabited_from(
m,
self.tables.expr_ty(expr),
@ -1143,7 +1143,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
}
hir::ExprKind::MethodCall(.., ref args) => {
let m = self.ir.tcx.parent_module(expr.hir_id);
let m = self.ir.tcx.parent_module(expr.hir_id).to_def_id();
let succ = if self.ir.tcx.is_ty_uninhabited_from(
m,
self.tables.expr_ty(expr),

View File

@ -46,7 +46,7 @@ fn method_might_be_inlined(
impl_item: &hir::ImplItem<'_>,
impl_src: DefId,
) -> bool {
let codegen_fn_attrs = tcx.codegen_fn_attrs(impl_item.hir_id.owner_def_id());
let codegen_fn_attrs = tcx.codegen_fn_attrs(impl_item.hir_id.owner.to_def_id());
let generics = tcx.generics_of(tcx.hir().local_def_id(impl_item.hir_id));
if codegen_fn_attrs.requests_inline() || generics.requires_monomorphization(tcx) {
return true;

View File

@ -327,7 +327,7 @@ fn def_id_visibility<'tcx>(
}
Node::Expr(expr) => {
return (
ty::Visibility::Restricted(tcx.parent_module(expr.hir_id)),
ty::Visibility::Restricted(tcx.parent_module(expr.hir_id).to_def_id()),
expr.span,
"private",
);

View File

@ -302,15 +302,15 @@ fn resolve_lifetimes(tcx: TyCtxt<'_>, for_krate: CrateNum) -> &ResolveLifetimes
let mut rl = ResolveLifetimes::default();
for (hir_id, v) in named_region_map.defs {
let map = rl.defs.entry(hir_id.owner_local_def_id()).or_default();
let map = rl.defs.entry(hir_id.owner).or_default();
map.insert(hir_id.local_id, v);
}
for hir_id in named_region_map.late_bound {
let map = rl.late_bound.entry(hir_id.owner_local_def_id()).or_default();
let map = rl.late_bound.entry(hir_id.owner).or_default();
map.insert(hir_id.local_id);
}
for (hir_id, v) in named_region_map.object_lifetime_defaults {
let map = rl.object_lifetime_defaults.entry(hir_id.owner_local_def_id()).or_default();
let map = rl.object_lifetime_defaults.entry(hir_id.owner).or_default();
map.insert(hir_id.local_id, v);
}

View File

@ -215,11 +215,22 @@ rustc_data_structures::define_id_collections!(DefIdMap, DefIdSet, DefId);
/// few cases where we know that only DefIds from the local crate are expected
/// and a DefId from a different crate would signify a bug somewhere. This
/// is when LocalDefId comes in handy.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LocalDefId {
pub local_def_index: DefIndex,
}
impl Idx for LocalDefId {
#[inline]
fn new(idx: usize) -> Self {
LocalDefId { local_def_index: Idx::new(idx) }
}
#[inline]
fn index(self) -> usize {
self.local_def_index.index()
}
}
impl LocalDefId {
#[inline]
pub fn to_def_id(self) -> DefId {

View File

@ -1287,7 +1287,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
let parent = hir.get_parent_node(expr_id);
if let Some(hir::Node::Expr(e)) = hir.find(parent) {
let parent_span = hir.span(parent);
let parent_did = parent.owner_def_id();
let parent_did = parent.owner.to_def_id();
// ```rust
// impl T {
// fn foo(&self) -> i32 {}

View File

@ -185,7 +185,7 @@ fn get_sized_bounds(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SmallVec<[Span; 1]>
.filter_map(|pred| {
match pred {
hir::WherePredicate::BoundPredicate(pred)
if pred.bounded_ty.hir_id.owner_def_id() == trait_def_id =>
if pred.bounded_ty.hir_id.owner.to_def_id() == trait_def_id =>
{
// Fetch spans for trait bounds that are Sized:
// `trait T where Self: Pred`

View File

@ -1631,7 +1631,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
fn point_at_param_definition(&self, err: &mut DiagnosticBuilder<'_>, param: ty::ParamTy) {
let generics = self.tcx.generics_of(self.body_id.owner_def_id());
let generics = self.tcx.generics_of(self.body_id.owner.to_def_id());
let generic_param = generics.type_param(&param, self.tcx);
if let ty::GenericParamDefKind::Type { synthetic: Some(..), .. } = generic_param.kind {
return;

View File

@ -427,7 +427,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
});
if let Some((field, field_ty)) = field_receiver {
let scope = self.tcx.parent_module(self.body_id);
let scope = self.tcx.parent_module(self.body_id).to_def_id();
let is_accessible = field.vis.is_accessible_from(scope, self.tcx);
if is_accessible {
@ -828,7 +828,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
candidates: Vec<DefId>,
) {
let module_did = self.tcx.parent_module(self.body_id);
let module_id = self.tcx.hir().as_local_hir_id(module_did).unwrap();
let module_id = self.tcx.hir().as_local_hir_id(module_did.to_def_id()).unwrap();
let krate = self.tcx.hir().krate();
let (span, found_use) = UsePlacementFinder::check(self.tcx, krate, module_id);
if let Some(span) = span {

View File

@ -639,7 +639,7 @@ pub struct InheritedBuilder<'tcx> {
impl Inherited<'_, 'tcx> {
pub fn build(tcx: TyCtxt<'tcx>, def_id: DefId) -> InheritedBuilder<'tcx> {
let hir_id_root = if let Some(def_id) = def_id.as_local() {
tcx.hir().local_def_id_to_hir_id(def_id).owner_def_id()
tcx.hir().local_def_id_to_hir_id(def_id).owner.to_def_id()
} else {
def_id
};
@ -1127,7 +1127,7 @@ fn typeck_tables_of_with_fallback<'tcx>(
// Consistency check our TypeckTables instance can hold all ItemLocalIds
// it will need to hold.
assert_eq!(tables.local_id_root, Some(DefId::local(id.owner)));
assert_eq!(tables.local_id_root, Some(id.owner.to_def_id()));
tables
}

View File

@ -9,7 +9,7 @@ use rustc::ty::fold::{TypeFoldable, TypeFolder};
use rustc::ty::{self, Ty, TyCtxt};
use rustc_data_structures::sync::Lrc;
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, DefIdSet};
use rustc_hir::def_id::DefIdSet;
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282;
use rustc_infer::infer::InferCtxt;
@ -107,11 +107,11 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
body: &'tcx hir::Body<'tcx>,
rustc_dump_user_substs: bool,
) -> WritebackCx<'cx, 'tcx> {
let owner = body.id().hir_id;
let owner = body.id().hir_id.owner;
WritebackCx {
fcx,
tables: ty::TypeckTables::empty(Some(DefId::local(owner.owner))),
tables: ty::TypeckTables::empty(Some(owner.to_def_id())),
body,
rustc_dump_user_substs,
}
@ -342,7 +342,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
let common_local_id_root = fcx_tables.local_id_root.unwrap();
for (&id, &origin) in fcx_tables.closure_kind_origins().iter() {
let hir_id = hir::HirId { owner: common_local_id_root.index, local_id: id };
let hir_id = hir::HirId { owner: common_local_id_root.expect_local(), local_id: id };
self.tables.closure_kind_origins_mut().insert(hir_id, origin);
}
}
@ -364,7 +364,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
let mut errors_buffer = Vec::new();
for (&local_id, c_ty) in fcx_tables.user_provided_types().iter() {
let hir_id = hir::HirId { owner: common_local_id_root.index, local_id };
let hir_id = hir::HirId { owner: common_local_id_root.expect_local(), local_id };
if cfg!(debug_assertions) && c_ty.has_local_value() {
span_bug!(hir_id.to_span(self.fcx.tcx), "writeback: `{:?}` is a local value", c_ty);
@ -557,7 +557,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
let common_local_id_root = fcx_tables.local_id_root.unwrap();
for (&local_id, fn_sig) in fcx_tables.liberated_fn_sigs().iter() {
let hir_id = hir::HirId { owner: common_local_id_root.index, local_id };
let hir_id = hir::HirId { owner: common_local_id_root.expect_local(), local_id };
let fn_sig = self.resolve(fn_sig, &hir_id);
self.tables.liberated_fn_sigs_mut().insert(hir_id, fn_sig.clone());
}
@ -569,7 +569,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
let common_local_id_root = fcx_tables.local_id_root.unwrap();
for (&local_id, ftys) in fcx_tables.fru_field_types().iter() {
let hir_id = hir::HirId { owner: common_local_id_root.index, local_id };
let hir_id = hir::HirId { owner: common_local_id_root.expect_local(), local_id };
let ftys = self.resolve(ftys, &hir_id);
self.tables.fru_field_types_mut().insert(hir_id, ftys);
}

View File

@ -348,7 +348,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
let parent_node = self.cx.as_local_hir_id(item.def_id).and_then(|hir_id| {
// FIXME: this fails hard for impls in non-module scope, but is necessary for the
// current `resolve()` implementation.
match self.cx.as_local_hir_id(self.cx.tcx.parent_module(hir_id)).unwrap() {
match self.cx.as_local_hir_id(self.cx.tcx.parent_module(hir_id).to_def_id()).unwrap() {
id if id != hir_id => Some(id),
_ => None,
}