Skip to content

Commit

Permalink
Split nested_visit_mode function off from nested_visit_map
Browse files Browse the repository at this point in the history
... and make the latter mandatory to implement.
  • Loading branch information
flodiebold committed Nov 29, 2016
1 parent 725cffb commit f0ce5bb
Show file tree
Hide file tree
Showing 35 changed files with 160 additions and 110 deletions.
40 changes: 21 additions & 19 deletions src/librustc/hir/intravisit.rs
Expand Up @@ -95,23 +95,26 @@ pub trait Visitor<'v> : Sized {
///////////////////////////////////////////////////////////////////////////
// Nested items.

/// The default versions of the `visit_nested_XXX` routines invoke
/// this method to get a map to use; if they get back `None`, they
/// just skip nested things. Otherwise, they will lookup the
/// nested item-like things in the map and visit it. So the best
/// way to implement a nested visitor is to override this method
/// to return a `Map`; one advantage of this is that if we add
/// more types of nested things in the future, they will
/// automatically work.
/// The default versions of the `visit_nested_XXX` routines invoke this
/// method to get a map to use; if they get back `None`, they just skip
/// nested things. Otherwise, they will lookup the nested thing in the map
/// and visit it depending on what `nested_visit_mode` returns. So the best
/// way to implement a nested visitor is to override this method to return a
/// `Map`; one advantage of this is that if we add more types of nested
/// things in the future, they will automatically work.
///
/// **If for some reason you want the nested behavior, but don't
/// have a `Map` are your disposal:** then you should override the
/// `visit_nested_XXX` methods, and override this method to
/// `panic!()`. This way, if a new `visit_nested_XXX` variant is
/// added in the future, we will see the panic in your code and
/// fix it appropriately.
fn nested_visit_map(&mut self) -> Option<(&Map<'v>, NestedVisitMode)> {
None
fn nested_visit_map(&mut self) -> Option<&Map<'v>>;

/// Specifies what things nested things this visitor wants to visit. By
/// default, bodies will be visited, but not nested items.
fn nested_visit_mode(&mut self) -> NestedVisitMode {
NestedVisitMode::OnlyBodies
}

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

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

fn map_for_item<'v, V: Visitor<'v>>(visitor: &mut V) -> Option<&Map<'v>> {
visitor.nested_visit_map().and_then(|(map, mode)| {
match mode {
NestedVisitMode::OnlyBodies => None,
NestedVisitMode::All => Some(map)
}
})
match visitor.nested_visit_mode() {
NestedVisitMode::OnlyBodies => None,
NestedVisitMode::All => Some(visitor.nested_visit_map()
.expect("NestedVisitMode::All without nested_visit_map"))
}
}

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

impl<'a, 'ast> Visitor<'ast> for IdRangeComputingVisitor<'a, 'ast> {
fn nested_visit_map(&mut self) -> Option<(&Map<'ast>, NestedVisitMode)> {
Some((&self.map, NestedVisitMode::OnlyBodies))
fn nested_visit_map(&mut self) -> Option<&Map<'ast>> {
Some(&self.map)
}

