Skip to content

Commit 287e85a

Browse files
committed
Various straight-forward ports that override visit_nested_items
to do "in-situ" visits.
1 parent 0c2e12b commit 287e85a

File tree

8 files changed

+229
-162
lines changed

8 files changed

+229
-162
lines changed

src/librustc/lint/context.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use syntax::parse::token::InternedString;
4444
use syntax::ast;
4545
use rustc_front::hir;
4646
use rustc_front::util;
47-
use rustc_front::visit as hir_visit;
47+
use rustc_front::intravisit as hir_visit;
4848
use syntax::visit as ast_visit;
4949
use syntax::diagnostic;
5050

@@ -606,10 +606,12 @@ impl<'a, 'tcx> LintContext for LateContext<'a, 'tcx> {
606606
}
607607

608608
fn enter_attrs(&mut self, attrs: &[ast::Attribute]) {
609+
debug!("late context: enter_attrs({:?})", attrs);
609610
run_lints!(self, enter_lint_attrs, late_passes, attrs);
610611
}
611612

612613
fn exit_attrs(&mut self, attrs: &[ast::Attribute]) {
614+
debug!("late context: exit_attrs({:?})", attrs);
613615
run_lints!(self, exit_lint_attrs, late_passes, attrs);
614616
}
615617
}
@@ -633,15 +635,24 @@ impl<'a> LintContext for EarlyContext<'a> {
633635
}
634636

635637
fn enter_attrs(&mut self, attrs: &[ast::Attribute]) {
638+
debug!("early context: exit_attrs({:?})", attrs);
636639
run_lints!(self, enter_lint_attrs, early_passes, attrs);
637640
}
638641

639642
fn exit_attrs(&mut self, attrs: &[ast::Attribute]) {
643+
debug!("early context: exit_attrs({:?})", attrs);
640644
run_lints!(self, exit_lint_attrs, early_passes, attrs);
641645
}
642646
}
643647

