]> git.lizzy.rs Git - rust.git/commitdiff
rustc: track hir::{TraitRef, Visibility} in the HIR map.
authorEduard Burtescu <edy.burt@gmail.com>
Sun, 30 Oct 2016 06:04:52 +0000 (08:04 +0200)
committerEduard-Mihai Burtescu <edy.burt@gmail.com>
Mon, 28 Nov 2016 02:18:10 +0000 (04:18 +0200)
src/librustc/hir/map/collector.rs
src/librustc/hir/map/mod.rs
src/librustc/hir/print.rs

index 89217e83ca22eeaf3be3dd317a4c64553cb3f40d..64bf4bbf08039d45737d102ae889cfe00847dae4 100644 (file)
@@ -124,13 +124,6 @@ fn visit_item(&mut self, i: &'ast Item) {
                         this.insert(struct_def.id(), NodeStructCtor(struct_def));
                     }
                 }
-                ItemTrait(.., ref bounds, _) => {
-                    for b in bounds.iter() {
-                        if let TraitTyParamBound(ref t, TraitBoundModifier::None) = *b {
-                            this.insert(t.trait_ref.ref_id, NodeItem(i));
-                        }
-                    }
-                }
                 ItemUse(ref view_path) => {
                     match view_path.node {
                         ViewPathList(_, ref paths) => {
@@ -217,6 +210,14 @@ fn visit_ty(&mut self, ty: &'ast Ty) {
         });
     }
 
+    fn visit_trait_ref(&mut self, tr: &'ast TraitRef) {
+        self.insert(tr.ref_id, NodeTraitRef(tr));
+
+        self.with_parent(tr.ref_id, |this| {
+            intravisit::walk_trait_ref(this, tr);
+        });
+    }
+
     fn visit_fn(&mut self, fk: intravisit::FnKind<'ast>, fd: &'ast FnDecl,
                 b: &'ast Expr, s: Span, id: NodeId) {
         assert_eq!(self.parent_node, id);
@@ -234,6 +235,20 @@ fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
         self.insert(lifetime.id, NodeLifetime(lifetime));
     }
 
+    fn visit_vis(&mut self, visibility: &'ast Visibility) {
+        match *visibility {
+            Visibility::Public |
+            Visibility::Crate |
+            Visibility::Inherited => {}
+            Visibility::Restricted { id, .. } => {
+                self.insert(id, NodeVisibility(visibility));
+                self.with_parent(id, |this| {
+                    intravisit::walk_vis(this, visibility);
+                });
+            }
+        }
+    }
+
     fn visit_macro_def(&mut self, macro_def: &'ast MacroDef) {
         self.insert_entry(macro_def.id, NotPresent);
     }
index a90577b34261c4e7c8338052aac83f4823511dda..7f6c85eeaac8d40f50430cba5f25423666a52177 100644 (file)
@@ -49,6 +49,7 @@ pub enum Node<'ast> {
     NodeExpr(&'ast Expr),
     NodeStmt(&'ast Stmt),
     NodeTy(&'ast Ty),
+    NodeTraitRef(&'ast TraitRef),
     NodeLocal(&'ast Pat),
     NodePat(&'ast Pat),
     NodeBlock(&'ast Block),
@@ -57,7 +58,8 @@ pub enum Node<'ast> {
     NodeStructCtor(&'ast VariantData),
 
     NodeLifetime(&'ast Lifetime),
-    NodeTyParam(&'ast TyParam)
+    NodeTyParam(&'ast TyParam),
+    NodeVisibility(&'ast Visibility),
 }
 
 /// Represents an entry and its parent NodeID.
@@ -76,12 +78,14 @@ pub enum MapEntry<'ast> {
     EntryExpr(NodeId, &'ast Expr),
     EntryStmt(NodeId, &'ast Stmt),
     EntryTy(NodeId, &'ast Ty),
+    EntryTraitRef(NodeId, &'ast TraitRef),
     EntryLocal(NodeId, &'ast Pat),
     EntryPat(NodeId, &'ast Pat),
     EntryBlock(NodeId, &'ast Block),
     EntryStructCtor(NodeId, &'ast VariantData),
     EntryLifetime(NodeId, &'ast Lifetime),
     EntryTyParam(NodeId, &'ast TyParam),
+    EntryVisibility(NodeId, &'ast Visibility),
 
     /// Roots for node trees.
     RootCrate,
@@ -105,12 +109,14 @@ fn from_node(p: NodeId, node: Node<'ast>) -> MapEntry<'ast> {
             NodeExpr(n) => EntryExpr(p, n),
             NodeStmt(n) => EntryStmt(p, n),
             NodeTy(n) => EntryTy(p, n),
+            NodeTraitRef(n) => EntryTraitRef(p, n),
             NodeLocal(n) => EntryLocal(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),
         }
     }
 
@@ -124,12 +130,14 @@ fn parent_node(self) -> Option<NodeId> {
             EntryExpr(id, _) => id,
             EntryStmt(id, _) => id,
             EntryTy(id, _) => id,
+            EntryTraitRef(id, _) => id,
             EntryLocal(id, _) => id,
             EntryPat(id, _) => id,
             EntryBlock(id, _) => id,
             EntryStructCtor(id, _) => id,
             EntryLifetime(id, _) => id,
             EntryTyParam(id, _) => id,
+            EntryVisibility(id, _) => id,
 
             NotPresent |
             RootCrate |
@@ -147,12 +155,14 @@ fn to_node(self) -> Option<Node<'ast>> {
             EntryExpr(_, n) => NodeExpr(n),
             EntryStmt(_, n) => NodeStmt(n),
             EntryTy(_, n) => NodeTy(n),
+            EntryTraitRef(_, n) => NodeTraitRef(n),
             EntryLocal(_, n) => NodeLocal(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),
             _ => return None
         })
     }
@@ -266,12 +276,14 @@ fn dep_node(&self, id0: NodeId) -> DepNode<DefId> {
                     EntryExpr(p, _) |
                     EntryStmt(p, _) |
                     EntryTy(p, _) |
+                    EntryTraitRef(p, _) |
                     EntryLocal(p, _) |
                     EntryPat(p, _) |
                     EntryBlock(p, _) |
                     EntryStructCtor(p, _) |
                     EntryLifetime(p, _) |
-                    EntryTyParam(p, _) =>
+                    EntryTyParam(p, _) |
+                    EntryVisibility(p, _) =>
                         id = p,
 
                     RootCrate =>
@@ -307,12 +319,14 @@ fn dep_node(&self, id0: NodeId) -> DepNode<DefId> {
                     EntryExpr(p, _) |
                     EntryStmt(p, _) |
                     EntryTy(p, _) |
+                    EntryTraitRef(p, _) |
                     EntryLocal(p, _) |
                     EntryPat(p, _) |
                     EntryBlock(p, _) |
                     EntryStructCtor(p, _) |
                     EntryLifetime(p, _) |
-                    EntryTyParam(p, _) =>
+                    EntryTyParam(p, _) |
+                    EntryVisibility(p, _) =>
                         id = p,
 
                     RootInlinedParent(parent) => match *parent {
@@ -707,11 +721,13 @@ pub fn opt_span(&self, id: NodeId) -> Option<Span> {
             Some(NodeExpr(expr)) => expr.span,
             Some(NodeStmt(stmt)) => stmt.span,
             Some(NodeTy(ty)) => ty.span,
+            Some(NodeTraitRef(tr)) => tr.path.span,
             Some(NodeLocal(pat)) => pat.span,
             Some(NodePat(pat)) => pat.span,
             Some(NodeBlock(block)) => block.span,
             Some(NodeStructCtor(_)) => self.expect_item(self.get_parent(id)).span,
             Some(NodeTyParam(ty_param)) => ty_param.span,
+            Some(NodeVisibility(&Visibility::Restricted { ref path, .. })) => path.span,
             _ => return None,
         };
         Some(sp)
@@ -926,9 +942,11 @@ fn print_node(&mut self, node: &Node) -> io::Result<()> {
             NodeExpr(a)        => self.print_expr(&a),
             NodeStmt(a)        => self.print_stmt(&a),
             NodeTy(a)          => self.print_type(&a),
+            NodeTraitRef(a)    => self.print_trait_ref(&a),
             NodePat(a)         => self.print_pat(&a),
             NodeBlock(a)       => self.print_block(&a),
             NodeLifetime(a)    => self.print_lifetime(&a),
+            NodeVisibility(a)  => self.print_visibility(&a),
             NodeTyParam(_)     => bug!("cannot print TyParam"),
             // these cases do not carry enough information in the
             // 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 {
         Some(NodeTy(ref ty)) => {
             format!("type {}{}", pprust::ty_to_string(&ty), id_str)
         }
+        Some(NodeTraitRef(ref tr)) => {
+            format!("trait_ref {}{}", pprust::path_to_string(&tr.path), id_str)
+        }
         Some(NodeLocal(ref pat)) => {
             format!("local {}{}", pprust::pat_to_string(&pat), id_str)
         }
@@ -1037,6 +1058,9 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
         Some(NodeTyParam(ref ty_param)) => {
             format!("typaram {:?}{}", ty_param, id_str)
         }
+        Some(NodeVisibility(ref vis)) => {
+            format!("visibility {:?}{}", vis, id_str)
+        }
         None => {
             format!("unknown node{}", id_str)
         }
index 448139c37e64662f0cf69c556a598601a86b1db3..f6f40a91bf491097a2f1e8933a20f02349ad720d 100644 (file)
@@ -845,7 +845,7 @@ pub fn print_item(&mut self, item: &hir::Item) -> io::Result<()> {
         self.ann.post(self, NodeItem(item))
     }
 
-    fn print_trait_ref(&mut self, t: &hir::TraitRef) -> io::Result<()> {
+    pub fn print_trait_ref(&mut self, t: &hir::TraitRef) -> io::Result<()> {
         self.print_path(&t.path, false)
     }