Skip to content

Commit 6ebc6d8

Browse files
committed
rustc: track hir::{TraitRef, Visibility} in the HIR map.
1 parent 16b5c2c commit 6ebc6d8

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

src/librustc/hir/map/collector.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,6 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
124124
this.insert(struct_def.id(), NodeStructCtor(struct_def));
125125
}
126126
}
127-
ItemTrait(.., ref bounds, _) => {
128-
for b in bounds.iter() {
129-
if let TraitTyParamBound(ref t, TraitBoundModifier::None) = *b {
130-
this.insert(t.trait_ref.ref_id, NodeItem(i));
131-
}
132-
}
133-
}
134127
ItemUse(ref view_path) => {
135128
match view_path.node {
136129
ViewPathList(_, ref paths) => {
@@ -217,6 +210,14 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
217210
});
218211
}
219212

213+
fn visit_trait_ref(&mut self, tr: &'ast TraitRef) {
214+
self.insert(tr.ref_id, NodeTraitRef(tr));
215+
216+
self.with_parent(tr.ref_id, |this| {
217+
intravisit::walk_trait_ref(this, tr);
218+
});
219+
}
220+
220221
fn visit_fn(&mut self, fk: intravisit::FnKind<'ast>, fd: &'ast FnDecl,
221222
b: &'ast Expr, s: Span, id: NodeId) {
222223
assert_eq!(self.parent_node, id);
@@ -234,6 +235,20 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
234235
self.insert(lifetime.id, NodeLifetime(lifetime));
235236
}
236237

238+
fn visit_vis(&mut self, visibility: &'ast Visibility) {
239+
match *visibility {
240+
Visibility::Public |
241+
Visibility::Crate |
242+
Visibility::Inherited => {}
243+
Visibility::Restricted { id, .. } => {
244+
self.insert(id, NodeVisibility(visibility));
245+
self.with_parent(id, |this| {
246+
intravisit::walk_vis(this, visibility);
247+
});
248+
}
249+
}
250+
}
251+
237252
fn visit_macro_def(&mut self, macro_def: &'ast MacroDef) {
238253
self.insert_entry(macro_def.id, NotPresent);
239254
}

