Skip to content

Commit f0ce5bb

Browse files
committed
Split nested_visit_mode function off from nested_visit_map
... and make the latter mandatory to implement.
1 parent 725cffb commit f0ce5bb

File tree

35 files changed

+160
-110
lines changed

35 files changed

+160
-110
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,26 @@ pub trait Visitor<'v> : Sized {
9595
///////////////////////////////////////////////////////////////////////////
9696
// Nested items.
9797

98-
/// The default versions of the `visit_nested_XXX` routines invoke
99-
/// this method to get a map to use; if they get back `None`, they
100-
/// just skip nested things. Otherwise, they will lookup the
101-
/// nested item-like things in the map and visit it. So the best
102-
/// way to implement a nested visitor is to override this method
103-
/// to return a `Map`; one advantage of this is that if we add
104-
/// more types of nested things in the future, they will
105-
/// automatically work.
98+
/// The default versions of the `visit_nested_XXX` routines invoke this
99+
/// method to get a map to use; if they get back `None`, they just skip
100+
/// nested things. Otherwise, they will lookup the nested thing in the map
101+
/// and visit it depending on what `nested_visit_mode` returns. So the best
102+
/// way to implement a nested visitor is to override this method to return a
103+
/// `Map`; one advantage of this is that if we add more types of nested
104+
/// things in the future, they will automatically work.
106105
///
107106
/// **If for some reason you want the nested behavior, but don't
108107
/// have a `Map` are your disposal:** then you should override the
109108
/// `visit_nested_XXX` methods, and override this method to
110109
/// `panic!()`. This way, if a new `visit_nested_XXX` variant is
111110
/// added in the future, we will see the panic in your code and
112111
/// fix it appropriately.
113-
fn nested_visit_map(&mut self) -> Option<(&Map<'v>, NestedVisitMode)> {
114-
None
112+
fn nested_visit_map(&mut self) -> Option<&Map<'v>>;
113+
114+
/// Specifies what things nested things this visitor wants to visit. By
115+
/// default, bodies will be visited, but not nested items.
116+
fn nested_visit_mode(&mut self) -> NestedVisitMode {
117+
NestedVisitMode::OnlyBodies
115118
}
116119

117120
/// Invoked when a nested item is encountered. By default does
@@ -300,16 +303,15 @@ pub trait Visitor<'v> : Sized {
300303
}
301304

302305
fn map_for_body<'v, V: Visitor<'v>>(visitor: &mut V) -> Option<&Map<'v>> {
303-
visitor.nested_visit_map().map(|(map, _mode)| map)
306+
visitor.nested_visit_map()
304307
}
305308

306309
fn map_for_item<'v, V: Visitor<'v>>(visitor: &mut V) -> Option<&Map<'v>> {
307-
visitor.nested_visit_map().and_then(|(map, mode)| {
308-
match mode {
309-
NestedVisitMode::OnlyBodies => None,
310-
NestedVisitMode::All => Some(map)
311-
}
312-
})
310+
match visitor.nested_visit_mode() {
311+
NestedVisitMode::OnlyBodies => None,
312+
NestedVisitMode::All => Some(visitor.nested_visit_map()
313+
.expect("NestedVisitMode::All without nested_visit_map"))
314+
}
313315
}
314316

