]> git.lizzy.rs Git - rust.git/commitdiff
rustc: Add `Local` to the HIR map of parents
authorAlex Crichton <alex@alexcrichton.com>
Fri, 18 Aug 2017 06:56:11 +0000 (23:56 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Fri, 18 Aug 2017 17:36:39 +0000 (10:36 -0700)
When walking parents for lints we want to be sure to hit `let` statements which
can have attributes, so hook up these statements in the HIR map.

Closes #43910

src/librustc/hir/map/collector.rs
src/librustc/hir/map/mod.rs
src/librustc_borrowck/borrowck/gather_loans/gather_moves.rs
src/test/run-pass/issue-43910.rs [new file with mode: 0644]

index 7b7d9adec05eded99e1a1887f600b858d917df68..0928081decda0aefcb1b6f4d196690bdf9b365ce 100644 (file)
@@ -195,6 +195,13 @@ fn visit_block(&mut self, block: &'hir Block) {
         });
     }
 
+    fn visit_local(&mut self, l: &'hir Local) {
+        self.insert(l.id, NodeLocal(l));
+        self.with_parent(l.id, |this| {
+            intravisit::walk_local(this, l)
+        })
+    }
+
     fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
         self.insert(lifetime.id, NodeLifetime(lifetime));
     }
index 1cf4e799ca769d94320e7c6cbac2eba51aaea7a6..1ff3166110aa0a31a079b224c5797f15c66567e9 100644 (file)
@@ -56,6 +56,7 @@ pub enum Node<'hir> {
     NodeBinding(&'hir Pat),
     NodePat(&'hir Pat),
     NodeBlock(&'hir Block),
+    NodeLocal(&'hir Local),
 
     /// NodeStructCtor represents a tuple struct.
     NodeStructCtor(&'hir VariantData),
@@ -90,6 +91,7 @@ enum MapEntry<'hir> {
     EntryLifetime(NodeId, &'hir Lifetime),
     EntryTyParam(NodeId, &'hir TyParam),
     EntryVisibility(NodeId, &'hir Visibility),
+    EntryLocal(NodeId, &'hir Local),
 
     /// Roots for node trees.
     RootCrate,
@@ -121,6 +123,7 @@ fn from_node(p: NodeId, node: Node<'hir>) -> MapEntry<'hir> {
             NodeLifetime(n) => EntryLifetime(p, n),
             NodeTyParam(n) => EntryTyParam(p, n),
             NodeVisibility(n) => EntryVisibility(p, n),
+            NodeLocal(n) => EntryLocal(p, n),
         }
     }
 
@@ -143,6 +146,7 @@ fn parent_node(self) -> Option<NodeId> {
             EntryLifetime(id, _) => id,
             EntryTyParam(id, _) => id,
             EntryVisibility(id, _) => id,
+            EntryLocal(id, _) => id,
 
             NotPresent |
             RootCrate => return None,
@@ -168,6 +172,7 @@ fn to_node(self) -> Option<Node<'hir>> {
             EntryLifetime(_, n) => NodeLifetime(n),
             EntryTyParam(_, n) => NodeTyParam(n),
             EntryVisibility(_, n) => NodeVisibility(n),
+            EntryLocal(_, n) => NodeLocal(n),
             _ => return None
         })
     }
@@ -325,7 +330,8 @@ fn dep_node(&self, id0: NodeId) -> DepNode {
                 EntryStructCtor(p, _) |
                 EntryLifetime(p, _) |
                 EntryTyParam(p, _) |
-                EntryVisibility(p, _) =>
+                EntryVisibility(p, _) |
+                EntryLocal(p, _) =>
                     id = p,
 
                 EntryExpr(p, _) => {
@@ -923,6 +929,7 @@ pub fn span(&self, id: NodeId) -> 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(NotPresent) | None => {
@@ -1131,6 +1138,7 @@ pub fn print_node(&mut self, node: Node) -> io::Result<()> {
             // hir_map to reconstruct their full structure for pretty
             // printing.
             NodeStructCtor(_)  => bug!("cannot print isolated StructCtor"),
+            NodeLocal(a)       => self.print_local_decl(&a),
         }
     }
 }
@@ -1232,6 +1240,9 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
         Some(NodeBlock(_)) => {
             format!("block {}{}", map.node_to_pretty_string(id), id_str)
         }
+        Some(NodeLocal(_)) => {
+            format!("local {}{}", map.node_to_pretty_string(id), id_str)
+        }
         Some(NodeStructCtor(_)) => {
             format!("struct_ctor {}{}", path_str(), id_str)
         }
index bca980c5ccf4c974b2823e5016a5349766b5b2aa..9ccf157ed897465223bfc3b1dbee2a48bf3f9acd 100644 (file)
@@ -68,19 +68,7 @@ fn get_pattern_source<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, pat: &Pat) -> Patte
                     });
             PatternSource::MatchExpr(e)
         }
-        NodeStmt(ref s) => {
-            // the enclosing statement must be a `let` or something else
-            match s.node {
-                StmtDecl(ref decl, _) => {
-                    match decl.node {
-                        DeclLocal(ref local) => PatternSource::LetDecl(local),
-                        _ => return PatternSource::Other,
-                    }
-                }
-                _ => return PatternSource::Other,
-            }
-        }
-
+        NodeLocal(local) => PatternSource::LetDecl(local),
         _ => return PatternSource::Other,
 
     }
diff --git a/src/test/run-pass/issue-43910.rs b/src/test/run-pass/issue-43910.rs
new file mode 100644 (file)
index 0000000..d61ce7f
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![deny(unused_variables)]
+
+fn main() {
+    #[allow(unused_variables)]
+    let x = 12;
+}