644648
impl<'a, 'tcx, 'v> hir_visit::Visitor<'v> for LateContext<'a, 'tcx> {
649+
/// Because lints are scoped lexically, we want to walk nested
650+
/// items in the context of the outer item, so enable
651+
/// deep-walking.
652+
fn visit_nested_item(&mut self, item: hir::ItemId) {
653+
self.visit_item(self.tcx.map.expect_item(item.id))
654+
}
655+
645656
fn visit_item(&mut self, it: &hir::Item) {
646657
self.with_lint_attrs(&it.attrs, |cx| {
647658
run_lints!(cx, check_item, late_passes, it);
@@ -947,6 +958,7 @@ impl<'a, 'tcx> IdVisitingOperation for LateContext<'a, 'tcx> {
947958
match self.sess().lints.borrow_mut().remove(&id) {
948959
None => {}
949960
Some(lints) => {
961+
debug!("LateContext::visit_id: id={:?} lints={:?}", id, lints);
950962
for (lint_id, span, msg) in lints {
951963
self.span_lint(lint_id.lint, span, &msg[..])
952964
}
@@ -1003,16 +1015,14 @@ impl LateLintPass for GatherNodeLevels {
10031015
///
10041016
/// Consumes the `lint_store` field of the `Session`.
10051017
pub fn check_crate(tcx: &ty::ctxt,
1006-
krate: &hir::Crate,
10071018
exported_items: &ExportedItems) {
1008-
1019+
let krate = tcx.map.krate();
10091020
let mut cx = LateContext::new(tcx, krate, exported_items);
10101021

10111022
// Visit the whole crate.
10121023
cx.with_lint_attrs(&krate.attrs, |cx| {
10131024
cx.visit_id(ast::CRATE_NODE_ID);
10141025
cx.visit_ids(|v| {
1015-
v.visited_outermost = true;
10161026
hir_visit::walk_crate(v, krate);
10171027
});
10181028

src/librustc/lint/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub use self::LintSource::*;
3434
use std::hash;
3535
use std::ascii::AsciiExt;
3636
use syntax::codemap::Span;
37-
use rustc_front::visit::FnKind;
37+
use rustc_front::intravisit::FnKind;
3838
use syntax::visit as ast_visit;
3939
use syntax::ast;
4040
use rustc_front::hir;
@@ -218,7 +218,7 @@ pub type EarlyLintPassObject = Box<EarlyLintPass + 'static>;
218218
pub type LateLintPassObject = Box<LateLintPass + 'static>;
219219

220220
/// Identifies a lint known to the compiler.
221-
#[derive(Clone, Copy)]
221+
#[derive(Clone, Copy, Debug)]
222222
pub struct LintId {
223223
// Identity is based on pointer equality of this field.
224224
lint: &'static Lint,

src/librustc/middle/dead.rs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use front::map as ast_map;
1616
use rustc_front::hir;
17-
use rustc_front::visit::{self, Visitor};
17+
use rustc_front::intravisit::{self, Visitor};
1818

1919
use middle::{def, pat_util, privacy, ty};
2020
use middle::def_id::{DefId};
@@ -182,29 +182,29 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
182182
.contains(&attr::ReprExtern)
183183
});
184184

185-
visit::walk_item(self, &*item);
185+
intravisit::walk_item(self, &*item);
186186
}
187187
hir::ItemEnum(..) => {
188188
self.inherited_pub_visibility = item.vis == hir::Public;
189-
visit::walk_item(self, &*item);
189+
intravisit::walk_item(self, &*item);
190190
}
191191
hir::ItemFn(..)
192192
| hir::ItemTy(..)
193193
| hir::ItemStatic(..)
194194
| hir::ItemConst(..) => {
195-
visit::walk_item(self, &*item);
195+
intravisit::walk_item(self, &*item);
196196
}
197197
_ => ()
198198
}
199199
}
200200
ast_map::NodeTraitItem(trait_item) => {
201-
visit::walk_trait_item(self, trait_item);
201+
intravisit::walk_trait_item(self, trait_item);
202202
}
203203
ast_map::NodeImplItem(impl_item) => {
204-
visit::walk_impl_item(self, impl_item);
204+
intravisit::walk_impl_item(self, impl_item);
205205
}
206206
ast_map::NodeForeignItem(foreign_item) => {
207-
visit::walk_foreign_item(self, &*foreign_item);
207+
intravisit::walk_foreign_item(self, &*foreign_item);
208208
}
209209
_ => ()
210210
}
@@ -227,7 +227,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
227227
});
228228
self.live_symbols.extend(live_fields.map(|f| f.node.id));
229229

230-
visit::walk_struct_def(self, def);
230+
intravisit::walk_struct_def(self, def);
231231
}
232232

233233
fn visit_expr(&mut self, expr: &hir::Expr) {
@@ -244,7 +244,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
244244
_ => ()
245245
}
246246

247-
visit::walk_expr(self, expr);
247+
intravisit::walk_expr(self, expr);
248248
}
249249

250250
fn visit_arm(&mut self, arm: &hir::Arm) {
@@ -257,10 +257,10 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
257257
// can't be reached unless the variant is constructed elsewhere.
258258
let len = self.ignore_variant_stack.len();
259259
self.ignore_variant_stack.push_all(&*variants);
260-
visit::walk_arm(self, arm);
260+
intravisit::walk_arm(self, arm);
261261
self.ignore_variant_stack.truncate(len);
262262
} else {
263-
visit::walk_arm(self, arm);
263+
intravisit::walk_arm(self, arm);
264264
}
265265
}
266266

@@ -278,23 +278,18 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
278278
}
279279

280280
self.ignore_non_const_paths = true;
281-
visit::walk_pat(self, pat);
281+
intravisit::walk_pat(self, pat);
282282
self.ignore_non_const_paths = false;
283283
}
284284

285285
fn visit_path(&mut self, path: &hir::Path, id: ast::NodeId) {
286286
self.lookup_and_handle_definition(&id);
287-
visit::walk_path(self, path);
287+
intravisit::walk_path(self, path);
288288
}
289289

290290
fn visit_path_list_item(&mut self, path: &hir::Path, item: &hir::PathListItem) {
291291
self.lookup_and_handle_definition(&item.node.id());
292-
visit::walk_path_list_item(self, path, item);
293-
}
294-
295-
fn visit_item(&mut self, _: &hir::Item) {
296-
// Do not recurse into items. These items will be added to the
297-
// worklist and recursed into manually if necessary.
292+
intravisit::walk_path_list_item(self, path, item);
298293
}
299294
}
300295