315317
pub fn walk_opt_name<'v, V: Visitor<'v>>(visitor: &mut V, span: Span, opt_name: Option<Name>) {
@@ -1059,8 +1061,8 @@ impl<'a, 'ast> IdRangeComputingVisitor<'a, 'ast> {
10591061
}
10601062

10611063
impl<'a, 'ast> Visitor<'ast> for IdRangeComputingVisitor<'a, 'ast> {
1062-
fn nested_visit_map(&mut self) -> Option<(&Map<'ast>, NestedVisitMode)> {
1063-
Some((&self.map, NestedVisitMode::OnlyBodies))
1064+
fn nested_visit_map(&mut self) -> Option<&Map<'ast>> {
1065+
Some(&self.map)
10641066
}
10651067

10661068
fn visit_id(&mut self, id: NodeId) {

src/librustc/hir/map/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use super::*;
1212

13-
use hir::intravisit::{Visitor, NestedVisitMode};
13+
use hir::intravisit::Visitor;
1414
use hir::def_id::DefId;
1515
use middle::cstore::InlinedItem;
1616
use std::iter::repeat;
@@ -91,7 +91,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
9191
/// deep walking so that we walk nested items in the context of
9292
/// their outer items.
9393
94-
fn nested_visit_map(&mut self) -> Option<(&map::Map<'ast>, NestedVisitMode)> {
94+
fn nested_visit_map(&mut self) -> Option<&map::Map<'ast>> {
9595
panic!("visit_nested_xxx must be manually implemented in this visitor")
9696
}
9797

src/librustc/hir/map/def_collector.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ impl<'a> visit::Visitor for DefCollector<'a> {
327327

328328
// We walk the HIR rather than the AST when reading items from metadata.
329329
impl<'ast> intravisit::Visitor<'ast> for DefCollector<'ast> {
330+
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'ast>> {
331+
None
332+
}
333+
330334
fn visit_body(&mut self, id: hir::ExprId) {
331335
if let Some(krate) = self.hir_crate {
332336
self.visit_expr(krate.expr(id));

src/librustc/lint/context.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -791,8 +791,12 @@ impl<'a, 'tcx> hir_visit::Visitor<'tcx> for LateContext<'a, 'tcx> {
791791
/// Because lints are scoped lexically, we want to walk nested
792792
/// items in the context of the outer item, so enable
793793
/// deep-walking.
794-
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, hir_visit::NestedVisitMode)> {
795-
Some((&self.tcx.map, hir_visit::NestedVisitMode::All))
794+
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
795+
Some(&self.tcx.map)
796+
}
797+
798+
fn nested_visit_mode(&mut self) -> hir_visit::NestedVisitMode {
799+
hir_visit::NestedVisitMode::All
796800
}
797801

798802
fn visit_item(&mut self, it: &'tcx hir::Item) {
@@ -1109,8 +1113,8 @@ struct IdVisitor<'a, 'b: 'a, 'tcx: 'a+'b> {
11091113

11101114
// Output any lints that were previously added to the session.
11111115
impl<'a, 'b, 'tcx> hir_visit::Visitor<'tcx> for IdVisitor<'a, 'b, 'tcx> {
1112-
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, hir_visit::NestedVisitMode)> {
1113-
Some((&self.cx.tcx.map, hir_visit::NestedVisitMode::OnlyBodies))
1116+
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
1117+
Some(&self.cx.tcx.map)
11141118
}
11151119

11161120
fn visit_id(&mut self, id: ast::NodeId) {

src/librustc/middle/dataflow.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ fn build_nodeid_to_index(decl: Option<&hir::FnDecl>,
193193
let mut formals = Formals { entry: entry, index: index };
194194
intravisit::walk_fn_decl(&mut formals, decl);
195195
impl<'a, 'v> intravisit::Visitor<'v> for Formals<'a> {
196+
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'v>> { None }
197+
196198
fn visit_pat(&mut self, p: &hir::Pat) {
197199
self.index.entry(p.id).or_insert(vec![]).push(self.entry);
198200
intravisit::walk_pat(self, p)

src/librustc/middle/dead.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
221221
}
222222

223223
impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
224-
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, NestedVisitMode)> {
225-
Some((&self.tcx.map, NestedVisitMode::OnlyBodies))
224+
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
225+
Some(&self.tcx.map)
226226
}
227227

228228
fn visit_variant_data(&mut self, def: &'tcx hir::VariantData, _: ast::Name,
@@ -510,10 +510,12 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
510510
/// on inner functions when the outer function is already getting
511511
/// an error. We could do this also by checking the parents, but
512512
/// this is how the code is setup and it seems harmless enough.
513-
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, NestedVisitMode)> {
514-
Some((&self.tcx.map, NestedVisitMode::All))
513+
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
514+
Some(&self.tcx.map)
515515
}
516516

517+
fn nested_visit_mode(&mut self) -> NestedVisitMode { NestedVisitMode::All }
518+
517519
fn visit_item(&mut self, item: &'tcx hir::Item) {
518520
if self.should_warn_about_item(item) {
519521
self.warn_dead_code(

src/librustc/middle/effect.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use syntax::ast;
2121
use syntax_pos::Span;
2222
use hir::{self, PatKind};
2323
use hir::def::Def;
24-
use hir::intravisit::{self, FnKind, Visitor, NestedVisitMode};
24+
use hir::intravisit::{self, FnKind, Visitor};
2525

2626
#[derive(Copy, Clone)]
2727
struct UnsafeContext {
@@ -93,8 +93,8 @@ impl<'a, 'tcx> EffectCheckVisitor<'a, 'tcx> {
9393
}
9494

9595
impl<'a, 'tcx> Visitor<'tcx> for EffectCheckVisitor<'a, 'tcx> {
96-
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, NestedVisitMode)> {
97-
Some((&self.tcx.map, NestedVisitMode::OnlyBodies))
96+
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
97+
Some(&self.tcx.map)
9898
}
9999

100100
fn visit_fn(&mut self, fn_kind: FnKind<'tcx>, fn_decl: &'tcx hir::FnDecl,

src/librustc/middle/intrinsicck.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use ty::layout::{LayoutError, Pointer, SizeSkeleton};
1919
use syntax::abi::Abi::RustIntrinsic;
2020
use syntax::ast;
2121
use syntax_pos::Span;
22-
use hir::intravisit::{self, Visitor, FnKind, NestedVisitMode};
22+
use hir::intravisit::{self, Visitor, FnKind};
2323
use hir;
2424

2525
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
@@ -117,8 +117,8 @@ impl<'a, 'gcx, 'tcx> ExprVisitor<'a, 'gcx, 'tcx> {
117117
}
118118

119119
impl<'a, 'tcx> Visitor<'tcx> for ItemVisitor<'a, 'tcx> {
120-
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, NestedVisitMode)> {
121-
Some((&self.tcx.map, NestedVisitMode::OnlyBodies))
120+
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
121+
Some(&self.tcx.map)
122122
}
123123

124124
// const, static and N in [T; N].
@@ -163,8 +163,8 @@ impl<'a, 'tcx> Visitor<'tcx> for ItemVisitor<'a, 'tcx> {
163163
}
164164

165165
impl<'a, 'gcx, 'tcx> Visitor<'gcx> for ExprVisitor<'a, 'gcx, 'tcx> {
166-
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'gcx>, NestedVisitMode)> {
167-
Some((&self.infcx.tcx.map, NestedVisitMode::OnlyBodies))
166+
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'gcx>> {
167+
Some(&self.infcx.tcx.map)
168168
}
169169

170170
fn visit_expr(&mut self, expr: &'gcx hir::Expr) {

src/librustc/middle/liveness.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ use syntax_pos::Span;
128128
use hir::Expr;
129129
use hir;
130130
use hir::print::{expr_to_string, block_to_string};
131-
use hir::intravisit::{self, Visitor, FnKind, NestedVisitMode};
131+
use hir::intravisit::{self, Visitor, FnKind};
132132

133133
/// For use with `propagate_through_loop`.
134134
enum LoopKind<'a> {
@@ -183,8 +183,8 @@ fn live_node_kind_to_string(lnk: LiveNodeKind, tcx: TyCtxt) -> String {
183183
}
184184

185185
impl<'a, 'tcx> Visitor<'tcx> for IrMaps<'a, 'tcx> {
186-
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, NestedVisitMode)> {
187-
Some((&self.tcx.map, NestedVisitMode::OnlyBodies))
186+
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
187+
Some(&self.tcx.map)
188188
}
189189
fn visit_fn(&mut self, fk: FnKind<'tcx>, fd: &'tcx hir::FnDecl,
190190
b: hir::ExprId, s: Span, id: NodeId) {
@@ -352,8 +352,8 @@ impl<'a, 'tcx> IrMaps<'a, 'tcx> {
352352
}
353353

354354
impl<'a, 'tcx> Visitor<'tcx> for Liveness<'a, 'tcx> {
355-
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, NestedVisitMode)> {
356-
Some((&self.ir.tcx.map, NestedVisitMode::OnlyBodies))
355+
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
356+
Some(&self.ir.tcx.map)
357357
}
358358
fn visit_fn(&mut self, _: FnKind<'tcx>, _: &'tcx hir::FnDecl,
359359
_: hir::ExprId, _: Span, _: NodeId) {

src/librustc/middle/reachable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use syntax::abi::Abi;
2828
use syntax::ast;
2929
use syntax::attr;
3030
use hir;
31-
use hir::intravisit::{Visitor, NestedVisitMode};
31+
use hir::intravisit::{Visitor};
3232
use hir::itemlikevisit::ItemLikeVisitor;
3333
use hir::intravisit;
3434

@@ -89,8 +89,8 @@ struct ReachableContext<'a, 'tcx: 'a> {
8989
}
9090

9191
impl<'a, 'tcx> Visitor<'tcx> for ReachableContext<'a, 'tcx> {
92-
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, NestedVisitMode)> {
93-
Some((&self.tcx.map, NestedVisitMode::OnlyBodies))
92+
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
93+
Some(&self.tcx.map)
9494
}
9595

9696
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {

0 commit comments

Comments
 (0)