Skip to content

Commit

Permalink
rustc: Add Local to the HIR map of parents
Browse files Browse the repository at this point in the history
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 rust-lang#43910
  • Loading branch information
alexcrichton committed Aug 18, 2017
1 parent 9b6f9d0 commit 4ba2df1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
7 changes: 7 additions & 0 deletions src/librustc/hir/map/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ impl<'hir> Visitor<'hir> for NodeCollector<'hir> {
});
}

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));
}
Expand Down
13 changes: 12 additions & 1 deletion src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -121,6 +123,7 @@ impl<'hir> MapEntry<'hir> {
NodeLifetime(n) => EntryLifetime(p, n),
NodeTyParam(n) => EntryTyParam(p, n),
NodeVisibility(n) => EntryVisibility(p, n),
NodeLocal(n) => EntryLocal(p, n),
}
}

Expand All @@ -143,6 +146,7 @@ impl<'hir> MapEntry<'hir> {
EntryLifetime(id, _) => id,
EntryTyParam(id, _) => id,
EntryVisibility(id, _) => id,
EntryLocal(id, _) => id,

NotPresent |
RootCrate => return None,
Expand All @@ -168,6 +172,7 @@ impl<'hir> MapEntry<'hir> {
EntryLifetime(_, n) => NodeLifetime(n),
EntryTyParam(_, n) => NodeTyParam(n),
EntryVisibility(_, n) => NodeVisibility(n),
EntryLocal(_, n) => NodeLocal(n),
_ => return None
})
}
Expand Down Expand Up @@ -325,7 +330,8 @@ impl<'hir> Map<'hir> {
EntryStructCtor(p, _) |
EntryLifetime(p, _) |
EntryTyParam(p, _) |
EntryVisibility(p, _) =>
EntryVisibility(p, _) |
EntryLocal(p, _) =>
id = p,

EntryExpr(p, _) => {
Expand Down Expand Up @@ -923,6 +929,7 @@ impl<'hir> Map<'hir> {
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 => {
Expand Down Expand Up @@ -1131,6 +1138,7 @@ impl<'a> print::State<'a> {
// hir_map to reconstruct their full structure for pretty
// printing.
NodeStructCtor(_) => bug!("cannot print isolated StructCtor"),
NodeLocal(a) => self.print_local_decl(&a),
}
}
}
Expand Down Expand Up @@ -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)
}
Expand Down
14 changes: 1 addition & 13 deletions src/librustc_borrowck/borrowck/gather_loans/gather_moves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

}
Expand Down
16 changes: 16 additions & 0 deletions src/test/run-pass/issue-43910.rs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 4ba2df1

Please sign in to comment.