@@ -371,7 +366,6 @@ impl<'v> Visitor<'v> for LifeSeeder {
371366
}
372367
_ => ()
373368
}
374-
visit::walk_item(self, item);
375369
}
376370
}
377371

@@ -408,7 +402,7 @@ fn create_and_seed_worklist(tcx: &ty::ctxt,
408402
let mut life_seeder = LifeSeeder {
409403
worklist: worklist
410404
};
411-
visit::walk_crate(&mut life_seeder, krate);
405+
krate.visit_all_items(&mut life_seeder);
412406

413407
return life_seeder.worklist;
414408
}
@@ -530,6 +524,14 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
530524
}
531525

532526
impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
527+
/// Walk nested items in place so that we don't report dead-code
528+
/// on inner functions when the outer function is already getting
529+
/// an error. We could do this also by checking the parents, but
530+
/// this is how the code is setup and it seems harmless enough.
531+
fn visit_nested_item(&mut self, item: hir::ItemId) {
532+
self.visit_item(self.tcx.map.expect_item(item.id))
533+
}
534+
533535
fn visit_item(&mut self, item: &hir::Item) {
534536
if self.should_warn_about_item(item) {
535537
self.warn_dead_code(
@@ -540,7 +542,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
540542
);
541543
} else {
542544
// Only continue if we didn't warn
543-
visit::walk_item(self, item);
545+
intravisit::walk_item(self, item);
544546
}
545547
}
546548

@@ -549,15 +551,15 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
549551
self.warn_dead_code(variant.node.data.id(), variant.span,
550552
variant.node.name, "variant");
551553
} else {
552-
visit::walk_variant(self, variant, g, id);
554+
intravisit::walk_variant(self, variant, g, id);
553555
}
554556
}
555557

556558
fn visit_foreign_item(&mut self, fi: &hir::ForeignItem) {
557559
if !self.symbol_is_live(fi.id, None) {
558560
self.warn_dead_code(fi.id, fi.span, fi.name, fi.node.descriptive_variant());
559561
}
560-
visit::walk_foreign_item(self, fi);
562+
intravisit::walk_foreign_item(self, fi);
561563
}
562564

563565
fn visit_struct_field(&mut self, field: &hir::StructField) {
@@ -566,7 +568,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
566568
field.node.name().unwrap(), "struct field");
567569
}
568570

569-
visit::walk_struct_field(self, field);
571+
intravisit::walk_struct_field(self, field);
570572
}
571573

572574
fn visit_impl_item(&mut self, impl_item: &hir::ImplItem) {
@@ -576,14 +578,14 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
576578
self.warn_dead_code(impl_item.id, impl_item.span,
577579
impl_item.name, "associated const");
578580
}
579-
visit::walk_expr(self, expr)
581+
intravisit::walk_expr(self, expr)
580582
}
581583
hir::ImplItemKind::Method(_, ref body) => {
582584
if !self.symbol_is_live(impl_item.id, None) {
583585
self.warn_dead_code(impl_item.id, impl_item.span,
584586
impl_item.name, "method");
585587
}
586-
visit::walk_block(self, body)
588+
intravisit::walk_block(self, body)
587589
}
588590
hir::ImplItemKind::Type(..) => {}
589591
}
@@ -593,10 +595,10 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
593595
fn visit_trait_item(&mut self, trait_item: &hir::TraitItem) {
594596
match trait_item.node {
595597
hir::ConstTraitItem(_, Some(ref expr)) => {
596-
visit::walk_expr(self, expr)
598+
intravisit::walk_expr(self, expr)
597599
}
598600
hir::MethodTraitItem(_, Some(ref body)) => {
599-
visit::walk_block(self, body)
601+
intravisit::walk_block(self, body)
600602
}
601603
hir::ConstTraitItem(_, None) |
602604
hir::MethodTraitItem(_, None) |
@@ -612,5 +614,5 @@ pub fn check_crate(tcx: &ty::ctxt,
612614
let live_symbols = find_live(tcx, exported_items,
613615
reachable_symbols, krate);
614616
let mut visitor = DeadVisitor { tcx: tcx, live_symbols: live_symbols };
615-
visit::walk_crate(&mut visitor, krate);
617+
intravisit::walk_crate(&mut visitor, krate);
616618
}

0 commit comments

Comments
 (0)