Skip to content

HirId-ify intravisit #58232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 66 additions & 67 deletions src/librustc/hir/intravisit.rs

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
@@ -3301,7 +3301,9 @@ impl<'a> LoweringContext<'a> {
let mut path = path.clone();
for seg in path.segments.iter_mut() {
if seg.id.is_some() {
seg.id = Some(self.next_id().node_id);
let next_id = self.next_id();
seg.id = Some(next_id.node_id);
seg.hir_id = Some(next_id.hir_id);
}
}
path
114 changes: 59 additions & 55 deletions src/librustc/hir/map/collector.rs
Original file line number Diff line number Diff line change
@@ -27,9 +27,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
/// The node map
map: Vec<Option<Entry<'hir>>>,
/// The parent of this node
parent_node: NodeId,

parent_hir: hir::HirId,
parent_node: hir::HirId,

// These fields keep track of the currently relevant DepNodes during
// the visitor's traversal.
@@ -40,6 +38,7 @@ pub(super) struct NodeCollector<'a, 'hir> {

dep_graph: &'a DepGraph,
definitions: &'a definitions::Definitions,
hir_to_node_id: &'a FxHashMap<HirId, NodeId>,

hcx: StableHashingContext<'a>,

@@ -100,6 +99,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
krate: &'hir Crate,
dep_graph: &'a DepGraph,
definitions: &'a definitions::Definitions,
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);
@@ -147,14 +147,14 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
krate,
source_map: sess.source_map(),
map: repeat(None).take(sess.current_node_id_count()).collect(),
parent_node: CRATE_NODE_ID,
parent_hir: hir::CRATE_HIR_ID,
parent_node: hir::CRATE_HIR_ID,
current_signature_dep_index: root_mod_sig_dep_index,
current_full_dep_index: root_mod_full_dep_index,
current_dep_node_owner: CRATE_DEF_INDEX,
currently_in_body: false,
dep_graph,
definitions,
hir_to_node_id,
hcx,
hir_body_nodes,
};
@@ -228,10 +228,10 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
self.map[id.as_usize()] = Some(entry);
}

