Skip to content

Commit 4ba2df1

Browse files
committed
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
1 parent 9b6f9d0 commit 4ba2df1

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

src/librustc/hir/map/collector.rs

+7
Original file line numberDiff line numberDiff line change
@@ -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

+12-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub enum Node<'hir> {
5656
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),
@@ -90,6 +91,7 @@ enum MapEntry<'hir> {
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,
@@ -121,6 +123,7 @@ impl<'hir> MapEntry<'hir> {
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

@@ -143,6 +146,7 @@ impl<'hir> MapEntry<'hir> {
143146
EntryLifetime(id, _) => id,
144147
EntryTyParam(id, _) => id,
145148
EntryVisibility(id, _) => id,
149+
EntryLocal(id, _) => id,
146150

147151
NotPresent |
148152
RootCrate => return None,
@@ -168,6 +172,7 @@ impl<'hir> MapEntry<'hir> {
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
}
@@ -325,7 +330,8 @@ impl<'hir> Map<'hir> {
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, _) => {
@@ -923,6 +929,7 @@ impl<'hir> Map<'hir> {
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 => {
@@ -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
}
@@ -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_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/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)