Skip to content

Commit

Permalink
rustc: use LocalDefId instead of DefIndex in hir::map::definitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Nov 8, 2019
1 parent 8959646 commit 9c1f88e
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 108 deletions.
13 changes: 6 additions & 7 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ impl<'a> LoweringContext<'a> {
UseTreeKind::Simple(_, id1, id2) => {
for &id in &[id1, id2] {
self.lctx.resolver.definitions().create_def_with_parent(
owner.index,
owner,
id,
DefPathData::Misc,
ExpnId::root(),
Expand Down Expand Up @@ -673,8 +673,7 @@ impl<'a> LoweringContext<'a> {
.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")
.assert_local();
that do not belong to the current owner");

hir::HirId {
owner,
Expand Down Expand Up @@ -815,7 +814,7 @@ impl<'a> LoweringContext<'a> {

// Add a definition for the in-band lifetime def.
self.resolver.definitions().create_def_with_parent(
parent_def_id.index,
parent_def_id,
node_id,
DefPathData::LifetimeNs(str_name),
ExpnId::root(),
Expand Down Expand Up @@ -986,7 +985,7 @@ impl<'a> LoweringContext<'a> {

fn def_key(&mut self, id: DefId) -> DefKey {
if let Some(id) = id.as_local() {
self.resolver.definitions().def_key(id.index)
self.resolver.definitions().def_key(id)
} else {
self.resolver.cstore().def_key(id)
}
Expand Down Expand Up @@ -1121,7 +1120,7 @@ impl<'a> LoweringContext<'a> {
let impl_trait_node_id = self.sess.next_node_id();
let parent_def_id = self.current_hir_id_owner.last().unwrap().0;
self.resolver.definitions().create_def_with_parent(
parent_def_id.index,
parent_def_id,
impl_trait_node_id,
DefPathData::ImplTrait,
ExpnId::root(),
Expand Down Expand Up @@ -1602,7 +1601,7 @@ impl<'a> LoweringContext<'a> {
let hir_id =
self.context.lower_node_id_with_owner(def_node_id, self.opaque_ty_id);
self.context.resolver.definitions().create_def_with_parent(
self.parent.index,
self.parent,
def_node_id,
DefPathData::LifetimeNs(name.ident().name),
ExpnId::root(),
Expand Down
34 changes: 21 additions & 13 deletions src/librustc/hir/map/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
hir_to_node_id: &'a FxHashMap<HirId, NodeId>,
mut hcx: StableHashingContext<'a>)
-> NodeCollector<'a, 'hir> {
let root_mod_def_path_hash = definitions.def_path_hash(CRATE_DEF_INDEX);
let root_mod_def_path_hash = definitions.def_path_hash(LocalDefId {
index: CRATE_DEF_INDEX,
});

let mut hir_body_nodes = Vec::new();

Expand Down Expand Up @@ -254,9 +256,9 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
assert_eq!(self.definitions.node_to_hir_id(node_id), hir_id);

if hir_id.owner != self.current_dep_node_owner {
let node_str = match self.definitions.opt_def_index(node_id) {
Some(def_index) => {
self.definitions.def_path(def_index).to_string_no_crate()
let node_str = match self.definitions.opt_local_def_id(node_id) {
Some(def_id) => {
self.definitions.def_path(def_id).to_string_no_crate()
}
None => format!("{:?}", node)
};
Expand All @@ -274,10 +276,10 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
self.source_map.span_to_string(span),
node_str,
self.definitions
.def_path(self.current_dep_node_owner.index)
.def_path(self.current_dep_node_owner)
.to_string_no_crate(),
self.current_dep_node_owner,
self.definitions.def_path(hir_id.owner.index).to_string_no_crate(),
self.definitions.def_path(hir_id.owner).to_string_no_crate(),
hir_id.owner,
forgot_str,
)
Expand Down Expand Up @@ -308,7 +310,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
let prev_full_dep_index = self.current_full_dep_index;
let prev_in_body = self.currently_in_body;

let def_path_hash = self.definitions.def_path_hash(dep_node_owner.index);
let def_path_hash = self.definitions.def_path_hash(dep_node_owner);

let (signature_dep_index, full_dep_index) = alloc_hir_dep_nodes(
self.dep_graph,
Expand Down Expand Up @@ -369,8 +371,10 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {

fn visit_item(&mut self, i: &'hir Item) {
debug!("visit_item: {:?}", i);
debug_assert_eq!(i.hir_id.owner.index,
self.definitions.opt_def_index(self.hir_to_node_id[&i.hir_id]).unwrap());
debug_assert_eq!(
i.hir_id.owner,
self.definitions.opt_local_def_id(self.hir_to_node_id[&i.hir_id]).unwrap(),
);
self.with_dep_node_owner(i.hir_id.owner, i, |this| {
this.insert(i.span, i.hir_id, Node::Item(i));
this.with_parent(i.hir_id, |this| {
Expand Down Expand Up @@ -399,8 +403,10 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
}

fn visit_trait_item(&mut self, ti: &'hir TraitItem) {
debug_assert_eq!(ti.hir_id.owner.index,
self.definitions.opt_def_index(self.hir_to_node_id[&ti.hir_id]).unwrap());
debug_assert_eq!(
ti.hir_id.owner,
self.definitions.opt_local_def_id(self.hir_to_node_id[&ti.hir_id]).unwrap(),
);
self.with_dep_node_owner(ti.hir_id.owner, ti, |this| {
this.insert(ti.span, ti.hir_id, Node::TraitItem(ti));

Expand All @@ -411,8 +417,10 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
}

fn visit_impl_item(&mut self, ii: &'hir ImplItem) {
debug_assert_eq!(ii.hir_id.owner.index,
self.definitions.opt_def_index(self.hir_to_node_id[&ii.hir_id]).unwrap());
debug_assert_eq!(
ii.hir_id.owner,
self.definitions.opt_local_def_id(self.hir_to_node_id[&ii.hir_id]).unwrap(),
);
self.with_dep_node_owner(ii.hir_id.owner, ii, |this| {
this.insert(ii.span, ii.hir_id, Node::ImplItem(ii));

Expand Down
8 changes: 4 additions & 4 deletions src/librustc/hir/map/def_collector.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::hir::map::definitions::*;
use crate::hir::def_id::DefIndex;
use crate::hir::def_id::LocalDefId;

use syntax::ast::*;
use syntax::visit;
Expand All @@ -11,7 +11,7 @@ use syntax_pos::Span;
/// Creates `DefId`s for nodes in the AST.
pub struct DefCollector<'a> {
definitions: &'a mut Definitions,
parent_def: DefIndex,
parent_def: LocalDefId,
expansion: ExpnId,
}

Expand All @@ -25,13 +25,13 @@ impl<'a> DefCollector<'a> {
node_id: NodeId,
data: DefPathData,
span: Span)
-> DefIndex {
-> LocalDefId {
let parent_def = self.parent_def;
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
self.definitions.create_def_with_parent(parent_def, node_id, data, self.expansion, span)
}

fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: DefIndex, f: F) {
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: LocalDefId, f: F) {
let orig_parent_def = std::mem::replace(&mut self.parent_def, parent_def);
f(self);
self.parent_def = orig_parent_def;
Expand Down
Loading

0 comments on commit 9c1f88e

Please sign in to comment.