Skip to content

Commit

Permalink
Fill in some missing parts in the default AST visitor
Browse files Browse the repository at this point in the history
+ Add helper macro for walking lists (including Options)
  • Loading branch information
petrochenkov committed Sep 28, 2015
1 parent 9e11845 commit eedb951
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 357 deletions.
8 changes: 2 additions & 6 deletions src/librustc/lint/context.rs
Expand Up @@ -898,12 +898,8 @@ impl<'a, 'v> ast_visit::Visitor<'v> for EarlyContext<'a> {
});
}

fn visit_opt_lifetime_ref(&mut self, sp: Span, lt: &Option<ast::Lifetime>) {
run_lints!(self, check_opt_lifetime_ref, early_passes, sp, lt);
}

fn visit_lifetime_ref(&mut self, lt: &ast::Lifetime) {
run_lints!(self, check_lifetime_ref, early_passes, lt);
fn visit_lifetime(&mut self, lt: &ast::Lifetime) {
run_lints!(self, check_lifetime, early_passes, lt);
}

fn visit_lifetime_def(&mut self, lt: &ast::LifetimeDef) {
Expand Down
6 changes: 1 addition & 5 deletions src/librustc/lint/mod.rs
Expand Up @@ -199,11 +199,7 @@ pub trait EarlyLintPass: LintPass {
fn check_struct_field(&mut self, _: &EarlyContext, _: &ast::StructField) { }
fn check_variant(&mut self, _: &EarlyContext, _: &ast::Variant, _: &ast::Generics) { }
fn check_variant_post(&mut self, _: &EarlyContext, _: &ast::Variant, _: &ast::Generics) { }
fn check_opt_lifetime_ref(&mut self,
_: &EarlyContext,
_: Span,
_: &Option<ast::Lifetime>) { }
fn check_lifetime_ref(&mut self, _: &EarlyContext, _: &ast::Lifetime) { }
fn check_lifetime(&mut self, _: &EarlyContext, _: &ast::Lifetime) { }
fn check_lifetime_def(&mut self, _: &EarlyContext, _: &ast::LifetimeDef) { }
fn check_explicit_self(&mut self, _: &EarlyContext, _: &ast::ExplicitSelf) { }
fn check_path(&mut self, _: &EarlyContext, _: &ast::Path, _: ast::NodeId) { }
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_trans/save/dump_csv.rs
Expand Up @@ -771,7 +771,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
}
}

visit::walk_expr_opt(self, base)
walk_list!(self, visit_expr, base);
}

fn process_method_call(&mut self, ex: &ast::Expr, args: &Vec<P<ast::Expr>>) {
Expand All @@ -785,7 +785,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
}

// walk receiver and args
visit::walk_exprs(self, &args);
walk_list!(self, visit_expr, args);
}

fn process_pat(&mut self, p: &ast::Pat) {
Expand Down Expand Up @@ -1200,7 +1200,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DumpCsvVisitor<'l, 'tcx> {
for &(id, ref path, ref_kind) in &paths_to_process {
self.process_path(id, path, ref_kind);
}
visit::walk_expr_opt(self, &arm.guard);
walk_list!(self, visit_expr, &arm.guard);
self.visit_expr(&arm.body);
}

Expand Down Expand Up @@ -1246,7 +1246,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DumpCsvVisitor<'l, 'tcx> {
}

// Just walk the initialiser and type (don't want to walk the pattern again).
visit::walk_ty_opt(self, &l.ty);
visit::walk_expr_opt(self, &l.init);
walk_list!(self, visit_ty, &l.ty);
walk_list!(self, visit_expr, &l.init);
}
}
7 changes: 7 additions & 0 deletions src/libsyntax/ast.rs
Expand Up @@ -1613,6 +1613,13 @@ impl PathListItem_ {
}
}

pub fn name(&self) -> Option<Ident> {
match *self {
PathListIdent { name, .. } => Some(name),
PathListMod { .. } => None,
}
}

pub fn rename(&self) -> Option<Ident> {
match *self {
PathListIdent { rename, .. } | PathListMod { rename, .. } => rename
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ast_util.rs
Expand Up @@ -476,12 +476,12 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
visit::walk_impl_item(self, ii);
}

fn visit_lifetime_ref(&mut self, lifetime: &Lifetime) {
fn visit_lifetime(&mut self, lifetime: &Lifetime) {
self.operation.visit_id(lifetime.id);
}

fn visit_lifetime_def(&mut self, def: &LifetimeDef) {
self.visit_lifetime_ref(&def.lifetime);
self.visit_lifetime(&def.lifetime);
}

fn visit_trait_ref(&mut self, trait_ref: &TraitRef) {
Expand Down
9 changes: 9 additions & 0 deletions src/libsyntax/owned_slice.rs
Expand Up @@ -12,6 +12,7 @@ use std::default::Default;
use std::fmt;
use std::iter::{IntoIterator, FromIterator};
use std::ops::Deref;
use std::slice;
use std::vec;
use serialize::{Encodable, Decodable, Encoder, Decoder};

Expand Down Expand Up @@ -82,6 +83,14 @@ impl<T> FromIterator<T> for OwnedSlice<T> {
}
}

impl<'a, T> IntoIterator for &'a OwnedSlice<T> {
type Item = &'a T;
type IntoIter = slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.data.into_iter()
}
}

impl<T: Encodable> Encodable for OwnedSlice<T> {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
Encodable::encode(&**self, s)
Expand Down

0 comments on commit eedb951

Please sign in to comment.