fn insert(&mut self, span: Span, id: NodeId, node: Node<'hir>) {
fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
let entry = Entry {
parent: self.parent_node,
parent_hir: self.parent_hir,
parent: self.hir_to_node_id[&self.parent_node],
parent_hir: self.parent_node,
dep_node: if self.currently_in_body {
self.current_full_dep_index
} else {
@@ -240,21 +240,23 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
node,
};

let node_id = self.hir_to_node_id[&hir_id];

// Make sure that the DepNode of some node coincides with the HirId
// owner of that node.
if cfg!(debug_assertions) {
let hir_id = self.definitions.node_to_hir_id(id);
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(id) {
let node_str = match self.definitions.opt_def_index(node_id) {
Some(def_index) => {
self.definitions.def_path(def_index).to_string_no_crate()
}
None => format!("{:?}", node)
};

let forgot_str = if hir_id == crate::hir::DUMMY_HIR_ID {
format!("\nMaybe you forgot to lower the node id {:?}?", id)
format!("\nMaybe you forgot to lower the node id {:?}?", node_id)
} else {
String::new()
};
@@ -276,12 +278,16 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
}
}

self.insert_entry(id, entry);
self.insert_entry(node_id, entry);
}

fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_id: NodeId, f: F) {
fn with_parent<F: FnOnce(&mut Self)>(
&mut self,
parent_node_id: HirId,
f: F,
) {
let parent_node = self.parent_node;
self.parent_node = parent_id;
self.parent_node = parent_node_id;
f(self);
self.parent_node = parent_node;
}
@@ -352,12 +358,12 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
debug_assert_eq!(i.hir_id.owner,
self.definitions.opt_def_index(i.id).unwrap());
self.with_dep_node_owner(i.hir_id.owner, i, |this| {
this.insert(i.span, i.id, Node::Item(i));
this.with_parent(i.id, |this| {
this.insert(i.span, i.hir_id, Node::Item(i));
this.with_parent(i.hir_id, |this| {
if let ItemKind::Struct(ref struct_def, _) = i.node {
// If this is a tuple-like struct, register the constructor.
if !struct_def.is_struct() {
this.insert(i.span, struct_def.id(), Node::StructCtor(struct_def));
this.insert(i.span, struct_def.hir_id(), Node::StructCtor(struct_def));
}
}
intravisit::walk_item(this, i);
@@ -366,25 +372,25 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
}

fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem) {
self.insert(foreign_item.span, foreign_item.id, Node::ForeignItem(foreign_item));
self.insert(foreign_item.span, foreign_item.hir_id, Node::ForeignItem(foreign_item));

self.with_parent(foreign_item.id, |this| {
self.with_parent(foreign_item.hir_id, |this| {
intravisit::walk_foreign_item(this, foreign_item);
});
}

fn visit_generic_param(&mut self, param: &'hir GenericParam) {
self.insert(param.span, param.id, Node::GenericParam(param));
self.insert(param.span, param.hir_id, Node::GenericParam(param));
intravisit::walk_generic_param(self, param);
}

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

this.with_parent(ti.id, |this| {
this.with_parent(ti.hir_id, |this| {
intravisit::walk_trait_item(this, ti);
});
});
@@ -394,9 +400,9 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
debug_assert_eq!(ii.hir_id.owner,
self.definitions.opt_def_index(ii.id).unwrap());
self.with_dep_node_owner(ii.hir_id.owner, ii, |this| {
this.insert(ii.span, ii.id, Node::ImplItem(ii));
this.insert(ii.span, ii.hir_id, Node::ImplItem(ii));

this.with_parent(ii.id, |this| {
this.with_parent(ii.hir_id, |this| {
intravisit::walk_impl_item(this, ii);
});
});
@@ -408,93 +414,92 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
} else {
Node::Pat(pat)
};
self.insert(pat.span, pat.id, node);
self.insert(pat.span, pat.hir_id, node);

self.with_parent(pat.id, |this| {
self.with_parent(pat.hir_id, |this| {
intravisit::walk_pat(this, pat);
});
}

fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
self.insert(DUMMY_SP, constant.id, Node::AnonConst(constant));
self.insert(DUMMY_SP, constant.hir_id, Node::AnonConst(constant));

self.with_parent(constant.id, |this| {
self.with_parent(constant.hir_id, |this| {
intravisit::walk_anon_const(this, constant);
});
}

fn visit_expr(&mut self, expr: &'hir Expr) {
self.insert(expr.span, expr.id, Node::Expr(expr));
self.insert(expr.span, expr.hir_id, Node::Expr(expr));

self.with_parent(expr.id, |this| {
self.with_parent(expr.hir_id, |this| {
intravisit::walk_expr(this, expr);
});
}

fn visit_stmt(&mut self, stmt: &'hir Stmt) {
let id = stmt.id;
self.insert(stmt.span, id, Node::Stmt(stmt));
self.insert(stmt.span, stmt.hir_id, Node::Stmt(stmt));

self.with_parent(id, |this| {
self.with_parent(stmt.hir_id, |this| {
intravisit::walk_stmt(this, stmt);
});
}

fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment) {
if let Some(id) = path_segment.id {
self.insert(path_span, id, Node::PathSegment(path_segment));
if let Some(hir_id) = path_segment.hir_id {
self.insert(path_span, hir_id, Node::PathSegment(path_segment));
}
intravisit::walk_path_segment(self, path_span, path_segment);
}

fn visit_ty(&mut self, ty: &'hir Ty) {
self.insert(ty.span, ty.id, Node::Ty(ty));
self.insert(ty.span, ty.hir_id, Node::Ty(ty));

self.with_parent(ty.id, |this| {
self.with_parent(ty.hir_id, |this| {
intravisit::walk_ty(this, ty);
});
}

fn visit_trait_ref(&mut self, tr: &'hir TraitRef) {
self.insert(tr.path.span, tr.ref_id, Node::TraitRef(tr));
self.insert(tr.path.span, tr.hir_ref_id, Node::TraitRef(tr));

self.with_parent(tr.ref_id, |this| {
self.with_parent(tr.hir_ref_id, |this| {
intravisit::walk_trait_ref(this, tr);
});
}

fn visit_fn(&mut self, fk: intravisit::FnKind<'hir>, fd: &'hir FnDecl,
b: BodyId, s: Span, id: NodeId) {
b: BodyId, s: Span, id: HirId) {
assert_eq!(self.parent_node, id);
intravisit::walk_fn(self, fk, fd, b, s, id);
}

fn visit_block(&mut self, block: &'hir Block) {
self.insert(block.span, block.id, Node::Block(block));
self.with_parent(block.id, |this| {
self.insert(block.span, block.hir_id, Node::Block(block));
self.with_parent(block.hir_id, |this| {
intravisit::walk_block(this, block);
});
}

fn visit_local(&mut self, l: &'hir Local) {
self.insert(l.span, l.id, Node::Local(l));
self.with_parent(l.id, |this| {
self.insert(l.span, l.hir_id, Node::Local(l));
self.with_parent(l.hir_id, |this| {
intravisit::walk_local(this, l)
})
}

fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
self.insert(lifetime.span, lifetime.id, Node::Lifetime(lifetime));
self.insert(lifetime.span, lifetime.hir_id, Node::Lifetime(lifetime));
}

fn visit_vis(&mut self, visibility: &'hir Visibility) {
match visibility.node {
VisibilityKind::Public |
VisibilityKind::Crate(_) |
VisibilityKind::Inherited => {}
VisibilityKind::Restricted { id, .. } => {
self.insert(visibility.span, id, Node::Visibility(visibility));
self.with_parent(id, |this| {
VisibilityKind::Restricted { hir_id, .. } => {
self.insert(visibility.span, hir_id, Node::Visibility(visibility));
self.with_parent(hir_id, |this| {
intravisit::walk_vis(this, visibility);
});
}
@@ -505,21 +510,20 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
let def_index = self.definitions.opt_def_index(macro_def.id).unwrap();

self.with_dep_node_owner(def_index, macro_def, |this| {
this.insert(macro_def.span, macro_def.id, Node::MacroDef(macro_def));
this.insert(macro_def.span, macro_def.hir_id, Node::MacroDef(macro_def));
});
}

fn visit_variant(&mut self, v: &'hir Variant, g: &'hir Generics, item_id: NodeId) {
let id = v.node.data.id();
self.insert(v.span, id, Node::Variant(v));
self.with_parent(id, |this| {
fn visit_variant(&mut self, v: &'hir Variant, g: &'hir Generics, item_id: HirId) {
self.insert(v.span, v.node.data.hir_id(), Node::Variant(v));
self.with_parent(v.node.data.hir_id(), |this| {
intravisit::walk_variant(this, v, g, item_id);
});
}

fn visit_struct_field(&mut self, field: &'hir StructField) {
self.insert(field.span, field.id, Node::Field(field));
self.with_parent(field.id, |this| {
self.insert(field.span, field.hir_id, Node::Field(field));
self.with_parent(field.hir_id, |this| {
intravisit::walk_struct_field(this, field);
});
}
Loading