src/librustc/hir/map/mod.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub enum Node<'ast> {
4949
NodeExpr(&'ast Expr),
5050
NodeStmt(&'ast Stmt),
5151
NodeTy(&'ast Ty),
52+
NodeTraitRef(&'ast TraitRef),
5253
NodeLocal(&'ast Pat),
5354
NodePat(&'ast Pat),
5455
NodeBlock(&'ast Block),
@@ -57,7 +58,8 @@ pub enum Node<'ast> {
5758
NodeStructCtor(&'ast VariantData),
5859

5960
NodeLifetime(&'ast Lifetime),
60-
NodeTyParam(&'ast TyParam)
61+
NodeTyParam(&'ast TyParam),
62+
NodeVisibility(&'ast Visibility),
6163
}
6264

6365
/// Represents an entry and its parent NodeID.
@@ -76,12 +78,14 @@ pub enum MapEntry<'ast> {
7678
EntryExpr(NodeId, &'ast Expr),
7779
EntryStmt(NodeId, &'ast Stmt),
7880
EntryTy(NodeId, &'ast Ty),
81+
EntryTraitRef(NodeId, &'ast TraitRef),
7982
EntryLocal(NodeId, &'ast Pat),
8083
EntryPat(NodeId, &'ast Pat),
8184
EntryBlock(NodeId, &'ast Block),
8285
EntryStructCtor(NodeId, &'ast VariantData),
8386
EntryLifetime(NodeId, &'ast Lifetime),
8487
EntryTyParam(NodeId, &'ast TyParam),
88+
EntryVisibility(NodeId, &'ast Visibility),
8589

8690
/// Roots for node trees.
8791
RootCrate,
@@ -105,12 +109,14 @@ impl<'ast> MapEntry<'ast> {
105109
NodeExpr(n) => EntryExpr(p, n),
106110
NodeStmt(n) => EntryStmt(p, n),
107111
NodeTy(n) => EntryTy(p, n),
112+
NodeTraitRef(n) => EntryTraitRef(p, n),
108113
NodeLocal(n) => EntryLocal(p, n),
109114
NodePat(n) => EntryPat(p, n),
110115
NodeBlock(n) => EntryBlock(p, n),
111116
NodeStructCtor(n) => EntryStructCtor(p, n),
112117
NodeLifetime(n) => EntryLifetime(p, n),
113118
NodeTyParam(n) => EntryTyParam(p, n),
119+
NodeVisibility(n) => EntryVisibility(p, n),
114120
}
115121
}
116122

@@ -124,12 +130,14 @@ impl<'ast> MapEntry<'ast> {
124130
EntryExpr(id, _) => id,
125131
EntryStmt(id, _) => id,
126132
EntryTy(id, _) => id,
133+
EntryTraitRef(id, _) => id,
127134
EntryLocal(id, _) => id,
128135
EntryPat(id, _) => id,
129136
EntryBlock(id, _) => id,
130137
EntryStructCtor(id, _) => id,
131138
EntryLifetime(id, _) => id,
132139
EntryTyParam(id, _) => id,
140+
EntryVisibility(id, _) => id,
133141

134142
NotPresent |
135143
RootCrate |
@@ -147,12 +155,14 @@ impl<'ast> MapEntry<'ast> {
147155
EntryExpr(_, n) => NodeExpr(n),
148156
EntryStmt(_, n) => NodeStmt(n),
149157
EntryTy(_, n) => NodeTy(n),
158+
EntryTraitRef(_, n) => NodeTraitRef(n),
150159
EntryLocal(_, n) => NodeLocal(n),
151160
EntryPat(_, n) => NodePat(n),
152161
EntryBlock(_, n) => NodeBlock(n),
153162
EntryStructCtor(_, n) => NodeStructCtor(n),
154163
EntryLifetime(_, n) => NodeLifetime(n),
155164
EntryTyParam(_, n) => NodeTyParam(n),
165+
EntryVisibility(_, n) => NodeVisibility(n),
156166
_ => return None
157167
})
158168
}
@@ -266,12 +276,14 @@ impl<'ast> Map<'ast> {
266276
EntryExpr(p, _) |
267277
EntryStmt(p, _) |
268278
EntryTy(p, _) |
279+
EntryTraitRef(p, _) |
269280
EntryLocal(p, _) |
270281
EntryPat(p, _) |
271282
EntryBlock(p, _) |
272283
EntryStructCtor(p, _) |
273284
EntryLifetime(p, _) |
274-
EntryTyParam(p, _) =>
285+
EntryTyParam(p, _) |
286+
EntryVisibility(p, _) =>
275287
id = p,
276288

277289
RootCrate =>
@@ -307,12 +319,14 @@ impl<'ast> Map<'ast> {
307319
EntryExpr(p, _) |
308320
EntryStmt(p, _) |
309321
EntryTy(p, _) |
322+
EntryTraitRef(p, _) |
310323
EntryLocal(p, _) |
311324
EntryPat(p, _) |
312325
EntryBlock(p, _) |
313326
EntryStructCtor(p, _) |
314327
EntryLifetime(p, _) |
315-
EntryTyParam(p, _) =>
328+
EntryTyParam(p, _) |
329+
EntryVisibility(p, _) =>
316330
id = p,
317331

318332
RootInlinedParent(parent) => match *parent {
@@ -707,11 +721,13 @@ impl<'ast> Map<'ast> {
707721
Some(NodeExpr(expr)) => expr.span,
708722
Some(NodeStmt(stmt)) => stmt.span,
709723
Some(NodeTy(ty)) => ty.span,
724+
Some(NodeTraitRef(tr)) => tr.path.span,
710725
Some(NodeLocal(pat)) => pat.span,
711726
Some(NodePat(pat)) => pat.span,
712727
Some(NodeBlock(block)) => block.span,
713728
Some(NodeStructCtor(_)) => self.expect_item(self.get_parent(id)).span,
714729
Some(NodeTyParam(ty_param)) => ty_param.span,
730+
Some(NodeVisibility(&Visibility::Restricted { ref path, .. })) => path.span,
715731
_ => return None,
716732
};
717733
Some(sp)
@@ -926,9 +942,11 @@ impl<'a> NodePrinter for pprust::State<'a> {
926942
NodeExpr(a) => self.print_expr(&a),
927943
NodeStmt(a) => self.print_stmt(&a),
928944
NodeTy(a) => self.print_type(&a),
945+
NodeTraitRef(a) => self.print_trait_ref(&a),
929946
NodePat(a) => self.print_pat(&a),
930947
NodeBlock(a) => self.print_block(&a),
931948
NodeLifetime(a) => self.print_lifetime(&a),
949+
NodeVisibility(a) => self.print_visibility(&a),
932950
NodeTyParam(_) => bug!("cannot print TyParam"),
933951
// these cases do not carry enough information in the
934952
// ast_map to reconstruct their full structure for pretty
@@ -1018,6 +1036,9 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
10181036
Some(NodeTy(ref ty)) => {
10191037
format!("type {}{}", pprust::ty_to_string(&ty), id_str)
10201038
}
1039+
Some(NodeTraitRef(ref tr)) => {
1040+
format!("trait_ref {}{}", pprust::path_to_string(&tr.path), id_str)
1041+
}
10211042
Some(NodeLocal(ref pat)) => {
10221043
format!("local {}{}", pprust::pat_to_string(&pat), id_str)
10231044
}
@@ -1037,6 +1058,9 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
10371058
Some(NodeTyParam(ref ty_param)) => {
10381059
format!("typaram {:?}{}", ty_param, id_str)
10391060
}
1061+
Some(NodeVisibility(ref vis)) => {
1062+
format!("visibility {:?}{}", vis, id_str)
1063+
}
10401064
None => {
10411065
format!("unknown node{}", id_str)
10421066
}

src/librustc/hir/print.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ impl<'a> State<'a> {
845845
self.ann.post(self, NodeItem(item))
846846
}
847847

848-
fn print_trait_ref(&mut self, t: &hir::TraitRef) -> io::Result<()> {
848+
pub fn print_trait_ref(&mut self, t: &hir::TraitRef) -> io::Result<()> {
849849
self.print_path(&t.path, false)
850850
}
851851

0 commit comments

Comments
 (0)