Skip to content

Commit

Permalink
rustc: use LocalDefId instead of DefIndex in HirId.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Mar 19, 2020
1 parent 6130b99 commit f3ec069
Show file tree
Hide file tree
Showing 32 changed files with 139 additions and 202 deletions.
2 changes: 1 addition & 1 deletion src/librustc/dep_graph/dep_node.rs
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/dep_graph/graph.rs
Expand Up @@ -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
Expand Down
31 changes: 15 additions & 16 deletions src/librustc/hir/map/collector.rs
Expand Up @@ -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};
Expand All @@ -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,

Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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,
)
Expand All @@ -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);

Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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| {
Expand All @@ -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| {
Expand Down Expand Up @@ -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,
Expand Down
35 changes: 14 additions & 21 deletions src/librustc/hir/map/hir_id_validator.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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>>,
}
Expand All @@ -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,
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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<_>>()
)
Expand All @@ -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(|| {
Expand All @@ -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()
)
});
}
Expand Down
14 changes: 5 additions & 9 deletions src/librustc/hir/map/mod.rs
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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>> {
Expand Down Expand Up @@ -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));
Expand Down
16 changes: 8 additions & 8 deletions src/librustc/hir/mod.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions src/librustc/ich/impls_hir.rs
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/middle/region.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
),
);
}
Expand Down
20 changes: 12 additions & 8 deletions src/librustc/query/mod.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -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()) }
}
}

Expand Down

0 comments on commit f3ec069

Please sign in to comment.