incr.comp.: Cache Hir-DepNodeIndices in the HIR map.

This commit is contained in:
Michael Woerister 2017-08-18 20:24:19 +02:00
parent 942711e115
commit 8d7c59b1fa
8 changed files with 341 additions and 228 deletions

View File

@ -244,8 +244,8 @@ impl DepGraphEdges {
self.edges.insert((source, target));
}
pub fn add_node(&mut self, node: DepNode) {
self.get_or_create_node(node);
pub fn add_node(&mut self, node: DepNode) -> DepNodeIndex {
self.get_or_create_node(node)
}
#[inline]

View File

@ -166,6 +166,14 @@ impl DepGraph {
self.data.as_ref().unwrap().edges.borrow_mut().add_node(node);
}
pub fn alloc_input_node(&self, node: DepNode) -> DepNodeIndex {
if let Some(ref data) = self.data {
data.edges.borrow_mut().add_node(node)
} else {
DepNodeIndex::INVALID
}
}
/// Indicates that a previous work product exists for `v`. This is
/// invoked during initial start-up based on what nodes are clean
/// (and what files exist in the incr. directory).

View File

@ -835,7 +835,16 @@ pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref:
pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
// NB: Deliberately force a compilation error if/when new fields are added.
let ImplItem { id: _, name, ref vis, ref defaultness, ref attrs, ref node, span } = *impl_item;
let ImplItem {
id: _,
hir_id: _,
name,
ref vis,
ref defaultness,
ref attrs,
ref node,
span
} = *impl_item;
visitor.visit_name(span, name);
visitor.visit_vis(vis);

View File

@ -1268,7 +1268,12 @@ impl<'a> LoweringContext<'a> {
path.span = span;
self.allocate_hir_id_counter(import.id, import);
self.with_hir_id_owner(import.id, |this| {
let LoweredNodeId {
node_id: import_node_id,
hir_id: import_hir_id,
} = self.lower_node_id(import.id);
self.with_hir_id_owner(import_node_id, |this| {
let vis = match *vis {
hir::Visibility::Public => hir::Visibility::Public,
hir::Visibility::Crate => hir::Visibility::Crate,
@ -1282,8 +1287,9 @@ impl<'a> LoweringContext<'a> {
}
};
this.items.insert(import.id, hir::Item {
id: import.id,
this.items.insert(import_node_id, hir::Item {
id: import_node_id,
hir_id: import_hir_id,
name: import.rename.unwrap_or(ident).name,
attrs: attrs.clone(),
node: hir::ItemUse(P(path), hir::UseKind::Single),
@ -1414,8 +1420,11 @@ impl<'a> LoweringContext<'a> {
fn lower_trait_item(&mut self, i: &TraitItem) -> hir::TraitItem {
self.with_parent_def(i.id, |this| {
let LoweredNodeId { node_id, hir_id } = this.lower_node_id(i.id);
hir::TraitItem {
id: this.lower_node_id(i.id).node_id,
id: node_id,
hir_id,
name: this.lower_ident(i.ident),
attrs: this.lower_attrs(&i.attrs),
node: match i.node {
@ -1475,8 +1484,11 @@ impl<'a> LoweringContext<'a> {
fn lower_impl_item(&mut self, i: &ImplItem) -> hir::ImplItem {
self.with_parent_def(i.id, |this| {
let LoweredNodeId { node_id, hir_id } = this.lower_node_id(i.id);
hir::ImplItem {
id: this.lower_node_id(i.id).node_id,
id: node_id,
hir_id,
name: this.lower_ident(i.ident),
attrs: this.lower_attrs(&i.attrs),
vis: this.lower_visibility(&i.vis, None),
@ -1567,8 +1579,11 @@ impl<'a> LoweringContext<'a> {
this.lower_item_kind(i.id, &mut name, &attrs, &mut vis, &i.node)
});
let LoweredNodeId { node_id, hir_id } = self.lower_node_id(i.id);
Some(hir::Item {
id: self.lower_node_id(i.id).node_id,
id: node_id,
hir_id,
name,
attrs,
node,

View File

@ -10,33 +10,55 @@
use super::*;
use dep_graph::{DepGraph, DepKind, DepNodeIndex};
use hir::intravisit::{Visitor, NestedVisitorMap};
use std::iter::repeat;
use syntax::ast::{NodeId, CRATE_NODE_ID};
use syntax_pos::Span;
/// A Visitor that walks over the HIR and collects Nodes into a HIR map
pub struct NodeCollector<'hir> {
pub(super) struct NodeCollector<'a, 'hir> {
/// The crate
pub krate: &'hir Crate,
krate: &'hir Crate,
/// The node map
pub(super) map: Vec<MapEntry<'hir>>,
map: Vec<MapEntry<'hir>>,
/// The parent of this node
pub parent_node: NodeId,
parent_node: NodeId,
current_dep_node_owner: DefIndex,
current_dep_node_index: DepNodeIndex,
dep_graph: &'a DepGraph,
definitions: &'a definitions::Definitions,
}
impl<'hir> NodeCollector<'hir> {
pub fn root(krate: &'hir Crate) -> NodeCollector<'hir> {
impl<'a, 'hir> NodeCollector<'a, 'hir> {
pub(super) fn root(krate: &'hir Crate,
dep_graph: &'a DepGraph,
definitions: &'a definitions::Definitions)
-> NodeCollector<'a, 'hir> {
let root_mod_def_path_hash = definitions.def_path_hash(CRATE_DEF_INDEX);
let root_mod_dep_node = root_mod_def_path_hash.to_dep_node(DepKind::Hir);
let root_mod_dep_node_index = dep_graph.alloc_input_node(root_mod_dep_node);
let mut collector = NodeCollector {
krate,
map: vec![],
parent_node: CRATE_NODE_ID,
current_dep_node_index: root_mod_dep_node_index,
current_dep_node_owner: CRATE_DEF_INDEX,
dep_graph,
definitions,
};
collector.insert_entry(CRATE_NODE_ID, RootCrate);
collector.insert_entry(CRATE_NODE_ID, RootCrate(root_mod_dep_node_index));
collector
}
pub(super) fn into_map(self) -> Vec<MapEntry<'hir>> {
self.map
}
fn insert_entry(&mut self, id: NodeId, entry: MapEntry<'hir>) {
debug!("hir_map: {:?} => {:?}", id, entry);
let len = self.map.len();
@ -47,8 +69,55 @@ impl<'hir> NodeCollector<'hir> {
}
fn insert(&mut self, id: NodeId, node: Node<'hir>) {
let entry = MapEntry::from_node(self.parent_node, node);
let parent = self.parent_node;
let dep_node_index = self.current_dep_node_index;
let entry = match node {
NodeItem(n) => EntryItem(parent, dep_node_index, n),
NodeForeignItem(n) => EntryForeignItem(parent, dep_node_index, n),
NodeTraitItem(n) => EntryTraitItem(parent, dep_node_index, n),
NodeImplItem(n) => EntryImplItem(parent, dep_node_index, n),
NodeVariant(n) => EntryVariant(parent, dep_node_index, n),
NodeField(n) => EntryField(parent, dep_node_index, n),
NodeExpr(n) => EntryExpr(parent, dep_node_index, n),
NodeStmt(n) => EntryStmt(parent, dep_node_index, n),
NodeTy(n) => EntryTy(parent, dep_node_index, n),
NodeTraitRef(n) => EntryTraitRef(parent, dep_node_index, n),
NodeBinding(n) => EntryBinding(parent, dep_node_index, n),
NodePat(n) => EntryPat(parent, dep_node_index, n),
NodeBlock(n) => EntryBlock(parent, dep_node_index, n),
NodeStructCtor(n) => EntryStructCtor(parent, dep_node_index, n),
NodeLifetime(n) => EntryLifetime(parent, dep_node_index, n),
NodeTyParam(n) => EntryTyParam(parent, dep_node_index, n),
NodeVisibility(n) => EntryVisibility(parent, dep_node_index, n),
NodeLocal(n) => EntryLocal(parent, dep_node_index, n),
};
// Make sure that the DepNode of some node coincides with the HirId
// owner of that node.
if cfg!(debug_assertions) {
let hir_id_owner = self.definitions.node_to_hir_id(id).owner;
if hir_id_owner != self.current_dep_node_owner {
let node_str = match self.definitions.opt_def_index(id) {
Some(def_index) => {
self.definitions.def_path(def_index).to_string_no_crate()
}
None => format!("{:?}", node)
};
bug!("inconsistent DepNode for `{}`: \
current_dep_node_owner={}, hir_id.owner={}",
node_str,
self.definitions
.def_path(self.current_dep_node_owner)
.to_string_no_crate(),
self.definitions.def_path(hir_id_owner).to_string_no_crate())
}
}
self.insert_entry(id, entry);
}
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_id: NodeId, f: F) {
@ -57,9 +126,27 @@ impl<'hir> NodeCollector<'hir> {
f(self);
self.parent_node = parent_node;
}
fn with_dep_node_owner<F: FnOnce(&mut Self)>(&mut self,
dep_node_owner: DefIndex,
f: F) {
let prev_owner = self.current_dep_node_owner;
let prev_index = self.current_dep_node_index;
// When we enter a new owner (item, impl item, or trait item), we always
// start out again with DepKind::Hir.
let new_dep_node = self.definitions
.def_path_hash(dep_node_owner)
.to_dep_node(DepKind::Hir);
self.current_dep_node_index = self.dep_graph.alloc_input_node(new_dep_node);
self.current_dep_node_owner = dep_node_owner;
f(self);
self.current_dep_node_index = prev_index;
self.current_dep_node_owner = prev_owner;
}
}
impl<'hir> Visitor<'hir> for NodeCollector<'hir> {
impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
/// Because we want to track parent items and so forth, enable
/// deep walking so that we walk nested items in the context of
/// their outer items.
@ -82,25 +169,37 @@ impl<'hir> Visitor<'hir> for NodeCollector<'hir> {
}
fn visit_nested_body(&mut self, id: BodyId) {
// When we enter a body, we switch to DepKind::HirBody.
// Note that current_dep_node_index might already be DepKind::HirBody,
// e.g. when entering the body of a closure that is already part of a
// surrounding body. That's expected and not a problem.
let prev_index = self.current_dep_node_index;
let new_dep_node = self.definitions
.def_path_hash(self.current_dep_node_owner)
.to_dep_node(DepKind::HirBody);
self.current_dep_node_index = self.dep_graph.alloc_input_node(new_dep_node);
self.visit_body(self.krate.body(id));
self.current_dep_node_index = prev_index;
}
fn visit_item(&mut self, i: &'hir Item) {
debug!("visit_item: {:?}", i);
self.insert(i.id, NodeItem(i));
self.with_parent(i.id, |this| {
match i.node {
ItemStruct(ref struct_def, _) => {
// If this is a tuple-like struct, register the constructor.
if !struct_def.is_struct() {
this.insert(struct_def.id(), NodeStructCtor(struct_def));
debug_assert_eq!(i.hir_id.owner,
self.definitions.opt_def_index(i.id).unwrap());
self.with_dep_node_owner(i.hir_id.owner, |this| {
this.insert(i.id, NodeItem(i));
this.with_parent(i.id, |this| {
match i.node {
ItemStruct(ref struct_def, _) => {
// If this is a tuple-like struct, register the constructor.
if !struct_def.is_struct() {
this.insert(struct_def.id(), NodeStructCtor(struct_def));
}
}
_ => {}
}
_ => {}
}
intravisit::walk_item(this, i);
intravisit::walk_item(this, i);
});
});
}
@ -121,18 +220,26 @@ impl<'hir> Visitor<'hir> for NodeCollector<'hir> {
}
fn visit_trait_item(&mut self, ti: &'hir TraitItem) {
self.insert(ti.id, NodeTraitItem(ti));
debug_assert_eq!(ti.hir_id.owner,
self.definitions.opt_def_index(ti.id).unwrap());
self.with_dep_node_owner(ti.hir_id.owner, |this| {
this.insert(ti.id, NodeTraitItem(ti));
self.with_parent(ti.id, |this| {
intravisit::walk_trait_item(this, ti);
this.with_parent(ti.id, |this| {
intravisit::walk_trait_item(this, ti);
});
});
}
fn visit_impl_item(&mut self, ii: &'hir ImplItem) {
self.insert(ii.id, NodeImplItem(ii));
debug_assert_eq!(ii.hir_id.owner,
self.definitions.opt_def_index(ii.id).unwrap());
self.with_dep_node_owner(ii.hir_id.owner, |this| {
this.insert(ii.id, NodeImplItem(ii));
self.with_parent(ii.id, |this| {
intravisit::walk_impl_item(this, ii);
this.with_parent(ii.id, |this| {
intravisit::walk_impl_item(this, ii);
});
});
}
@ -238,4 +345,33 @@ impl<'hir> Visitor<'hir> for NodeCollector<'hir> {
intravisit::walk_struct_field(this, field);
});
}
fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
// Do not visit the duplicate information in TraitItemRef. We want to
// map the actual nodes, not the duplicate ones in the *Ref.
let TraitItemRef {
id,
name: _,
kind: _,
span: _,
defaultness: _,
} = *ii;
self.visit_nested_trait_item(id);
}
fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
// Do not visit the duplicate information in ImplItemRef. We want to
// map the actual nodes, not the duplicate ones in the *Ref.
let ImplItemRef {
id,
name: _,
kind: _,
span: _,
vis: _,
defaultness: _,
} = *ii;
self.visit_nested_impl_item(id);
}
}

View File

@ -15,7 +15,7 @@ pub use self::def_collector::{DefCollector, MacroInvocationData};
pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData,
DisambiguatedDefPathData, DefPathHash};
use dep_graph::{DepGraph, DepNode, DepKind};
use dep_graph::{DepGraph, DepNode, DepKind, DepNodeIndex};
use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndexAddressSpace};
@ -74,27 +74,28 @@ enum MapEntry<'hir> {
NotPresent,
/// All the node types, with a parent ID.
EntryItem(NodeId, &'hir Item),
EntryForeignItem(NodeId, &'hir ForeignItem),
EntryTraitItem(NodeId, &'hir TraitItem),
EntryImplItem(NodeId, &'hir ImplItem),
EntryVariant(NodeId, &'hir Variant),
EntryField(NodeId, &'hir StructField),
EntryExpr(NodeId, &'hir Expr),
EntryStmt(NodeId, &'hir Stmt),
EntryTy(NodeId, &'hir Ty),
EntryTraitRef(NodeId, &'hir TraitRef),
EntryBinding(NodeId, &'hir Pat),
EntryPat(NodeId, &'hir Pat),
EntryBlock(NodeId, &'hir Block),
EntryStructCtor(NodeId, &'hir VariantData),
EntryLifetime(NodeId, &'hir Lifetime),
EntryTyParam(NodeId, &'hir TyParam),
EntryVisibility(NodeId, &'hir Visibility),
EntryLocal(NodeId, &'hir Local),
EntryItem(NodeId, DepNodeIndex, &'hir Item),
EntryForeignItem(NodeId, DepNodeIndex, &'hir ForeignItem),
EntryTraitItem(NodeId, DepNodeIndex, &'hir TraitItem),
EntryImplItem(NodeId, DepNodeIndex, &'hir ImplItem),
EntryVariant(NodeId, DepNodeIndex, &'hir Variant),
EntryField(NodeId, DepNodeIndex, &'hir StructField),
EntryExpr(NodeId, DepNodeIndex, &'hir Expr),
EntryStmt(NodeId, DepNodeIndex, &'hir Stmt),
EntryTy(NodeId, DepNodeIndex, &'hir Ty),
EntryTraitRef(NodeId, DepNodeIndex, &'hir TraitRef),
EntryBinding(NodeId, DepNodeIndex, &'hir Pat),
EntryPat(NodeId, DepNodeIndex, &'hir Pat),
EntryBlock(NodeId, DepNodeIndex, &'hir Block),
EntryStructCtor(NodeId, DepNodeIndex, &'hir VariantData),
EntryLifetime(NodeId, DepNodeIndex, &'hir Lifetime),
EntryTyParam(NodeId, DepNodeIndex, &'hir TyParam),
EntryVisibility(NodeId, DepNodeIndex, &'hir Visibility),
EntryLocal(NodeId, DepNodeIndex, &'hir Local),
/// Roots for node trees.
RootCrate,
/// Roots for node trees. The DepNodeIndex is the dependency node of the
/// crate's root module.
RootCrate(DepNodeIndex),
}
impl<'hir> Clone for MapEntry<'hir> {
@ -104,82 +105,59 @@ impl<'hir> Clone for MapEntry<'hir> {
}
impl<'hir> MapEntry<'hir> {
fn from_node(p: NodeId, node: Node<'hir>) -> MapEntry<'hir> {
match node {
NodeItem(n) => EntryItem(p, n),
NodeForeignItem(n) => EntryForeignItem(p, n),
NodeTraitItem(n) => EntryTraitItem(p, n),
NodeImplItem(n) => EntryImplItem(p, n),
NodeVariant(n) => EntryVariant(p, n),
NodeField(n) => EntryField(p, n),
NodeExpr(n) => EntryExpr(p, n),
NodeStmt(n) => EntryStmt(p, n),
NodeTy(n) => EntryTy(p, n),
NodeTraitRef(n) => EntryTraitRef(p, n),
NodeBinding(n) => EntryBinding(p, n),
NodePat(n) => EntryPat(p, n),
NodeBlock(n) => EntryBlock(p, n),
NodeStructCtor(n) => EntryStructCtor(p, n),
NodeLifetime(n) => EntryLifetime(p, n),
NodeTyParam(n) => EntryTyParam(p, n),
NodeVisibility(n) => EntryVisibility(p, n),
NodeLocal(n) => EntryLocal(p, n),
}
}
fn parent_node(self) -> Option<NodeId> {
Some(match self {
EntryItem(id, _) => id,
EntryForeignItem(id, _) => id,
EntryTraitItem(id, _) => id,
EntryImplItem(id, _) => id,
EntryVariant(id, _) => id,
EntryField(id, _) => id,
EntryExpr(id, _) => id,
EntryStmt(id, _) => id,
EntryTy(id, _) => id,
EntryTraitRef(id, _) => id,
EntryBinding(id, _) => id,
EntryPat(id, _) => id,
EntryBlock(id, _) => id,
EntryStructCtor(id, _) => id,
EntryLifetime(id, _) => id,
EntryTyParam(id, _) => id,
EntryVisibility(id, _) => id,
EntryLocal(id, _) => id,
EntryItem(id, _, _) => id,
EntryForeignItem(id, _, _) => id,
EntryTraitItem(id, _, _) => id,
EntryImplItem(id, _, _) => id,
EntryVariant(id, _, _) => id,
EntryField(id, _, _) => id,
EntryExpr(id, _, _) => id,
EntryStmt(id, _, _) => id,
EntryTy(id, _, _) => id,
EntryTraitRef(id, _, _) => id,
EntryBinding(id, _, _) => id,
EntryPat(id, _, _) => id,
EntryBlock(id, _, _) => id,
EntryStructCtor(id, _, _) => id,
EntryLifetime(id, _, _) => id,
EntryTyParam(id, _, _) => id,
EntryVisibility(id, _, _) => id,
EntryLocal(id, _, _) => id,
NotPresent |
RootCrate => return None,
RootCrate(_) => return None,
})
}
fn to_node(self) -> Option<Node<'hir>> {
Some(match self {
EntryItem(_, n) => NodeItem(n),
EntryForeignItem(_, n) => NodeForeignItem(n),
EntryTraitItem(_, n) => NodeTraitItem(n),
EntryImplItem(_, n) => NodeImplItem(n),
EntryVariant(_, n) => NodeVariant(n),
EntryField(_, n) => NodeField(n),
EntryExpr(_, n) => NodeExpr(n),
EntryStmt(_, n) => NodeStmt(n),
EntryTy(_, n) => NodeTy(n),
EntryTraitRef(_, n) => NodeTraitRef(n),
EntryBinding(_, n) => NodeBinding(n),
EntryPat(_, n) => NodePat(n),
EntryBlock(_, n) => NodeBlock(n),
EntryStructCtor(_, n) => NodeStructCtor(n),
EntryLifetime(_, n) => NodeLifetime(n),
EntryTyParam(_, n) => NodeTyParam(n),
EntryVisibility(_, n) => NodeVisibility(n),
EntryLocal(_, n) => NodeLocal(n),
EntryItem(_, _, n) => NodeItem(n),
EntryForeignItem(_, _, n) => NodeForeignItem(n),
EntryTraitItem(_, _, n) => NodeTraitItem(n),
EntryImplItem(_, _, n) => NodeImplItem(n),
EntryVariant(_, _, n) => NodeVariant(n),
EntryField(_, _, n) => NodeField(n),
EntryExpr(_, _, n) => NodeExpr(n),
EntryStmt(_, _, n) => NodeStmt(n),
EntryTy(_, _, n) => NodeTy(n),
EntryTraitRef(_, _, n) => NodeTraitRef(n),
EntryBinding(_, _, n) => NodeBinding(n),
EntryPat(_, _, n) => NodePat(n),
EntryBlock(_, _, n) => NodeBlock(n),
EntryStructCtor(_, _, n) => NodeStructCtor(n),
EntryLifetime(_, _, n) => NodeLifetime(n),
EntryTyParam(_, _, n) => NodeTyParam(n),
EntryVisibility(_, _, n) => NodeVisibility(n),
EntryLocal(_, _, n) => NodeLocal(n),
_ => return None
})
}
fn associated_body(self) -> Option<BodyId> {
match self {
EntryItem(_, item) => {
EntryItem(_, _, item) => {
match item.node {
ItemConst(_, body) |
ItemStatic(.., body) |
@ -188,7 +166,7 @@ impl<'hir> MapEntry<'hir> {
}
}
EntryTraitItem(_, item) => {
EntryTraitItem(_, _, item) => {
match item.node {
TraitItemKind::Const(_, Some(body)) |
TraitItemKind::Method(_, TraitMethod::Provided(body)) => Some(body),
@ -196,7 +174,7 @@ impl<'hir> MapEntry<'hir> {
}
}
EntryImplItem(_, item) => {
EntryImplItem(_, _, item) => {
match item.node {
ImplItemKind::Const(_, body) |
ImplItemKind::Method(_, body) => Some(body),
@ -204,7 +182,7 @@ impl<'hir> MapEntry<'hir> {
}
}
EntryExpr(_, expr) => {
EntryExpr(_, _, expr) => {
match expr.node {
ExprClosure(.., body, _) => Some(body),
_ => None,
@ -282,83 +260,40 @@ impl<'hir> Map<'hir> {
/// read recorded). If the function just returns a DefId or
/// NodeId, no actual content was returned, so no read is needed.
pub fn read(&self, id: NodeId) {
self.dep_graph.read(self.dep_node(id));
}
fn dep_node(&self, id0: NodeId) -> DepNode {
let mut id = id0;
let mut last_expr = None;
loop {
let entry = self.map[id.as_usize()];
match entry {
EntryItem(..) |
EntryTraitItem(..) |
EntryImplItem(..) => {
let def_index = self.definitions.opt_def_index(id).unwrap();
let entry = self.map[id.as_usize()];
match entry {
EntryItem(_, dep_node_index, _) |
EntryTraitItem(_, dep_node_index, _) |
EntryImplItem(_, dep_node_index, _) |
EntryVariant(_, dep_node_index, _) |
EntryForeignItem(_, dep_node_index, _) |
EntryField(_, dep_node_index, _) |
EntryStmt(_, dep_node_index, _) |
EntryTy(_, dep_node_index, _) |
EntryTraitRef(_, dep_node_index, _) |
EntryBinding(_, dep_node_index, _) |
EntryPat(_, dep_node_index, _) |
EntryBlock(_, dep_node_index, _) |
EntryStructCtor(_, dep_node_index, _) |
EntryLifetime(_, dep_node_index, _) |
EntryTyParam(_, dep_node_index, _) |
EntryVisibility(_, dep_node_index, _) |
EntryExpr(_, dep_node_index, _) |
EntryLocal(_, dep_node_index, _) |
RootCrate(dep_node_index) => {
self.dep_graph.read_index(dep_node_index);
}
NotPresent => {
// Some nodes, notably macro definitions, are not
// present in the map for whatever reason, but
// they *do* have def-ids. So if we encounter an
// empty hole, check for that case.
if let Some(def_index) = self.definitions.opt_def_index(id) {
let def_path_hash = self.definitions.def_path_hash(def_index);
if let Some(last_id) = last_expr {
// The body may have a separate dep node
if entry.is_body_owner(last_id) {
return def_path_hash.to_dep_node(DepKind::HirBody);
}
}
return def_path_hash.to_dep_node(DepKind::Hir);
self.dep_graph.read(def_path_hash.to_dep_node(DepKind::Hir));
} else {
bug!("called HirMap::read() with invalid NodeId")
}
EntryVariant(p, v) => {
id = p;
if last_expr.is_some() {
if v.node.disr_expr.map(|e| e.node_id) == last_expr {
// The enum parent holds both Hir and HirBody nodes.
let def_index = self.definitions.opt_def_index(id).unwrap();
let def_path_hash = self.definitions.def_path_hash(def_index);
return def_path_hash.to_dep_node(DepKind::HirBody);
}
}
}
EntryForeignItem(p, _) |
EntryField(p, _) |
EntryStmt(p, _) |
EntryTy(p, _) |
EntryTraitRef(p, _) |
EntryBinding(p, _) |
EntryPat(p, _) |
EntryBlock(p, _) |
EntryStructCtor(p, _) |
EntryLifetime(p, _) |
EntryTyParam(p, _) |
EntryVisibility(p, _) |
EntryLocal(p, _) =>
id = p,
EntryExpr(p, _) => {
last_expr = Some(id);
id = p;
}
RootCrate => {
let def_path_hash = self.definitions.def_path_hash(CRATE_DEF_INDEX);
return def_path_hash.to_dep_node(DepKind::Hir);
}
NotPresent =>
// Some nodes, notably macro definitions, are not
// present in the map for whatever reason, but
// they *do* have def-ids. So if we encounter an
// empty hole, check for that case.
return self.definitions.opt_def_index(id)
.map(|def_index| {
let def_path_hash = self.definitions.def_path_hash(def_index);
def_path_hash.to_dep_node(DepKind::Hir)
})
.unwrap_or_else(|| {
bug!("Walking parents from `{}` \
led to `NotPresent` at `{}`",
id0, id)
}),
}
}
}
@ -481,7 +416,7 @@ impl<'hir> Map<'hir> {
Some(body_id)
} else {
// For some expressions, the expression is its own body.
if let EntryExpr(_, expr) = entry {
if let EntryExpr(_, _, expr) = entry {
Some(BodyId { node_id: expr.id })
} else {
None
@ -760,7 +695,7 @@ impl<'hir> Map<'hir> {
pub fn get_foreign_abi(&self, id: NodeId) -> Abi {
let parent = self.get_parent(id);
let abi = match self.find_entry(parent) {
Some(EntryItem(_, i)) => {
Some(EntryItem(_, _, i)) => {
match i.node {
ItemForeignMod(ref nm) => Some(nm.abi),
_ => None
@ -911,27 +846,27 @@ impl<'hir> Map<'hir> {
pub fn span(&self, id: NodeId) -> Span {
self.read(id); // reveals span from node
match self.find_entry(id) {
Some(EntryItem(_, item)) => item.span,
Some(EntryForeignItem(_, foreign_item)) => foreign_item.span,
Some(EntryTraitItem(_, trait_method)) => trait_method.span,
Some(EntryImplItem(_, impl_item)) => impl_item.span,
Some(EntryVariant(_, variant)) => variant.span,
Some(EntryField(_, field)) => field.span,
Some(EntryExpr(_, expr)) => expr.span,
Some(EntryStmt(_, stmt)) => stmt.span,
Some(EntryTy(_, ty)) => ty.span,
Some(EntryTraitRef(_, tr)) => tr.path.span,
Some(EntryBinding(_, pat)) => pat.span,
Some(EntryPat(_, pat)) => pat.span,
Some(EntryBlock(_, block)) => block.span,
Some(EntryStructCtor(_, _)) => self.expect_item(self.get_parent(id)).span,
Some(EntryLifetime(_, lifetime)) => lifetime.span,
Some(EntryTyParam(_, ty_param)) => ty_param.span,
Some(EntryVisibility(_, &Visibility::Restricted { ref path, .. })) => path.span,
Some(EntryVisibility(_, v)) => bug!("unexpected Visibility {:?}", v),
Some(EntryLocal(_, local)) => local.span,
Some(EntryItem(_, _, item)) => item.span,
Some(EntryForeignItem(_, _, foreign_item)) => foreign_item.span,
Some(EntryTraitItem(_, _, trait_method)) => trait_method.span,
Some(EntryImplItem(_, _, impl_item)) => impl_item.span,
Some(EntryVariant(_, _, variant)) => variant.span,
Some(EntryField(_, _, field)) => field.span,
Some(EntryExpr(_, _, expr)) => expr.span,
Some(EntryStmt(_, _, stmt)) => stmt.span,
Some(EntryTy(_, _, ty)) => ty.span,
Some(EntryTraitRef(_, _, tr)) => tr.path.span,
Some(EntryBinding(_, _, pat)) => pat.span,
Some(EntryPat(_, _, pat)) => pat.span,
Some(EntryBlock(_, _, block)) => block.span,
Some(EntryStructCtor(_, _, _)) => self.expect_item(self.get_parent(id)).span,
Some(EntryLifetime(_, _, lifetime)) => lifetime.span,
Some(EntryTyParam(_, _, ty_param)) => ty_param.span,
Some(EntryVisibility(_, _, &Visibility::Restricted { ref path, .. })) => path.span,
Some(EntryVisibility(_, _, v)) => bug!("unexpected Visibility {:?}", v),
Some(EntryLocal(_, _, local)) => local.span,
Some(RootCrate) => self.forest.krate.span,
Some(RootCrate(_)) => self.forest.krate.span,
Some(NotPresent) | None => {
bug!("hir::map::Map::span: id not in map: {:?}", id)
}
@ -1029,12 +964,12 @@ impl<'a, 'hir> Iterator for NodesMatchingSuffix<'a, 'hir> {
}
self.idx = NodeId::from_u32(self.idx.as_u32() + 1);
let name = match self.map.find_entry(idx) {
Some(EntryItem(_, n)) => n.name(),
Some(EntryForeignItem(_, n))=> n.name(),
Some(EntryTraitItem(_, n)) => n.name(),
Some(EntryImplItem(_, n)) => n.name(),
Some(EntryVariant(_, n)) => n.name(),
Some(EntryField(_, n)) => n.name(),
Some(EntryItem(_, _, n)) => n.name(),
Some(EntryForeignItem(_, _, n))=> n.name(),
Some(EntryTraitItem(_, _, n)) => n.name(),
Some(EntryImplItem(_, _, n)) => n.name(),
Some(EntryVariant(_, _, n)) => n.name(),
Some(EntryField(_, _, n)) => n.name(),
_ => continue,
};
if self.matches_names(self.map.get_parent(idx), name) {
@ -1060,9 +995,13 @@ impl Named for ImplItem { fn name(&self) -> Name { self.name } }
pub fn map_crate<'hir>(forest: &'hir mut Forest,
definitions: Definitions)
-> Map<'hir> {
let mut collector = NodeCollector::root(&forest.krate);
intravisit::walk_crate(&mut collector, &forest.krate);
let map = collector.map;
let map = {
let mut collector = NodeCollector::root(&forest.krate,
&forest.dep_graph,
&definitions);
intravisit::walk_crate(&mut collector, &forest.krate);
collector.into_map()
};
if log_enabled!(::log::LogLevel::Debug) {
// This only makes sense for ordered stores; note the

View File

@ -1265,6 +1265,7 @@ pub struct TraitItemId {
pub struct TraitItem {
pub id: NodeId,
pub name: Name,
pub hir_id: HirId,
pub attrs: HirVec<Attribute>,
pub node: TraitItemKind,
pub span: Span,
@ -1306,6 +1307,7 @@ pub struct ImplItemId {
pub struct ImplItem {
pub id: NodeId,
pub name: Name,
pub hir_id: HirId,
pub vis: Visibility,
pub defaultness: Defaultness,
pub attrs: HirVec<Attribute>,
@ -1712,8 +1714,9 @@ pub struct ItemId {
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct Item {
pub name: Name,
pub attrs: HirVec<Attribute>,
pub id: NodeId,
pub hir_id: HirId,
pub attrs: HirVec<Attribute>,
pub node: Item_,
pub vis: Visibility,
pub span: Span,

View File

@ -741,6 +741,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::T
hasher: &mut StableHasher<W>) {
let hir::TraitItem {
id,
hir_id: _,
name,
ref attrs,
ref node,
@ -774,6 +775,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::I
hasher: &mut StableHasher<W>) {
let hir::ImplItem {
id,
hir_id: _,
name,
ref vis,
defaultness,
@ -930,6 +932,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::I
name,
ref attrs,
id,
hir_id: _,
ref node,
ref vis,
span