Skip to content

Commit

Permalink
Move lints to HIR
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Sep 3, 2015
1 parent 1661947 commit 7732ad8
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 337 deletions.
91 changes: 44 additions & 47 deletions src/librustc/lint/context.rs
Expand Up @@ -37,12 +37,14 @@ use std::cell::RefCell;
use std::cmp;
use std::mem;
use syntax::ast_util::IdVisitingOperation;
use syntax::attr::AttrMetaMethods;
use syntax::attr;
use rustc_front::attr::{self, AttrMetaMethods};
use rustc_front::util;
use syntax::codemap::Span;
use syntax::visit::{Visitor, FnKind};
use syntax::parse::token::InternedString;
use syntax::{ast, ast_util, visit};
use syntax::ast;
use rustc_front::hir;
use rustc_front::visit::{self, Visitor, FnKind};
use syntax::visit::Visitor as SyntaxVisitor;
use syntax::diagnostic;

/// Information about the registered lints.
Expand Down Expand Up @@ -252,7 +254,7 @@ pub struct Context<'a, 'tcx: 'a> {
pub tcx: &'a ty::ctxt<'tcx>,

/// The crate being checked.
pub krate: &'a ast::Crate,
pub krate: &'a hir::Crate,

/// Items exported from the crate being checked.
pub exported_items: &'a ExportedItems,
Expand Down Expand Up @@ -284,7 +286,7 @@ macro_rules! run_lints { ($cx:expr, $f:ident, $($args:expr),*) => ({
/// Parse the lint attributes into a vector, with `Err`s for malformed lint
/// attributes. Writing this as an iterator is an enormous mess.
// See also the hir version just below.
pub fn gather_attrs(attrs: &[ast::Attribute])
pub fn gather_attrs(attrs: &[hir::Attribute])
-> Vec<Result<(InternedString, Level, Span), Span>> {
let mut out = vec!();
for attr in attrs {
Expand All @@ -297,7 +299,7 @@ pub fn gather_attrs(attrs: &[ast::Attribute])

let meta = &attr.node.value;
let metas = match meta.node {
ast::MetaList(_, ref metas) => metas,
hir::MetaList(_, ref metas) => metas,
_ => {
out.push(Err(meta.span));
continue;
Expand All @@ -306,7 +308,7 @@ pub fn gather_attrs(attrs: &[ast::Attribute])

for meta in metas {
out.push(match meta.node {
ast::MetaWord(ref lint_name) => Ok((lint_name.clone(), level, meta.span)),
hir::MetaWord(ref lint_name) => Ok((lint_name.clone(), level, meta.span)),
_ => Err(meta.span),
});
}
Expand Down Expand Up @@ -398,7 +400,7 @@ pub fn raw_emit_lint(sess: &Session, lint: &'static Lint,

impl<'a, 'tcx> Context<'a, 'tcx> {
fn new(tcx: &'a ty::ctxt<'tcx>,
krate: &'a ast::Crate,
krate: &'a hir::Crate,
exported_items: &'a ExportedItems) -> Context<'a, 'tcx> {
// We want to own the lint store, so move it out of the session.
let lint_store = mem::replace(&mut *tcx.sess.lint_store.borrow_mut(),
Expand Down Expand Up @@ -452,7 +454,7 @@ impl<'a, 'tcx> Context<'a, 'tcx> {
/// current lint context, call the provided function, then reset the
/// lints in effect to their previous state.
fn with_lint_attrs<F>(&mut self,
attrs: &[ast::Attribute],
attrs: &[hir::Attribute],
f: F) where
F: FnOnce(&mut Context),
{
Expand Down Expand Up @@ -519,9 +521,9 @@ impl<'a, 'tcx> Context<'a, 'tcx> {
}

fn visit_ids<F>(&mut self, f: F) where
F: FnOnce(&mut ast_util::IdVisitor<Context>)
F: FnOnce(&mut util::IdVisitor<Context>)
{
let mut v = ast_util::IdVisitor {
let mut v = util::IdVisitor {
operation: self,
pass_through_items: false,
visited_outermost: false,
Expand All @@ -531,68 +533,68 @@ impl<'a, 'tcx> Context<'a, 'tcx> {
}

impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
fn visit_item(&mut self, it: &ast::Item) {
fn visit_item(&mut self, it: &hir::Item) {
self.with_lint_attrs(&it.attrs, |cx| {
run_lints!(cx, check_item, it);
cx.visit_ids(|v| v.visit_item(it));
visit::walk_item(cx, it);
})
}

fn visit_foreign_item(&mut self, it: &ast::ForeignItem) {
fn visit_foreign_item(&mut self, it: &hir::ForeignItem) {
self.with_lint_attrs(&it.attrs, |cx| {
run_lints!(cx, check_foreign_item, it);
visit::walk_foreign_item(cx, it);
})
}

fn visit_pat(&mut self, p: &ast::Pat) {
fn visit_pat(&mut self, p: &hir::Pat) {
run_lints!(self, check_pat, p);
visit::walk_pat(self, p);
}

fn visit_expr(&mut self, e: &ast::Expr) {
fn visit_expr(&mut self, e: &hir::Expr) {
run_lints!(self, check_expr, e);
visit::walk_expr(self, e);
}

fn visit_stmt(&mut self, s: &ast::Stmt) {
fn visit_stmt(&mut self, s: &hir::Stmt) {
run_lints!(self, check_stmt, s);
visit::walk_stmt(self, s);
}

fn visit_fn(&mut self, fk: FnKind<'v>, decl: &'v ast::FnDecl,
body: &'v ast::Block, span: Span, id: ast::NodeId) {
fn visit_fn(&mut self, fk: FnKind<'v>, decl: &'v hir::FnDecl,
body: &'v hir::Block, span: Span, id: ast::NodeId) {
run_lints!(self, check_fn, fk, decl, body, span, id);
visit::walk_fn(self, fk, decl, body, span);
}

fn visit_struct_def(&mut self,
s: &ast::StructDef,
s: &hir::StructDef,
ident: ast::Ident,
g: &ast::Generics,
g: &hir::Generics,
id: ast::NodeId) {
run_lints!(self, check_struct_def, s, ident, g, id);
visit::walk_struct_def(self, s);
run_lints!(self, check_struct_def_post, s, ident, g, id);
}

fn visit_struct_field(&mut self, s: &ast::StructField) {
fn visit_struct_field(&mut self, s: &hir::StructField) {
self.with_lint_attrs(&s.node.attrs, |cx| {
run_lints!(cx, check_struct_field, s);
visit::walk_struct_field(cx, s);
})
}

fn visit_variant(&mut self, v: &ast::Variant, g: &ast::Generics) {
fn visit_variant(&mut self, v: &hir::Variant, g: &hir::Generics) {
self.with_lint_attrs(&v.node.attrs, |cx| {
run_lints!(cx, check_variant, v, g);
visit::walk_variant(cx, v, g);
run_lints!(cx, check_variant_post, v, g);
})
}

fn visit_ty(&mut self, t: &ast::Ty) {
fn visit_ty(&mut self, t: &hir::Ty) {
run_lints!(self, check_ty, t);
visit::walk_ty(self, t);
}
Expand All @@ -601,84 +603,79 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
run_lints!(self, check_ident, sp, id);
}

fn visit_mod(&mut self, m: &ast::Mod, s: Span, n: ast::NodeId) {
fn visit_mod(&mut self, m: &hir::Mod, s: Span, n: ast::NodeId) {
run_lints!(self, check_mod, m, s, n);
visit::walk_mod(self, m);
}

fn visit_local(&mut self, l: &ast::Local) {
fn visit_local(&mut self, l: &hir::Local) {
run_lints!(self, check_local, l);
visit::walk_local(self, l);
}

fn visit_block(&mut self, b: &ast::Block) {
fn visit_block(&mut self, b: &hir::Block) {
run_lints!(self, check_block, b);
visit::walk_block(self, b);
}

fn visit_arm(&mut self, a: &ast::Arm) {
fn visit_arm(&mut self, a: &hir::Arm) {
run_lints!(self, check_arm, a);
visit::walk_arm(self, a);
}

fn visit_decl(&mut self, d: &ast::Decl) {
fn visit_decl(&mut self, d: &hir::Decl) {
run_lints!(self, check_decl, d);
visit::walk_decl(self, d);
}

fn visit_expr_post(&mut self, e: &ast::Expr) {
fn visit_expr_post(&mut self, e: &hir::Expr) {
run_lints!(self, check_expr_post, e);
}

fn visit_generics(&mut self, g: &ast::Generics) {
fn visit_generics(&mut self, g: &hir::Generics) {
run_lints!(self, check_generics, g);
visit::walk_generics(self, g);
}

fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
fn visit_trait_item(&mut self, trait_item: &hir::TraitItem) {
self.with_lint_attrs(&trait_item.attrs, |cx| {
run_lints!(cx, check_trait_item, trait_item);
cx.visit_ids(|v| v.visit_trait_item(trait_item));
visit::walk_trait_item(cx, trait_item);
});
}

fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
fn visit_impl_item(&mut self, impl_item: &hir::ImplItem) {
self.with_lint_attrs(&impl_item.attrs, |cx| {
run_lints!(cx, check_impl_item, impl_item);
cx.visit_ids(|v| v.visit_impl_item(impl_item));
visit::walk_impl_item(cx, impl_item);
});
}

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

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

fn visit_lifetime_def(&mut self, lt: &ast::LifetimeDef) {
fn visit_lifetime_def(&mut self, lt: &hir::LifetimeDef) {
run_lints!(self, check_lifetime_def, lt);
}

fn visit_explicit_self(&mut self, es: &ast::ExplicitSelf) {
fn visit_explicit_self(&mut self, es: &hir::ExplicitSelf) {
run_lints!(self, check_explicit_self, es);
visit::walk_explicit_self(self, es);
}

fn visit_mac(&mut self, mac: &ast::Mac) {
run_lints!(self, check_mac, mac);
visit::walk_mac(self, mac);
}

fn visit_path(&mut self, p: &ast::Path, id: ast::NodeId) {
fn visit_path(&mut self, p: &hir::Path, id: ast::NodeId) {
run_lints!(self, check_path, p, id);
visit::walk_path(self, p);
}

fn visit_attribute(&mut self, attr: &ast::Attribute) {
fn visit_attribute(&mut self, attr: &hir::Attribute) {
run_lints!(self, check_attribute, attr);
}
}
Expand Down Expand Up @@ -709,9 +706,9 @@ impl LintPass for GatherNodeLevels {
lint_array!()
}

fn check_item(&mut self, cx: &Context, it: &ast::Item) {
fn check_item(&mut self, cx: &Context, it: &hir::Item) {
match it.node {
ast::ItemEnum(..) => {
hir::ItemEnum(..) => {
let lint_id = LintId::of(builtin::VARIANT_SIZE_DIFFERENCES);
let lvlsrc = cx.lints.get_level_source(lint_id);
match lvlsrc {
Expand All @@ -731,7 +728,7 @@ impl LintPass for GatherNodeLevels {
///
/// Consumes the `lint_store` field of the `Session`.
pub fn check_crate(tcx: &ty::ctxt,
krate: &ast::Crate,
krate: &hir::Crate,
exported_items: &ExportedItems) {

let mut cx = Context::new(tcx, krate, exported_items);
Expand Down
63 changes: 32 additions & 31 deletions src/librustc/lint/mod.rs
Expand Up @@ -34,8 +34,9 @@ pub use self::LintSource::*;
use std::hash;
use std::ascii::AsciiExt;
use syntax::codemap::Span;
use syntax::visit::FnKind;
use rustc_front::visit::FnKind;
use syntax::ast;
use rustc_front::hir;

pub use lint::context::{Context, LintStore, raw_emit_lint, check_crate, gather_attrs,
gather_attrs_from_hir, GatherNodeLevels};
Expand Down Expand Up @@ -125,46 +126,46 @@ pub trait LintPass {
/// `Lint`, make it a private `static` item in its own module.
fn get_lints(&self) -> LintArray;

fn check_crate(&mut self, _: &Context, _: &ast::Crate) { }
fn check_crate(&mut self, _: &Context, _: &hir::Crate) { }
fn check_ident(&mut self, _: &Context, _: Span, _: ast::Ident) { }
fn check_mod(&mut self, _: &Context, _: &ast::Mod, _: Span, _: ast::NodeId) { }
fn check_foreign_item(&mut self, _: &Context, _: &ast::ForeignItem) { }
fn check_item(&mut self, _: &Context, _: &ast::Item) { }
fn check_local(&mut self, _: &Context, _: &ast::Local) { }
fn check_block(&mut self, _: &Context, _: &ast::Block) { }
fn check_stmt(&mut self, _: &Context, _: &ast::Stmt) { }
fn check_arm(&mut self, _: &Context, _: &ast::Arm) { }
fn check_pat(&mut self, _: &Context, _: &ast::Pat) { }
fn check_decl(&mut self, _: &Context, _: &ast::Decl) { }
fn check_expr(&mut self, _: &Context, _: &ast::Expr) { }
fn check_expr_post(&mut self, _: &Context, _: &ast::Expr) { }
fn check_ty(&mut self, _: &Context, _: &ast::Ty) { }
fn check_generics(&mut self, _: &Context, _: &ast::Generics) { }
fn check_mod(&mut self, _: &Context, _: &hir::Mod, _: Span, _: ast::NodeId) { }
fn check_foreign_item(&mut self, _: &Context, _: &hir::ForeignItem) { }
fn check_item(&mut self, _: &Context, _: &hir::Item) { }
fn check_local(&mut self, _: &Context, _: &hir::Local) { }
fn check_block(&mut self, _: &Context, _: &hir::Block) { }
fn check_stmt(&mut self, _: &Context, _: &hir::Stmt) { }
fn check_arm(&mut self, _: &Context, _: &hir::Arm) { }
fn check_pat(&mut self, _: &Context, _: &hir::Pat) { }
fn check_decl(&mut self, _: &Context, _: &hir::Decl) { }
fn check_expr(&mut self, _: &Context, _: &hir::Expr) { }
fn check_expr_post(&mut self, _: &Context, _: &hir::Expr) { }
fn check_ty(&mut self, _: &Context, _: &hir::Ty) { }
fn check_generics(&mut self, _: &Context, _: &hir::Generics) { }
fn check_fn(&mut self, _: &Context,
_: FnKind, _: &ast::FnDecl, _: &ast::Block, _: Span, _: ast::NodeId) { }
fn check_trait_item(&mut self, _: &Context, _: &ast::TraitItem) { }
fn check_impl_item(&mut self, _: &Context, _: &ast::ImplItem) { }
_: FnKind, _: &hir::FnDecl, _: &hir::Block, _: Span, _: ast::NodeId) { }
fn check_trait_item(&mut self, _: &Context, _: &hir::TraitItem) { }
fn check_impl_item(&mut self, _: &Context, _: &hir::ImplItem) { }
fn check_struct_def(&mut self, _: &Context,
_: &ast::StructDef, _: ast::Ident, _: &ast::Generics, _: ast::NodeId) { }
_: &hir::StructDef, _: ast::Ident, _: &hir::Generics, _: ast::NodeId) { }
fn check_struct_def_post(&mut self, _: &Context,
_: &ast::StructDef, _: ast::Ident, _: &ast::Generics, _: ast::NodeId) { }
fn check_struct_field(&mut self, _: &Context, _: &ast::StructField) { }
fn check_variant(&mut self, _: &Context, _: &ast::Variant, _: &ast::Generics) { }
fn check_variant_post(&mut self, _: &Context, _: &ast::Variant, _: &ast::Generics) { }
fn check_opt_lifetime_ref(&mut self, _: &Context, _: Span, _: &Option<ast::Lifetime>) { }
fn check_lifetime_ref(&mut self, _: &Context, _: &ast::Lifetime) { }
fn check_lifetime_def(&mut self, _: &Context, _: &ast::LifetimeDef) { }
fn check_explicit_self(&mut self, _: &Context, _: &ast::ExplicitSelf) { }
_: &hir::StructDef, _: ast::Ident, _: &hir::Generics, _: ast::NodeId) { }
fn check_struct_field(&mut self, _: &Context, _: &hir::StructField) { }
fn check_variant(&mut self, _: &Context, _: &hir::Variant, _: &hir::Generics) { }
fn check_variant_post(&mut self, _: &Context, _: &hir::Variant, _: &hir::Generics) { }
fn check_opt_lifetime_ref(&mut self, _: &Context, _: Span, _: &Option<hir::Lifetime>) { }
fn check_lifetime_ref(&mut self, _: &Context, _: &hir::Lifetime) { }
fn check_lifetime_def(&mut self, _: &Context, _: &hir::LifetimeDef) { }
fn check_explicit_self(&mut self, _: &Context, _: &hir::ExplicitSelf) { }
fn check_mac(&mut self, _: &Context, _: &ast::Mac) { }
fn check_path(&mut self, _: &Context, _: &ast::Path, _: ast::NodeId) { }
fn check_attribute(&mut self, _: &Context, _: &ast::Attribute) { }
fn check_path(&mut self, _: &Context, _: &hir::Path, _: ast::NodeId) { }
fn check_attribute(&mut self, _: &Context, _: &hir::Attribute) { }

/// Called when entering a syntax node that can have lint attributes such
/// as `#[allow(...)]`. Called with *all* the attributes of that node.
fn enter_lint_attrs(&mut self, _: &Context, _: &[ast::Attribute]) { }
fn enter_lint_attrs(&mut self, _: &Context, _: &[hir::Attribute]) { }

/// Counterpart to `enter_lint_attrs`.
fn exit_lint_attrs(&mut self, _: &Context, _: &[ast::Attribute]) { }
fn exit_lint_attrs(&mut self, _: &Context, _: &[hir::Attribute]) { }
}

/// A lint pass boxed up as a trait object.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_driver/driver.rs
Expand Up @@ -761,7 +761,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: Session,
&tcx.sess, lib_features_used));

time(time_passes, "lint checking", ||
lint::check_crate(tcx, ast_crate, &exported_items));
lint::check_crate(tcx, &lower_crate(ast_crate), &exported_items));

// The above three passes generate errors w/o aborting
tcx.sess.abort_if_errors();
Expand Down

0 comments on commit 7732ad8

Please sign in to comment.