fn visit_id(&mut self, id: NodeId) {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/map/collector.rs
Expand Up @@ -10,7 +10,7 @@

use super::*;

use hir::intravisit::{Visitor, NestedVisitMode};
use hir::intravisit::Visitor;
use hir::def_id::DefId;
use middle::cstore::InlinedItem;
use std::iter::repeat;
Expand Down Expand Up @@ -91,7 +91,7 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
/// deep walking so that we walk nested items in the context of
/// their outer items.

fn nested_visit_map(&mut self) -> Option<(&map::Map<'ast>, NestedVisitMode)> {
fn nested_visit_map(&mut self) -> Option<&map::Map<'ast>> {
panic!("visit_nested_xxx must be manually implemented in this visitor")
}

Expand Down
4 changes: 4 additions & 0 deletions src/librustc/hir/map/def_collector.rs
Expand Up @@ -327,6 +327,10 @@ impl<'a> visit::Visitor for DefCollector<'a> {

// We walk the HIR rather than the AST when reading items from metadata.
impl<'ast> intravisit::Visitor<'ast> for DefCollector<'ast> {
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'ast>> {
None
}

fn visit_body(&mut self, id: hir::ExprId) {
if let Some(krate) = self.hir_crate {
self.visit_expr(krate.expr(id));
Expand Down
12 changes: 8 additions & 4 deletions src/librustc/lint/context.rs
Expand Up @@ -791,8 +791,12 @@ impl<'a, 'tcx> hir_visit::Visitor<'tcx> for LateContext<'a, 'tcx> {
/// Because lints are scoped lexically, we want to walk nested
/// items in the context of the outer item, so enable
/// deep-walking.
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, hir_visit::NestedVisitMode)> {
Some((&self.tcx.map, hir_visit::NestedVisitMode::All))
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
Some(&self.tcx.map)
}

fn nested_visit_mode(&mut self) -> hir_visit::NestedVisitMode {
hir_visit::NestedVisitMode::All
}

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

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

fn visit_id(&mut self, id: ast::NodeId) {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/middle/dataflow.rs
Expand Up @@ -193,6 +193,8 @@ fn build_nodeid_to_index(decl: Option<&hir::FnDecl>,
let mut formals = Formals { entry: entry, index: index };
intravisit::walk_fn_decl(&mut formals, decl);
impl<'a, 'v> intravisit::Visitor<'v> for Formals<'a> {
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'v>> { None }

fn visit_pat(&mut self, p: &hir::Pat) {
self.index.entry(p.id).or_insert(vec![]).push(self.entry);
intravisit::walk_pat(self, p)
Expand Down
10 changes: 6 additions & 4 deletions src/librustc/middle/dead.rs
Expand Up @@ -221,8 +221,8 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
}

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

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

fn nested_visit_mode(&mut self) -> NestedVisitMode { NestedVisitMode::All }

fn visit_item(&mut self, item: &'tcx hir::Item) {
if self.should_warn_about_item(item) {
self.warn_dead_code(
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/effect.rs
Expand Up @@ -21,7 +21,7 @@ use syntax::ast;
use syntax_pos::Span;
use hir::{self, PatKind};
use hir::def::Def;
use hir::intravisit::{self, FnKind, Visitor, NestedVisitMode};
use hir::intravisit::{self, FnKind, Visitor};

#[derive(Copy, Clone)]
struct UnsafeContext {
Expand Down Expand Up @@ -93,8 +93,8 @@ impl<'a, 'tcx> EffectCheckVisitor<'a, 'tcx> {
}

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

fn visit_fn(&mut self, fn_kind: FnKind<'tcx>, fn_decl: &'tcx hir::FnDecl,
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/middle/intrinsicck.rs
Expand Up @@ -19,7 +19,7 @@ use ty::layout::{LayoutError, Pointer, SizeSkeleton};
use syntax::abi::Abi::RustIntrinsic;
use syntax::ast;
use syntax_pos::Span;
use hir::intravisit::{self, Visitor, FnKind, NestedVisitMode};
use hir::intravisit::{self, Visitor, FnKind};
use hir;

pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
Expand Down Expand Up @@ -117,8 +117,8 @@ impl<'a, 'gcx, 'tcx> ExprVisitor<'a, 'gcx, 'tcx> {
}

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

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

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

fn visit_expr(&mut self, expr: &'gcx hir::Expr) {
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/middle/liveness.rs
Expand Up @@ -128,7 +128,7 @@ use syntax_pos::Span;
use hir::Expr;
use hir;
use hir::print::{expr_to_string, block_to_string};
use hir::intravisit::{self, Visitor, FnKind, NestedVisitMode};
use hir::intravisit::{self, Visitor, FnKind};

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

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

impl<'a, 'tcx> Visitor<'tcx> for Liveness<'a, 'tcx> {
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, NestedVisitMode)> {
Some((&self.ir.tcx.map, NestedVisitMode::OnlyBodies))
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
Some(&self.ir.tcx.map)
}
fn visit_fn(&mut self, _: FnKind<'tcx>, _: &'tcx hir::FnDecl,
_: hir::ExprId, _: Span, _: NodeId) {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/reachable.rs
Expand Up @@ -28,7 +28,7 @@ use syntax::abi::Abi;
use syntax::ast;
use syntax::attr;
use hir;
use hir::intravisit::{Visitor, NestedVisitMode};
use hir::intravisit::{Visitor};
use hir::itemlikevisit::ItemLikeVisitor;
use hir::intravisit;

Expand Down Expand Up @@ -89,8 +89,8 @@ struct ReachableContext<'a, 'tcx: 'a> {
}

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

fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/region.rs
Expand Up @@ -31,7 +31,7 @@ use syntax::ast::{self, NodeId};
use syntax_pos::Span;

use hir;
use hir::intravisit::{self, Visitor, FnKind, NestedVisitMode};
use hir::intravisit::{self, Visitor, FnKind};
use hir::{Block, Item, FnDecl, Arm, Pat, PatKind, Stmt, Expr, Local};

#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, RustcEncodable,
Expand Down Expand Up @@ -1170,8 +1170,8 @@ impl<'ast, 'a> RegionResolutionVisitor<'ast, 'a> {
}

impl<'ast, 'a> Visitor<'ast> for RegionResolutionVisitor<'ast, 'a> {
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'ast>, NestedVisitMode)> {
Some((&self.map, NestedVisitMode::OnlyBodies))
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'ast>> {
Some(&self.map)
}

fn visit_block(&mut self, b: &'ast Block) {
Expand Down
12 changes: 10 additions & 2 deletions src/librustc/middle/resolve_lifetime.rs
Expand Up @@ -132,10 +132,12 @@ pub fn krate(sess: &Session,
impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
// Override the nested functions -- lifetimes follow lexical scope,
// so it's convenient to walk the tree in lexical order.
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, NestedVisitMode)> {
Some((&self.hir_map, NestedVisitMode::All))
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
Some(&self.hir_map)
}

fn nested_visit_mode(&mut self) -> NestedVisitMode { NestedVisitMode::All }

fn visit_item(&mut self, item: &'tcx hir::Item) {
// Save labels for nested items.
let saved_labels_in_fn = replace(&mut self.labels_in_fn, vec![]);
Expand Down Expand Up @@ -423,6 +425,8 @@ fn extract_labels(ctxt: &mut LifetimeContext, b: hir::ExprId) {
return;

impl<'v, 'a> Visitor<'v> for GatherLabels<'a> {
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'v>> { None }

fn visit_expr(&mut self, ex: &'v hir::Expr) {
// do not recurse into closures defined in the block
// since they are treated as separate fns from the POV of
Expand Down Expand Up @@ -938,6 +942,8 @@ fn insert_late_bound_lifetimes(map: &mut NamedRegionMap,
}

impl<'v> Visitor<'v> for ConstrainedCollector {
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'v>> { None }

fn visit_ty(&mut self, ty: &'v hir::Ty) {
match ty.node {
hir::TyPath(hir::QPath::Resolved(Some(_), _)) |
Expand Down Expand Up @@ -975,6 +981,8 @@ fn insert_late_bound_lifetimes(map: &mut NamedRegionMap,
}

impl<'v> Visitor<'v> for AllCollector {
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'v>> { None }

fn visit_lifetime(&mut self, lifetime_ref: &'v hir::Lifetime) {
self.regions.insert(lifetime_ref.name);
}
Expand Down
12 changes: 8 additions & 4 deletions src/librustc/middle/stability.rs
Expand Up @@ -234,10 +234,12 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
/// Because stability levels are scoped lexically, we want to walk
/// nested items in the context of the outer item, so enable
/// deep-walking.
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, NestedVisitMode)> {
Some((&self.tcx.map, NestedVisitMode::All))
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
Some(&self.tcx.map)
}

fn nested_visit_mode(&mut self) -> NestedVisitMode { NestedVisitMode::All }

fn visit_item(&mut self, i: &'tcx Item) {
let orig_in_trait_impl = self.in_trait_impl;
let mut kind = AnnotationKind::Required;
Expand Down Expand Up @@ -534,10 +536,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, NestedVisitMode)> {
Some((&self.tcx.map, NestedVisitMode::OnlyBodies))
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
Some(&self.tcx.map)
}

fn nested_visit_mode(&mut self) -> NestedVisitMode { NestedVisitMode::OnlyBodies }

fn visit_item(&mut self, item: &'tcx hir::Item) {
match item.node {
hir::ItemExternCrate(_) => {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/middle/weak_lang_items.rs
Expand Up @@ -125,6 +125,8 @@ impl<'a> Context<'a> {
}

impl<'a, 'v> Visitor<'v> for Context<'a> {
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'v>> { None }

fn visit_foreign_item(&mut self, i: &hir::ForeignItem) {
if let Some(lang_item) = lang_items::extract(&i.attrs) {
self.register(&lang_item.as_str(), i.span);
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_borrowck/borrowck/gather_loans/mod.rs
Expand Up @@ -30,7 +30,7 @@ use syntax_pos::Span;
use rustc::hir;
use rustc::hir::Expr;
use rustc::hir::intravisit;
use rustc::hir::intravisit::{Visitor, NestedVisitMode};
use rustc::hir::intravisit::{Visitor};

use self::restrictions::RestrictionResult;

Expand Down Expand Up @@ -521,8 +521,8 @@ struct StaticInitializerCtxt<'a, 'tcx: 'a> {
}

impl<'a, 'tcx> Visitor<'tcx> for StaticInitializerCtxt<'a, 'tcx> {
fn nested_visit_map(&mut self) -> Option<(&hir::map::Map<'tcx>, NestedVisitMode)> {
Some((&self.bccx.tcx.map, NestedVisitMode::OnlyBodies))
fn nested_visit_map(&mut self) -> Option<&hir::map::Map<'tcx>> {
Some(&self.bccx.tcx.map)
}

fn visit_expr(&mut self, ex: &'tcx Expr) {
Expand Down

0 comments on commit f0ce5bb

Please sign in to comment.