Skip to content

Commit 80be2f8

Browse files
committed
Auto merge of #43971 - alexcrichton:lint-statements, r=michaelwoerister
rustc: Add `Local` to the HIR map of parents 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
2 parents 757b7ac + 4ba2df1 commit 80be2f8

File tree

9 files changed

+53
-31
lines changed

9 files changed

+53
-31
lines changed

src/librustc/hir/map/collector.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl<'hir> Visitor<'hir> for NodeCollector<'hir> {
138138

139139
fn visit_pat(&mut self, pat: &'hir Pat) {
140140
let node = if let PatKind::Binding(..) = pat.node {
141-
NodeLocal(pat)
141+
NodeBinding(pat)
142142
} else {
143143
NodePat(pat)
144144
};
@@ -195,6 +195,13 @@ impl<'hir> Visitor<'hir> for NodeCollector<'hir> {
195195
});
196196
}
197197

198+
fn visit_local(&mut self, l: &'hir Local) {
199+
self.insert(l.id, NodeLocal(l));
200+
self.with_parent(l.id, |this| {
201+
intravisit::walk_local(this, l)
202+
})
203+
}
204+
198205
fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
199206
self.insert(lifetime.id, NodeLifetime(lifetime));
200207
}

src/librustc/hir/map/mod.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ pub enum Node<'hir> {
5353
NodeStmt(&'hir Stmt),
5454
NodeTy(&'hir Ty),
5555
NodeTraitRef(&'hir TraitRef),
56-
NodeLocal(&'hir Pat),
56+
NodeBinding(&'hir Pat),
5757
NodePat(&'hir Pat),
5858
NodeBlock(&'hir Block),
59+
NodeLocal(&'hir Local),
5960

6061
/// NodeStructCtor represents a tuple struct.
6162
NodeStructCtor(&'hir VariantData),
@@ -83,13 +84,14 @@ enum MapEntry<'hir> {
8384
EntryStmt(NodeId, &'hir Stmt),
8485
EntryTy(NodeId, &'hir Ty),
8586
EntryTraitRef(NodeId, &'hir TraitRef),
86-
EntryLocal(NodeId, &'hir Pat),
87+
EntryBinding(NodeId, &'hir Pat),
8788
EntryPat(NodeId, &'hir Pat),
8889
EntryBlock(NodeId, &'hir Block),
8990
EntryStructCtor(NodeId, &'hir VariantData),
9091
EntryLifetime(NodeId, &'hir Lifetime),
9192
EntryTyParam(NodeId, &'hir TyParam),
9293
EntryVisibility(NodeId, &'hir Visibility),
94+
EntryLocal(NodeId, &'hir Local),
9395

9496
/// Roots for node trees.
9597
RootCrate,
@@ -114,13 +116,14 @@ impl<'hir> MapEntry<'hir> {
114116
NodeStmt(n) => EntryStmt(p, n),
115117
NodeTy(n) => EntryTy(p, n),
116118
NodeTraitRef(n) => EntryTraitRef(p, n),
117-
NodeLocal(n) => EntryLocal(p, n),
119+
NodeBinding(n) => EntryBinding(p, n),
118120
NodePat(n) => EntryPat(p, n),
119121
NodeBlock(n) => EntryBlock(p, n),
120122
NodeStructCtor(n) => EntryStructCtor(p, n),
121123
NodeLifetime(n) => EntryLifetime(p, n),
122124
NodeTyParam(n) => EntryTyParam(p, n),
123125
NodeVisibility(n) => EntryVisibility(p, n),
126+
NodeLocal(n) => EntryLocal(p, n),
124127
}
125128
}
126129

@@ -136,13 +139,14 @@ impl<'hir> MapEntry<'hir> {
136139
EntryStmt(id, _) => id,
137140
EntryTy(id, _) => id,
138141
EntryTraitRef(id, _) => id,
139-
EntryLocal(id, _) => id,
142+
EntryBinding(id, _) => id,
140143
EntryPat(id, _) => id,
141144
EntryBlock(id, _) => id,
142145
EntryStructCtor(id, _) => id,
143146
EntryLifetime(id, _) => id,
144147
EntryTyParam(id, _) => id,
145148
EntryVisibility(id, _) => id,
149+
EntryLocal(id, _) => id,
146150

147151
NotPresent |
148152
RootCrate => return None,
@@ -161,13 +165,14 @@ impl<'hir> MapEntry<'hir> {
161165
EntryStmt(_, n) => NodeStmt(n),
162166
EntryTy(_, n) => NodeTy(n),
163167
EntryTraitRef(_, n) => NodeTraitRef(n),
164-
EntryLocal(_, n) => NodeLocal(n),
168+
EntryBinding(_, n) => NodeBinding(n),
165169
EntryPat(_, n) => NodePat(n),
166170
EntryBlock(_, n) => NodeBlock(n),
167171
EntryStructCtor(_, n) => NodeStructCtor(n),
168172
EntryLifetime(_, n) => NodeLifetime(n),
169173
EntryTyParam(_, n) => NodeTyParam(n),
170174
EntryVisibility(_, n) => NodeVisibility(n),
175+
EntryLocal(_, n) => NodeLocal(n),
171176
_ => return None
172177
})
173178
}
@@ -319,13 +324,14 @@ impl<'hir> Map<'hir> {
319324
EntryStmt(p, _) |
320325
EntryTy(p, _) |
321326
EntryTraitRef(p, _) |
322-
EntryLocal(p, _) |
327+
EntryBinding(p, _) |
323328
EntryPat(p, _) |
324329
EntryBlock(p, _) |
325330
EntryStructCtor(p, _) |
326331
EntryLifetime(p, _) |
327332
EntryTyParam(p, _) |
328-
EntryVisibility(p, _) =>
333+
EntryVisibility(p, _) |
334+
EntryLocal(p, _) =>
329335
id = p,
330336

331337
EntryExpr(p, _) => {
@@ -589,7 +595,7 @@ impl<'hir> Map<'hir> {
589595
/// immediate parent is an item or a closure.
590596
pub fn is_argument(&self, id: NodeId) -> bool {
591597
match self.find(id) {
592-
Some(NodeLocal(_)) => (),
598+
Some(NodeBinding(_)) => (),
593599
_ => return false,
594600
}
595601
match self.find(self.get_parent_node(id)) {
@@ -856,7 +862,7 @@ impl<'hir> Map<'hir> {
856862
NodeField(f) => f.name,
857863
NodeLifetime(lt) => lt.name,
858864
NodeTyParam(tp) => tp.name,
859-
NodeLocal(&Pat { node: PatKind::Binding(_,_,l,_), .. }) => l.node,
865+
NodeBinding(&Pat { node: PatKind::Binding(_,_,l,_), .. }) => l.node,
860866
NodeStructCtor(_) => self.name(self.get_parent(id)),
861867
_ => bug!("no name for {}", self.node_to_string(id))
862868
}
@@ -915,14 +921,15 @@ impl<'hir> Map<'hir> {
915921
Some(EntryStmt(_, stmt)) => stmt.span,
916922
Some(EntryTy(_, ty)) => ty.span,
917923
Some(EntryTraitRef(_, tr)) => tr.path.span,
918-
Some(EntryLocal(_, pat)) => pat.span,
924+
Some(EntryBinding(_, pat)) => pat.span,
919925
Some(EntryPat(_, pat)) => pat.span,
920926
Some(EntryBlock(_, block)) => block.span,
921927
Some(EntryStructCtor(_, _)) => self.expect_item(self.get_parent(id)).span,
922928
Some(EntryLifetime(_, lifetime)) => lifetime.span,
923929
Some(EntryTyParam(_, ty_param)) => ty_param.span,
924930
Some(EntryVisibility(_, &Visibility::Restricted { ref path, .. })) => path.span,
925931
Some(EntryVisibility(_, v)) => bug!("unexpected Visibility {:?}", v),
932+
Some(EntryLocal(_, local)) => local.span,
926933

927934
Some(RootCrate) => self.forest.krate.span,
928935
Some(NotPresent) | None => {
@@ -1112,7 +1119,7 @@ impl<'a> print::State<'a> {
11121119
NodeStmt(a) => self.print_stmt(&a),
11131120
NodeTy(a) => self.print_type(&a),
11141121
NodeTraitRef(a) => self.print_trait_ref(&a),
1115-
NodeLocal(a) |
1122+
NodeBinding(a) |
11161123
NodePat(a) => self.print_pat(&a),
11171124
NodeBlock(a) => {
11181125
use syntax::print::pprust::PrintState;
@@ -1131,6 +1138,7 @@ impl<'a> print::State<'a> {
11311138
// hir_map to reconstruct their full structure for pretty
11321139
// printing.
11331140
NodeStructCtor(_) => bug!("cannot print isolated StructCtor"),
1141+
NodeLocal(a) => self.print_local_decl(&a),
11341142
}
11351143
}
11361144
}
@@ -1223,7 +1231,7 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
12231231
Some(NodeTraitRef(_)) => {
12241232
format!("trait_ref {}{}", map.node_to_pretty_string(id), id_str)
12251233
}
1226-
Some(NodeLocal(_)) => {
1234+
Some(NodeBinding(_)) => {
12271235
format!("local {}{}", map.node_to_pretty_string(id), id_str)
12281236
}
12291237
Some(NodePat(_)) => {
@@ -1232,6 +1240,9 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
12321240
Some(NodeBlock(_)) => {
12331241
format!("block {}{}", map.node_to_pretty_string(id), id_str)
12341242
}
1243+
Some(NodeLocal(_)) => {
1244+
format!("local {}{}", map.node_to_pretty_string(id), id_str)
1245+
}
12351246
Some(NodeStructCtor(_)) => {
12361247
format!("struct_ctor {}{}", path_str(), id_str)
12371248
}

src/librustc/middle/mem_categorization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ impl MutabilityCategory {
332332

333333
fn from_local(tcx: TyCtxt, tables: &ty::TypeckTables, id: ast::NodeId) -> MutabilityCategory {
334334
let ret = match tcx.hir.get(id) {
335-
hir_map::NodeLocal(p) => match p.node {
335+
hir_map::NodeBinding(p) => match p.node {
336336
PatKind::Binding(..) => {
337337
let bm = *tables.pat_binding_modes()
338338
.get(p.hir_id)

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
19711971

19721972
pub fn local_var_name_str(self, id: NodeId) -> InternedString {
19731973
match self.hir.find(id) {
1974-
Some(hir_map::NodeLocal(pat)) => {
1974+
Some(hir_map::NodeBinding(pat)) => {
19751975
match pat.node {
19761976
hir::PatKind::Binding(_, _, ref path1, _) => path1.node.as_str(),
19771977
_ => {

src/librustc_borrowck/borrowck/gather_loans/gather_moves.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,7 @@ fn get_pattern_source<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, pat: &Pat) -> Patte
6868
});
6969
PatternSource::MatchExpr(e)
7070
}
71-
NodeStmt(ref s) => {
72-
// the enclosing statement must be a `let` or something else
73-
match s.node {
74-
StmtDecl(ref decl, _) => {
75-
match decl.node {
76-
DeclLocal(ref local) => PatternSource::LetDecl(local),
77-
_ => return PatternSource::Other,
78-
}
79-
}
80-
_ => return PatternSource::Other,
81-
}
82-
}
83-
71+
NodeLocal(local) => PatternSource::LetDecl(local),
8472
_ => return PatternSource::Other,
8573

8674
}

src/librustc_borrowck/borrowck/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
903903

904904
fn local_binding_mode(&self, node_id: ast::NodeId) -> ty::BindingMode {
905905
let pat = match self.tcx.hir.get(node_id) {
906-
hir_map::Node::NodeLocal(pat) => pat,
906+
hir_map::Node::NodeBinding(pat) => pat,
907907
node => bug!("bad node for local: {:?}", node)
908908
};
909909

src/librustc_mir/build/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
382382
debug_name: keywords::Invalid.name(),
383383
by_ref,
384384
};
385-
if let Some(hir::map::NodeLocal(pat)) = tcx.hir.find(var_node_id) {
385+
if let Some(hir::map::NodeBinding(pat)) = tcx.hir.find(var_node_id) {
386386
if let hir::PatKind::Binding(_, _, ref ident, _) = pat.node {
387387
decl.debug_name = ident.node;
388388
}

src/librustc_save_analysis/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
589589
self.tables.qpath_def(qpath, hir_id)
590590
}
591591

592-
Node::NodeLocal(&hir::Pat { node: hir::PatKind::Binding(_, def_id, ..), .. }) => {
592+
Node::NodeBinding(&hir::Pat { node: hir::PatKind::Binding(_, def_id, ..), .. }) => {
593593
HirDef::Local(def_id)
594594
}
595595

src/test/run-pass/issue-43910.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![deny(unused_variables)]
12+
13+
fn main() {
14+
#[allow(unused_variables)]
15+
let x = 12;
16+
}

0 commit comments

Comments
 (0)