Split nested_visit_mode function off from nested_visit_map

... and make the latter mandatory to implement.
This commit is contained in:
Florian Diebold 2016-11-24 20:15:11 +01:00 committed by Florian Diebold
parent 725cffb1d5
commit f0ce5bb66b
35 changed files with 160 additions and 110 deletions

View file

@ -95,14 +95,13 @@ 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
@ -110,8 +109,12 @@ pub trait Visitor<'v> : Sized {
/// `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
@ -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>) {
@ -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) {

View file

@ -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;
@ -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")
}

View file

@ -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));

View file

@ -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) {
@ -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) {

View file

@ -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)

View file

@ -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,
@ -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(

View file

@ -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 {
@ -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,

View file

@ -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>) {
@ -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].
@ -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) {

View file

@ -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> {
@ -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) {
@ -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) {

View file

@ -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;
@ -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) {

View file

@ -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,
@ -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) {

View file

@ -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![]);
@ -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
@ -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(_), _)) |
@ -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);
}

View file

@ -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;
@ -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(_) => {

View file

@ -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);