From 3a350e1a39a47f33653be1ced1f3ccb940adeb54 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 30 Dec 2019 14:11:49 +0100 Subject: [PATCH 1/3] Move early lint machanism in librustc_lint. --- src/librustc/lint/context.rs | 372 +----------------------------- src/librustc/lint/mod.rs | 8 +- src/librustc_interface/passes.rs | 4 +- src/librustc_lint/early.rs | 383 +++++++++++++++++++++++++++++++ src/librustc_lint/lib.rs | 2 + 5 files changed, 399 insertions(+), 370 deletions(-) create mode 100644 src/librustc_lint/early.rs diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index 6e0bb3a2a2b38..ad6bdae1697e0 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -23,7 +23,7 @@ use crate::hir::intravisit::Visitor; use crate::hir::map::{definitions::DisambiguatedDefPathData, DefPathData}; use crate::lint::builtin::BuiltinLintDiagnostics; use crate::lint::levels::{LintLevelSets, LintLevelsBuilder}; -use crate::lint::{EarlyLintPass, EarlyLintPassObject, LateLintPass, LateLintPassObject}; +use crate::lint::{EarlyLintPassObject, LateLintPass, LateLintPassObject}; use crate::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId, LintPass}; use crate::middle::privacy::AccessLevels; use crate::session::Session; @@ -38,7 +38,6 @@ use rustc_span::{symbol::Symbol, MultiSpan, Span}; use std::slice; use syntax::ast; use syntax::util::lev_distance::find_best_match_for_name; -use syntax::visit as ast_visit; use rustc_error_codes::*; @@ -56,8 +55,8 @@ pub struct LintStore { /// interior mutability, we don't enforce this (and lints should, in theory, /// be compatible with being constructed more than once, though not /// necessarily in a sane manner. This is safe though.) - pre_expansion_passes: Vec EarlyLintPassObject + sync::Send + sync::Sync>>, - early_passes: Vec EarlyLintPassObject + sync::Send + sync::Sync>>, + pub pre_expansion_passes: Vec EarlyLintPassObject + sync::Send + sync::Sync>>, + pub early_passes: Vec EarlyLintPassObject + sync::Send + sync::Sync>>, late_passes: Vec LateLintPassObject + sync::Send + sync::Sync>>, /// This is unique in that we construct them per-module, so not once. late_module_passes: Vec LateLintPassObject + sync::Send + sync::Sync>>, @@ -455,7 +454,7 @@ pub struct LateContext<'a, 'tcx> { pub generics: Option<&'tcx hir::Generics<'tcx>>, /// We are only looking at one module - only_module: bool, + pub only_module: bool, } pub struct LateContextAndPass<'a, 'tcx, T: LateLintPass<'a, 'tcx>> { @@ -472,17 +471,12 @@ pub struct EarlyContext<'a> { /// The crate being checked. pub krate: &'a ast::Crate, - builder: LintLevelsBuilder<'a>, + pub builder: LintLevelsBuilder<'a>, /// The store of registered lints and the lint levels. - lint_store: &'a LintStore, + pub lint_store: &'a LintStore, - buffered: LintBuffer, -} - -pub struct EarlyContextAndPass<'a, T: EarlyLintPass> { - context: EarlyContext<'a>, - pass: T, + pub buffered: LintBuffer, } pub trait LintPassObject: Sized {} @@ -567,7 +561,7 @@ pub trait LintContext: Sized { } impl<'a> EarlyContext<'a> { - fn new( + pub fn new( sess: &'a Session, lint_store: &'a LintStore, krate: &'a ast::Crate, @@ -588,48 +582,6 @@ macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({ $cx.pass.$f(&$cx.context, $($args),*); }) } -macro_rules! run_early_pass { ($cx:expr, $f:ident, $($args:expr),*) => ({ - $cx.pass.$f(&$cx.context, $($args),*); -}) } - -impl<'a, T: EarlyLintPass> EarlyContextAndPass<'a, T> { - fn check_id(&mut self, id: ast::NodeId) { - for early_lint in self.context.buffered.take(id) { - self.context.lookup_and_emit_with_diagnostics( - early_lint.lint_id.lint, - Some(early_lint.span.clone()), - &early_lint.msg, - early_lint.diagnostic, - ); - } - } - - /// Merge the lints specified by any lint attributes into the - /// current lint context, call the provided function, then reset the - /// lints in effect to their previous state. - fn with_lint_attrs(&mut self, id: ast::NodeId, attrs: &'a [ast::Attribute], f: F) - where - F: FnOnce(&mut Self), - { - let push = self.context.builder.push(attrs, &self.context.lint_store); - self.check_id(id); - self.enter_attrs(attrs); - f(self); - self.exit_attrs(attrs); - self.context.builder.pop(push); - } - - fn enter_attrs(&mut self, attrs: &'a [ast::Attribute]) { - debug!("early context: enter_attrs({:?})", attrs); - run_early_pass!(self, enter_lint_attrs, attrs); - } - - fn exit_attrs(&mut self, attrs: &'a [ast::Attribute]) { - debug!("early context: exit_attrs({:?})", attrs); - run_early_pass!(self, exit_lint_attrs, attrs); - } -} - impl LintContext for LateContext<'_, '_> { type PassObject = LateLintPassObject; @@ -1103,195 +1055,6 @@ impl<'a, 'tcx, T: LateLintPass<'a, 'tcx>> hir_visit::Visitor<'tcx> } } -impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> { - fn visit_param(&mut self, param: &'a ast::Param) { - self.with_lint_attrs(param.id, ¶m.attrs, |cx| { - run_early_pass!(cx, check_param, param); - ast_visit::walk_param(cx, param); - }); - } - - fn visit_item(&mut self, it: &'a ast::Item) { - self.with_lint_attrs(it.id, &it.attrs, |cx| { - run_early_pass!(cx, check_item, it); - ast_visit::walk_item(cx, it); - run_early_pass!(cx, check_item_post, it); - }) - } - - fn visit_foreign_item(&mut self, it: &'a ast::ForeignItem) { - self.with_lint_attrs(it.id, &it.attrs, |cx| { - run_early_pass!(cx, check_foreign_item, it); - ast_visit::walk_foreign_item(cx, it); - run_early_pass!(cx, check_foreign_item_post, it); - }) - } - - fn visit_pat(&mut self, p: &'a ast::Pat) { - run_early_pass!(self, check_pat, p); - self.check_id(p.id); - ast_visit::walk_pat(self, p); - run_early_pass!(self, check_pat_post, p); - } - - fn visit_expr(&mut self, e: &'a ast::Expr) { - self.with_lint_attrs(e.id, &e.attrs, |cx| { - run_early_pass!(cx, check_expr, e); - ast_visit::walk_expr(cx, e); - }) - } - - fn visit_stmt(&mut self, s: &'a ast::Stmt) { - run_early_pass!(self, check_stmt, s); - self.check_id(s.id); - ast_visit::walk_stmt(self, s); - } - - fn visit_fn( - &mut self, - fk: ast_visit::FnKind<'a>, - decl: &'a ast::FnDecl, - span: Span, - id: ast::NodeId, - ) { - run_early_pass!(self, check_fn, fk, decl, span, id); - self.check_id(id); - ast_visit::walk_fn(self, fk, decl, span); - run_early_pass!(self, check_fn_post, fk, decl, span, id); - } - - fn visit_variant_data(&mut self, s: &'a ast::VariantData) { - run_early_pass!(self, check_struct_def, s); - if let Some(ctor_hir_id) = s.ctor_id() { - self.check_id(ctor_hir_id); - } - ast_visit::walk_struct_def(self, s); - run_early_pass!(self, check_struct_def_post, s); - } - - fn visit_struct_field(&mut self, s: &'a ast::StructField) { - self.with_lint_attrs(s.id, &s.attrs, |cx| { - run_early_pass!(cx, check_struct_field, s); - ast_visit::walk_struct_field(cx, s); - }) - } - - fn visit_variant(&mut self, v: &'a ast::Variant) { - self.with_lint_attrs(v.id, &v.attrs, |cx| { - run_early_pass!(cx, check_variant, v); - ast_visit::walk_variant(cx, v); - run_early_pass!(cx, check_variant_post, v); - }) - } - - fn visit_ty(&mut self, t: &'a ast::Ty) { - run_early_pass!(self, check_ty, t); - self.check_id(t.id); - ast_visit::walk_ty(self, t); - } - - fn visit_ident(&mut self, ident: ast::Ident) { - run_early_pass!(self, check_ident, ident); - } - - fn visit_mod(&mut self, m: &'a ast::Mod, s: Span, _a: &[ast::Attribute], n: ast::NodeId) { - run_early_pass!(self, check_mod, m, s, n); - self.check_id(n); - ast_visit::walk_mod(self, m); - run_early_pass!(self, check_mod_post, m, s, n); - } - - fn visit_local(&mut self, l: &'a ast::Local) { - self.with_lint_attrs(l.id, &l.attrs, |cx| { - run_early_pass!(cx, check_local, l); - ast_visit::walk_local(cx, l); - }) - } - - fn visit_block(&mut self, b: &'a ast::Block) { - run_early_pass!(self, check_block, b); - self.check_id(b.id); - ast_visit::walk_block(self, b); - run_early_pass!(self, check_block_post, b); - } - - fn visit_arm(&mut self, a: &'a ast::Arm) { - run_early_pass!(self, check_arm, a); - ast_visit::walk_arm(self, a); - } - - fn visit_expr_post(&mut self, e: &'a ast::Expr) { - run_early_pass!(self, check_expr_post, e); - } - - fn visit_generic_param(&mut self, param: &'a ast::GenericParam) { - run_early_pass!(self, check_generic_param, param); - ast_visit::walk_generic_param(self, param); - } - - fn visit_generics(&mut self, g: &'a ast::Generics) { - run_early_pass!(self, check_generics, g); - ast_visit::walk_generics(self, g); - } - - fn visit_where_predicate(&mut self, p: &'a ast::WherePredicate) { - run_early_pass!(self, check_where_predicate, p); - ast_visit::walk_where_predicate(self, p); - } - - fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef, m: &'a ast::TraitBoundModifier) { - run_early_pass!(self, check_poly_trait_ref, t, m); - ast_visit::walk_poly_trait_ref(self, t, m); - } - - fn visit_trait_item(&mut self, trait_item: &'a ast::AssocItem) { - self.with_lint_attrs(trait_item.id, &trait_item.attrs, |cx| { - run_early_pass!(cx, check_trait_item, trait_item); - ast_visit::walk_trait_item(cx, trait_item); - run_early_pass!(cx, check_trait_item_post, trait_item); - }); - } - - fn visit_impl_item(&mut self, impl_item: &'a ast::AssocItem) { - self.with_lint_attrs(impl_item.id, &impl_item.attrs, |cx| { - run_early_pass!(cx, check_impl_item, impl_item); - ast_visit::walk_impl_item(cx, impl_item); - run_early_pass!(cx, check_impl_item_post, impl_item); - }); - } - - fn visit_lifetime(&mut self, lt: &'a ast::Lifetime) { - run_early_pass!(self, check_lifetime, lt); - self.check_id(lt.id); - } - - fn visit_path(&mut self, p: &'a ast::Path, id: ast::NodeId) { - run_early_pass!(self, check_path, p, id); - self.check_id(id); - ast_visit::walk_path(self, p); - } - - fn visit_attribute(&mut self, attr: &'a ast::Attribute) { - run_early_pass!(self, check_attribute, attr); - } - - fn visit_mac_def(&mut self, mac: &'a ast::MacroDef, id: ast::NodeId) { - run_early_pass!(self, check_mac_def, mac, id); - self.check_id(id); - } - - fn visit_mac(&mut self, mac: &'a ast::Mac) { - // FIXME(#54110): So, this setup isn't really right. I think - // that (a) the libsyntax visitor ought to be doing this as - // part of `walk_mac`, and (b) we should be calling - // `visit_path`, *but* that would require a `NodeId`, and I - // want to get #53686 fixed quickly. -nmatsakis - ast_visit::walk_path(self, &mac.path); - - run_early_pass!(self, check_mac, mac); - } -} - struct LateLintPassObjects<'a> { lints: &'a mut [LateLintPassObject], } @@ -1451,122 +1214,3 @@ pub fn check_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>( }, ); } - -struct EarlyLintPassObjects<'a> { - lints: &'a mut [EarlyLintPassObject], -} - -#[allow(rustc::lint_pass_impl_without_macro)] -impl LintPass for EarlyLintPassObjects<'_> { - fn name(&self) -> &'static str { - panic!() - } -} - -macro_rules! expand_early_lint_pass_impl_methods { - ([$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => ( - $(fn $name(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) { - for obj in self.lints.iter_mut() { - obj.$name(context, $($param),*); - } - })* - ) -} - -macro_rules! early_lint_pass_impl { - ([], [$($methods:tt)*]) => ( - impl EarlyLintPass for EarlyLintPassObjects<'_> { - expand_early_lint_pass_impl_methods!([$($methods)*]); - } - ) -} - -early_lint_methods!(early_lint_pass_impl, []); - -fn early_lint_crate( - sess: &Session, - lint_store: &LintStore, - krate: &ast::Crate, - pass: T, - buffered: LintBuffer, - warn_about_weird_lints: bool, -) -> LintBuffer { - let mut cx = EarlyContextAndPass { - context: EarlyContext::new(sess, lint_store, krate, buffered, warn_about_weird_lints), - pass, - }; - - // Visit the whole crate. - cx.with_lint_attrs(ast::CRATE_NODE_ID, &krate.attrs, |cx| { - // since the root module isn't visited as an item (because it isn't an - // item), warn for it here. - run_early_pass!(cx, check_crate, krate); - - ast_visit::walk_crate(cx, krate); - - run_early_pass!(cx, check_crate_post, krate); - }); - cx.context.buffered -} - -pub fn check_ast_crate( - sess: &Session, - lint_store: &LintStore, - krate: &ast::Crate, - pre_expansion: bool, - lint_buffer: Option, - builtin_lints: T, -) { - let mut passes: Vec<_> = if pre_expansion { - lint_store.pre_expansion_passes.iter().map(|p| (p)()).collect() - } else { - lint_store.early_passes.iter().map(|p| (p)()).collect() - }; - let mut buffered = lint_buffer.unwrap_or_default(); - - if !sess.opts.debugging_opts.no_interleave_lints { - buffered = - early_lint_crate(sess, lint_store, krate, builtin_lints, buffered, pre_expansion); - - if !passes.is_empty() { - buffered = early_lint_crate( - sess, - lint_store, - krate, - EarlyLintPassObjects { lints: &mut passes[..] }, - buffered, - pre_expansion, - ); - } - } else { - for pass in &mut passes { - buffered = time(sess, &format!("running lint: {}", pass.name()), || { - early_lint_crate( - sess, - lint_store, - krate, - EarlyLintPassObjects { lints: slice::from_mut(pass) }, - buffered, - pre_expansion, - ) - }); - } - } - - // All of the buffered lints should have been emitted at this point. - // If not, that means that we somehow buffered a lint for a node id - // that was not lint-checked (perhaps it doesn't exist?). This is a bug. - // - // Rustdoc runs everybody-loops before the early lints and removes - // function bodies, so it's totally possible for linted - // node ids to not exist (e.g., macros defined within functions for the - // unused_macro lint) anymore. So we only run this check - // when we're not in rustdoc mode. (see issue #47639) - if !sess.opts.actually_rustdoc { - for (_id, lints) in buffered.map { - for early_lint in lints { - sess.delay_span_bug(early_lint.span, "failed to process buffered lint here"); - } - } - } -} diff --git a/src/librustc/lint/mod.rs b/src/librustc/lint/mod.rs index 7e433f50db208..f2d7d72360d44 100644 --- a/src/librustc/lint/mod.rs +++ b/src/librustc/lint/mod.rs @@ -39,8 +39,8 @@ use syntax::source_map::{DesugaringKind, ExpnKind, MultiSpan}; use syntax::symbol::Symbol; pub use crate::lint::context::{ - check_ast_crate, check_crate, late_lint_mod, BufferedEarlyLint, CheckLintNameResult, - EarlyContext, LateContext, LintContext, LintStore, + check_crate, late_lint_mod, BufferedEarlyLint, CheckLintNameResult, EarlyContext, LateContext, + LintContext, LintStore, }; pub use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintId}; @@ -380,7 +380,7 @@ pub use self::levels::{LintLevelMap, LintLevelSets}; #[derive(Default)] pub struct LintBuffer { - map: NodeMap>, + pub map: NodeMap>, } impl LintBuffer { @@ -405,7 +405,7 @@ impl LintBuffer { } } - fn take(&mut self, id: ast::NodeId) -> Vec { + pub fn take(&mut self, id: ast::NodeId) -> Vec { self.map.remove(&id).unwrap_or_default() } diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index 4d15424112e5c..35d6d3abdd894 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -231,7 +231,7 @@ fn configure_and_expand_inner<'a>( metadata_loader: &'a MetadataLoaderDyn, ) -> Result<(ast::Crate, Resolver<'a>)> { time(sess, "pre-AST-expansion lint checks", || { - lint::check_ast_crate( + rustc_lint::check_ast_crate( sess, lint_store, &krate, @@ -458,7 +458,7 @@ pub fn lower_to_hir<'res, 'tcx>( }); time(sess, "early lint checks", || { - lint::check_ast_crate( + rustc_lint::check_ast_crate( sess, lint_store, &krate, diff --git a/src/librustc_lint/early.rs b/src/librustc_lint/early.rs new file mode 100644 index 0000000000000..482a7822b7679 --- /dev/null +++ b/src/librustc_lint/early.rs @@ -0,0 +1,383 @@ +//! Implementation of lint checking. +//! +//! The lint checking is mostly consolidated into one pass which runs +//! after all other analyses. Throughout compilation, lint warnings +//! can be added via the `add_lint` method on the Session structure. This +//! requires a span and an ID of the node that the lint is being added to. The +//! lint isn't actually emitted at that time because it is unknown what the +//! actual lint level at that location is. +//! +//! To actually emit lint warnings/errors, a separate pass is used. +//! A context keeps track of the current state of all lint levels. +//! Upon entering a node of the ast which can modify the lint settings, the +//! previous lint state is pushed onto a stack and the ast is then recursed +//! upon. As the ast is traversed, this keeps track of the current lint level +//! for all lint attributes. + +use rustc::lint::{EarlyContext, LintStore}; +use rustc::lint::{EarlyLintPass, EarlyLintPassObject}; +use rustc::lint::{LintBuffer, LintContext, LintPass}; +use rustc::session::Session; +use rustc::util::common::time; + +use rustc_span::Span; +use std::slice; +use syntax::ast; +use syntax::visit as ast_visit; + +use log::debug; + +macro_rules! run_early_pass { ($cx:expr, $f:ident, $($args:expr),*) => ({ + $cx.pass.$f(&$cx.context, $($args),*); +}) } + +struct EarlyContextAndPass<'a, T: EarlyLintPass> { + context: EarlyContext<'a>, + pass: T, +} + +impl<'a, T: EarlyLintPass> EarlyContextAndPass<'a, T> { + fn check_id(&mut self, id: ast::NodeId) { + for early_lint in self.context.buffered.take(id) { + self.context.lookup_and_emit_with_diagnostics( + early_lint.lint_id.lint, + Some(early_lint.span.clone()), + &early_lint.msg, + early_lint.diagnostic, + ); + } + } + + /// Merge the lints specified by any lint attributes into the + /// current lint context, call the provided function, then reset the + /// lints in effect to their previous state. + fn with_lint_attrs(&mut self, id: ast::NodeId, attrs: &'a [ast::Attribute], f: F) + where + F: FnOnce(&mut Self), + { + let push = self.context.builder.push(attrs, &self.context.lint_store); + self.check_id(id); + self.enter_attrs(attrs); + f(self); + self.exit_attrs(attrs); + self.context.builder.pop(push); + } + + fn enter_attrs(&mut self, attrs: &'a [ast::Attribute]) { + debug!("early context: enter_attrs({:?})", attrs); + run_early_pass!(self, enter_lint_attrs, attrs); + } + + fn exit_attrs(&mut self, attrs: &'a [ast::Attribute]) { + debug!("early context: exit_attrs({:?})", attrs); + run_early_pass!(self, exit_lint_attrs, attrs); + } +} + +impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> { + fn visit_param(&mut self, param: &'a ast::Param) { + self.with_lint_attrs(param.id, ¶m.attrs, |cx| { + run_early_pass!(cx, check_param, param); + ast_visit::walk_param(cx, param); + }); + } + + fn visit_item(&mut self, it: &'a ast::Item) { + self.with_lint_attrs(it.id, &it.attrs, |cx| { + run_early_pass!(cx, check_item, it); + ast_visit::walk_item(cx, it); + run_early_pass!(cx, check_item_post, it); + }) + } + + fn visit_foreign_item(&mut self, it: &'a ast::ForeignItem) { + self.with_lint_attrs(it.id, &it.attrs, |cx| { + run_early_pass!(cx, check_foreign_item, it); + ast_visit::walk_foreign_item(cx, it); + run_early_pass!(cx, check_foreign_item_post, it); + }) + } + + fn visit_pat(&mut self, p: &'a ast::Pat) { + run_early_pass!(self, check_pat, p); + self.check_id(p.id); + ast_visit::walk_pat(self, p); + run_early_pass!(self, check_pat_post, p); + } + + fn visit_expr(&mut self, e: &'a ast::Expr) { + self.with_lint_attrs(e.id, &e.attrs, |cx| { + run_early_pass!(cx, check_expr, e); + ast_visit::walk_expr(cx, e); + }) + } + + fn visit_stmt(&mut self, s: &'a ast::Stmt) { + run_early_pass!(self, check_stmt, s); + self.check_id(s.id); + ast_visit::walk_stmt(self, s); + } + + fn visit_fn( + &mut self, + fk: ast_visit::FnKind<'a>, + decl: &'a ast::FnDecl, + span: Span, + id: ast::NodeId, + ) { + run_early_pass!(self, check_fn, fk, decl, span, id); + self.check_id(id); + ast_visit::walk_fn(self, fk, decl, span); + run_early_pass!(self, check_fn_post, fk, decl, span, id); + } + + fn visit_variant_data(&mut self, s: &'a ast::VariantData) { + run_early_pass!(self, check_struct_def, s); + if let Some(ctor_hir_id) = s.ctor_id() { + self.check_id(ctor_hir_id); + } + ast_visit::walk_struct_def(self, s); + run_early_pass!(self, check_struct_def_post, s); + } + + fn visit_struct_field(&mut self, s: &'a ast::StructField) { + self.with_lint_attrs(s.id, &s.attrs, |cx| { + run_early_pass!(cx, check_struct_field, s); + ast_visit::walk_struct_field(cx, s); + }) + } + + fn visit_variant(&mut self, v: &'a ast::Variant) { + self.with_lint_attrs(v.id, &v.attrs, |cx| { + run_early_pass!(cx, check_variant, v); + ast_visit::walk_variant(cx, v); + run_early_pass!(cx, check_variant_post, v); + }) + } + + fn visit_ty(&mut self, t: &'a ast::Ty) { + run_early_pass!(self, check_ty, t); + self.check_id(t.id); + ast_visit::walk_ty(self, t); + } + + fn visit_ident(&mut self, ident: ast::Ident) { + run_early_pass!(self, check_ident, ident); + } + + fn visit_mod(&mut self, m: &'a ast::Mod, s: Span, _a: &[ast::Attribute], n: ast::NodeId) { + run_early_pass!(self, check_mod, m, s, n); + self.check_id(n); + ast_visit::walk_mod(self, m); + run_early_pass!(self, check_mod_post, m, s, n); + } + + fn visit_local(&mut self, l: &'a ast::Local) { + self.with_lint_attrs(l.id, &l.attrs, |cx| { + run_early_pass!(cx, check_local, l); + ast_visit::walk_local(cx, l); + }) + } + + fn visit_block(&mut self, b: &'a ast::Block) { + run_early_pass!(self, check_block, b); + self.check_id(b.id); + ast_visit::walk_block(self, b); + run_early_pass!(self, check_block_post, b); + } + + fn visit_arm(&mut self, a: &'a ast::Arm) { + run_early_pass!(self, check_arm, a); + ast_visit::walk_arm(self, a); + } + + fn visit_expr_post(&mut self, e: &'a ast::Expr) { + run_early_pass!(self, check_expr_post, e); + } + + fn visit_generic_param(&mut self, param: &'a ast::GenericParam) { + run_early_pass!(self, check_generic_param, param); + ast_visit::walk_generic_param(self, param); + } + + fn visit_generics(&mut self, g: &'a ast::Generics) { + run_early_pass!(self, check_generics, g); + ast_visit::walk_generics(self, g); + } + + fn visit_where_predicate(&mut self, p: &'a ast::WherePredicate) { + run_early_pass!(self, check_where_predicate, p); + ast_visit::walk_where_predicate(self, p); + } + + fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef, m: &'a ast::TraitBoundModifier) { + run_early_pass!(self, check_poly_trait_ref, t, m); + ast_visit::walk_poly_trait_ref(self, t, m); + } + + fn visit_trait_item(&mut self, trait_item: &'a ast::AssocItem) { + self.with_lint_attrs(trait_item.id, &trait_item.attrs, |cx| { + run_early_pass!(cx, check_trait_item, trait_item); + ast_visit::walk_trait_item(cx, trait_item); + run_early_pass!(cx, check_trait_item_post, trait_item); + }); + } + + fn visit_impl_item(&mut self, impl_item: &'a ast::AssocItem) { + self.with_lint_attrs(impl_item.id, &impl_item.attrs, |cx| { + run_early_pass!(cx, check_impl_item, impl_item); + ast_visit::walk_impl_item(cx, impl_item); + run_early_pass!(cx, check_impl_item_post, impl_item); + }); + } + + fn visit_lifetime(&mut self, lt: &'a ast::Lifetime) { + run_early_pass!(self, check_lifetime, lt); + self.check_id(lt.id); + } + + fn visit_path(&mut self, p: &'a ast::Path, id: ast::NodeId) { + run_early_pass!(self, check_path, p, id); + self.check_id(id); + ast_visit::walk_path(self, p); + } + + fn visit_attribute(&mut self, attr: &'a ast::Attribute) { + run_early_pass!(self, check_attribute, attr); + } + + fn visit_mac_def(&mut self, mac: &'a ast::MacroDef, id: ast::NodeId) { + run_early_pass!(self, check_mac_def, mac, id); + self.check_id(id); + } + + fn visit_mac(&mut self, mac: &'a ast::Mac) { + // FIXME(#54110): So, this setup isn't really right. I think + // that (a) the libsyntax visitor ought to be doing this as + // part of `walk_mac`, and (b) we should be calling + // `visit_path`, *but* that would require a `NodeId`, and I + // want to get #53686 fixed quickly. -nmatsakis + ast_visit::walk_path(self, &mac.path); + + run_early_pass!(self, check_mac, mac); + } +} + +struct EarlyLintPassObjects<'a> { + lints: &'a mut [EarlyLintPassObject], +} + +#[allow(rustc::lint_pass_impl_without_macro)] +impl LintPass for EarlyLintPassObjects<'_> { + fn name(&self) -> &'static str { + panic!() + } +} + +macro_rules! expand_early_lint_pass_impl_methods { + ([$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => ( + $(fn $name(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) { + for obj in self.lints.iter_mut() { + obj.$name(context, $($param),*); + } + })* + ) +} + +macro_rules! early_lint_pass_impl { + ([], [$($methods:tt)*]) => ( + impl EarlyLintPass for EarlyLintPassObjects<'_> { + expand_early_lint_pass_impl_methods!([$($methods)*]); + } + ) +} + +early_lint_methods!(early_lint_pass_impl, []); + +fn early_lint_crate( + sess: &Session, + lint_store: &LintStore, + krate: &ast::Crate, + pass: T, + buffered: LintBuffer, + warn_about_weird_lints: bool, +) -> LintBuffer { + let mut cx = EarlyContextAndPass { + context: EarlyContext::new(sess, lint_store, krate, buffered, warn_about_weird_lints), + pass, + }; + + // Visit the whole crate. + cx.with_lint_attrs(ast::CRATE_NODE_ID, &krate.attrs, |cx| { + // since the root module isn't visited as an item (because it isn't an + // item), warn for it here. + run_early_pass!(cx, check_crate, krate); + + ast_visit::walk_crate(cx, krate); + + run_early_pass!(cx, check_crate_post, krate); + }); + cx.context.buffered +} + +pub fn check_ast_crate( + sess: &Session, + lint_store: &LintStore, + krate: &ast::Crate, + pre_expansion: bool, + lint_buffer: Option, + builtin_lints: T, +) { + let mut passes: Vec<_> = if pre_expansion { + lint_store.pre_expansion_passes.iter().map(|p| (p)()).collect() + } else { + lint_store.early_passes.iter().map(|p| (p)()).collect() + }; + let mut buffered = lint_buffer.unwrap_or_default(); + + if !sess.opts.debugging_opts.no_interleave_lints { + buffered = + early_lint_crate(sess, lint_store, krate, builtin_lints, buffered, pre_expansion); + + if !passes.is_empty() { + buffered = early_lint_crate( + sess, + lint_store, + krate, + EarlyLintPassObjects { lints: &mut passes[..] }, + buffered, + pre_expansion, + ); + } + } else { + for pass in &mut passes { + buffered = time(sess, &format!("running lint: {}", pass.name()), || { + early_lint_crate( + sess, + lint_store, + krate, + EarlyLintPassObjects { lints: slice::from_mut(pass) }, + buffered, + pre_expansion, + ) + }); + } + } + + // All of the buffered lints should have been emitted at this point. + // If not, that means that we somehow buffered a lint for a node id + // that was not lint-checked (perhaps it doesn't exist?). This is a bug. + // + // Rustdoc runs everybody-loops before the early lints and removes + // function bodies, so it's totally possible for linted + // node ids to not exist (e.g., macros defined within functions for the + // unused_macro lint) anymore. So we only run this check + // when we're not in rustdoc mode. (see issue #47639) + if !sess.opts.actually_rustdoc { + for (_id, lints) in buffered.map { + for early_lint in lints { + sess.delay_span_bug(early_lint.span, "failed to process buffered lint here"); + } + } + } +} diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index 099f154d4c887..57f260e758dee 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -24,6 +24,7 @@ extern crate rustc_session; mod array_into_iter; pub mod builtin; +mod early; mod non_ascii_idents; mod nonstandard_style; mod redundant_semicolon; @@ -57,6 +58,7 @@ use unused::*; /// Useful for other parts of the compiler. pub use builtin::SoftLints; +pub use early::check_ast_crate; pub fn provide(providers: &mut Providers<'_>) { *providers = Providers { lint_mod, ..*providers }; From 96180ff655dc10bc03bce3fb630f6d16cde71398 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 30 Dec 2019 14:22:46 +0100 Subject: [PATCH 2/3] Move late lint machanism in librustc_lint. --- src/librustc/lint/context.rs | 459 +----------------------------- src/librustc/lint/mod.rs | 3 +- src/librustc_interface/passes.rs | 2 +- src/librustc_lint/late.rs | 473 +++++++++++++++++++++++++++++++ src/librustc_lint/lib.rs | 4 +- 5 files changed, 486 insertions(+), 455 deletions(-) create mode 100644 src/librustc_lint/late.rs diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index ad6bdae1697e0..e1350ad03a10f 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -17,23 +17,20 @@ use self::TargetLint::*; use crate::hir; -use crate::hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; -use crate::hir::intravisit as hir_visit; -use crate::hir::intravisit::Visitor; +use crate::hir::def_id::{CrateNum, DefId}; use crate::hir::map::{definitions::DisambiguatedDefPathData, DefPathData}; use crate::lint::builtin::BuiltinLintDiagnostics; use crate::lint::levels::{LintLevelSets, LintLevelsBuilder}; -use crate::lint::{EarlyLintPassObject, LateLintPass, LateLintPassObject}; -use crate::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId, LintPass}; +use crate::lint::{EarlyLintPassObject, LateLintPassObject}; +use crate::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintId}; use crate::middle::privacy::AccessLevels; use crate::session::Session; use crate::ty::layout::{LayoutError, LayoutOf, TyLayout}; use crate::ty::{self, print::Printer, subst::GenericArg, Ty, TyCtxt}; -use crate::util::common::time; use crate::util::nodemap::FxHashMap; use errors::DiagnosticBuilder; -use rustc_data_structures::sync::{self, join, par_iter, ParallelIterator}; +use rustc_data_structures::sync; use rustc_span::{symbol::Symbol, MultiSpan, Span}; use std::slice; use syntax::ast; @@ -57,9 +54,9 @@ pub struct LintStore { /// necessarily in a sane manner. This is safe though.) pub pre_expansion_passes: Vec EarlyLintPassObject + sync::Send + sync::Sync>>, pub early_passes: Vec EarlyLintPassObject + sync::Send + sync::Sync>>, - late_passes: Vec LateLintPassObject + sync::Send + sync::Sync>>, + pub late_passes: Vec LateLintPassObject + sync::Send + sync::Sync>>, /// This is unique in that we construct them per-module, so not once. - late_module_passes: Vec LateLintPassObject + sync::Send + sync::Sync>>, + pub late_module_passes: Vec LateLintPassObject + sync::Send + sync::Sync>>, /// Lints indexed by name. by_name: FxHashMap, @@ -446,9 +443,9 @@ pub struct LateContext<'a, 'tcx> { pub access_levels: &'a AccessLevels, /// The store of registered lints and the lint levels. - lint_store: &'tcx LintStore, + pub lint_store: &'tcx LintStore, - last_node_with_lint_attrs: hir::HirId, + pub last_node_with_lint_attrs: hir::HirId, /// Generic type parameters in scope for the item we are in. pub generics: Option<&'tcx hir::Generics<'tcx>>, @@ -457,11 +454,6 @@ pub struct LateContext<'a, 'tcx> { pub only_module: bool, } -pub struct LateContextAndPass<'a, 'tcx, T: LateLintPass<'a, 'tcx>> { - context: LateContext<'a, 'tcx>, - pass: T, -} - /// Context for lint checking of the AST, after expansion, before lowering to /// HIR. pub struct EarlyContext<'a> { @@ -578,10 +570,6 @@ impl<'a> EarlyContext<'a> { } } -macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({ - $cx.pass.$f(&$cx.context, $($args),*); -}) } - impl LintContext for LateContext<'_, '_> { type PassObject = LateLintPassObject; @@ -783,434 +771,3 @@ impl<'a, 'tcx> LayoutOf for LateContext<'a, 'tcx> { self.tcx.layout_of(self.param_env.and(ty)) } } - -impl<'a, 'tcx, T: LateLintPass<'a, 'tcx>> LateContextAndPass<'a, 'tcx, T> { - /// Merge the lints specified by any lint attributes into the - /// current lint context, call the provided function, then reset the - /// lints in effect to their previous state. - fn with_lint_attrs(&mut self, id: hir::HirId, attrs: &'tcx [ast::Attribute], f: F) - where - F: FnOnce(&mut Self), - { - let prev = self.context.last_node_with_lint_attrs; - self.context.last_node_with_lint_attrs = id; - self.enter_attrs(attrs); - f(self); - self.exit_attrs(attrs); - self.context.last_node_with_lint_attrs = prev; - } - - fn with_param_env(&mut self, id: hir::HirId, f: F) - where - F: FnOnce(&mut Self), - { - let old_param_env = self.context.param_env; - self.context.param_env = - self.context.tcx.param_env(self.context.tcx.hir().local_def_id(id)); - f(self); - self.context.param_env = old_param_env; - } - - fn process_mod(&mut self, m: &'tcx hir::Mod<'tcx>, s: Span, n: hir::HirId) { - lint_callback!(self, check_mod, m, s, n); - hir_visit::walk_mod(self, m, n); - lint_callback!(self, check_mod_post, m, s, n); - } - - fn enter_attrs(&mut self, attrs: &'tcx [ast::Attribute]) { - debug!("late context: enter_attrs({:?})", attrs); - lint_callback!(self, enter_lint_attrs, attrs); - } - - fn exit_attrs(&mut self, attrs: &'tcx [ast::Attribute]) { - debug!("late context: exit_attrs({:?})", attrs); - lint_callback!(self, exit_lint_attrs, attrs); - } -} - -impl<'a, 'tcx, T: LateLintPass<'a, 'tcx>> hir_visit::Visitor<'tcx> - for LateContextAndPass<'a, 'tcx, T> -{ - /// 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<'this>(&'this mut self) -> hir_visit::NestedVisitorMap<'this, 'tcx> { - hir_visit::NestedVisitorMap::All(&self.context.tcx.hir()) - } - - fn visit_nested_body(&mut self, body: hir::BodyId) { - let old_tables = self.context.tables; - self.context.tables = self.context.tcx.body_tables(body); - let body = self.context.tcx.hir().body(body); - self.visit_body(body); - self.context.tables = old_tables; - } - - fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) { - self.with_lint_attrs(param.hir_id, ¶m.attrs, |cx| { - lint_callback!(cx, check_param, param); - hir_visit::walk_param(cx, param); - }); - } - - fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) { - lint_callback!(self, check_body, body); - hir_visit::walk_body(self, body); - lint_callback!(self, check_body_post, body); - } - - fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) { - let generics = self.context.generics.take(); - self.context.generics = it.kind.generics(); - self.with_lint_attrs(it.hir_id, &it.attrs, |cx| { - cx.with_param_env(it.hir_id, |cx| { - lint_callback!(cx, check_item, it); - hir_visit::walk_item(cx, it); - lint_callback!(cx, check_item_post, it); - }); - }); - self.context.generics = generics; - } - - fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) { - self.with_lint_attrs(it.hir_id, &it.attrs, |cx| { - cx.with_param_env(it.hir_id, |cx| { - lint_callback!(cx, check_foreign_item, it); - hir_visit::walk_foreign_item(cx, it); - lint_callback!(cx, check_foreign_item_post, it); - }); - }) - } - - fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) { - lint_callback!(self, check_pat, p); - hir_visit::walk_pat(self, p); - } - - fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) { - self.with_lint_attrs(e.hir_id, &e.attrs, |cx| { - lint_callback!(cx, check_expr, e); - hir_visit::walk_expr(cx, e); - lint_callback!(cx, check_expr_post, e); - }) - } - - fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) { - // statement attributes are actually just attributes on one of - // - item - // - local - // - expression - // so we keep track of lint levels there - lint_callback!(self, check_stmt, s); - hir_visit::walk_stmt(self, s); - } - - fn visit_fn( - &mut self, - fk: hir_visit::FnKind<'tcx>, - decl: &'tcx hir::FnDecl<'tcx>, - body_id: hir::BodyId, - span: Span, - id: hir::HirId, - ) { - // Wrap in tables here, not just in visit_nested_body, - // in order for `check_fn` to be able to use them. - let old_tables = self.context.tables; - self.context.tables = self.context.tcx.body_tables(body_id); - let body = self.context.tcx.hir().body(body_id); - lint_callback!(self, check_fn, fk, decl, body, span, id); - hir_visit::walk_fn(self, fk, decl, body_id, span, id); - lint_callback!(self, check_fn_post, fk, decl, body, span, id); - self.context.tables = old_tables; - } - - fn visit_variant_data( - &mut self, - s: &'tcx hir::VariantData<'tcx>, - _: ast::Name, - _: &'tcx hir::Generics<'tcx>, - _: hir::HirId, - _: Span, - ) { - lint_callback!(self, check_struct_def, s); - hir_visit::walk_struct_def(self, s); - lint_callback!(self, check_struct_def_post, s); - } - - fn visit_struct_field(&mut self, s: &'tcx hir::StructField<'tcx>) { - self.with_lint_attrs(s.hir_id, &s.attrs, |cx| { - lint_callback!(cx, check_struct_field, s); - hir_visit::walk_struct_field(cx, s); - }) - } - - fn visit_variant( - &mut self, - v: &'tcx hir::Variant<'tcx>, - g: &'tcx hir::Generics<'tcx>, - item_id: hir::HirId, - ) { - self.with_lint_attrs(v.id, &v.attrs, |cx| { - lint_callback!(cx, check_variant, v); - hir_visit::walk_variant(cx, v, g, item_id); - lint_callback!(cx, check_variant_post, v); - }) - } - - fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx>) { - lint_callback!(self, check_ty, t); - hir_visit::walk_ty(self, t); - } - - fn visit_name(&mut self, sp: Span, name: ast::Name) { - lint_callback!(self, check_name, sp, name); - } - - fn visit_mod(&mut self, m: &'tcx hir::Mod<'tcx>, s: Span, n: hir::HirId) { - if !self.context.only_module { - self.process_mod(m, s, n); - } - } - - fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) { - self.with_lint_attrs(l.hir_id, &l.attrs, |cx| { - lint_callback!(cx, check_local, l); - hir_visit::walk_local(cx, l); - }) - } - - fn visit_block(&mut self, b: &'tcx hir::Block<'tcx>) { - lint_callback!(self, check_block, b); - hir_visit::walk_block(self, b); - lint_callback!(self, check_block_post, b); - } - - fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) { - lint_callback!(self, check_arm, a); - hir_visit::walk_arm(self, a); - } - - fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) { - lint_callback!(self, check_generic_param, p); - hir_visit::walk_generic_param(self, p); - } - - fn visit_generics(&mut self, g: &'tcx hir::Generics<'tcx>) { - lint_callback!(self, check_generics, g); - hir_visit::walk_generics(self, g); - } - - fn visit_where_predicate(&mut self, p: &'tcx hir::WherePredicate<'tcx>) { - lint_callback!(self, check_where_predicate, p); - hir_visit::walk_where_predicate(self, p); - } - - fn visit_poly_trait_ref( - &mut self, - t: &'tcx hir::PolyTraitRef<'tcx>, - m: hir::TraitBoundModifier, - ) { - lint_callback!(self, check_poly_trait_ref, t, m); - hir_visit::walk_poly_trait_ref(self, t, m); - } - - fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) { - let generics = self.context.generics.take(); - self.context.generics = Some(&trait_item.generics); - self.with_lint_attrs(trait_item.hir_id, &trait_item.attrs, |cx| { - cx.with_param_env(trait_item.hir_id, |cx| { - lint_callback!(cx, check_trait_item, trait_item); - hir_visit::walk_trait_item(cx, trait_item); - lint_callback!(cx, check_trait_item_post, trait_item); - }); - }); - self.context.generics = generics; - } - - fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) { - let generics = self.context.generics.take(); - self.context.generics = Some(&impl_item.generics); - self.with_lint_attrs(impl_item.hir_id, &impl_item.attrs, |cx| { - cx.with_param_env(impl_item.hir_id, |cx| { - lint_callback!(cx, check_impl_item, impl_item); - hir_visit::walk_impl_item(cx, impl_item); - lint_callback!(cx, check_impl_item_post, impl_item); - }); - }); - self.context.generics = generics; - } - - fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) { - lint_callback!(self, check_lifetime, lt); - hir_visit::walk_lifetime(self, lt); - } - - fn visit_path(&mut self, p: &'tcx hir::Path<'tcx>, id: hir::HirId) { - lint_callback!(self, check_path, p, id); - hir_visit::walk_path(self, p); - } - - fn visit_attribute(&mut self, attr: &'tcx ast::Attribute) { - lint_callback!(self, check_attribute, attr); - } -} - -struct LateLintPassObjects<'a> { - lints: &'a mut [LateLintPassObject], -} - -#[allow(rustc::lint_pass_impl_without_macro)] -impl LintPass for LateLintPassObjects<'_> { - fn name(&self) -> &'static str { - panic!() - } -} - -macro_rules! expand_late_lint_pass_impl_methods { - ([$a:tt, $hir:tt], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => ( - $(fn $name(&mut self, context: &LateContext<$a, $hir>, $($param: $arg),*) { - for obj in self.lints.iter_mut() { - obj.$name(context, $($param),*); - } - })* - ) -} - -macro_rules! late_lint_pass_impl { - ([], [$hir:tt], $methods:tt) => ( - impl LateLintPass<'a, $hir> for LateLintPassObjects<'_> { - expand_late_lint_pass_impl_methods!(['a, $hir], $methods); - } - ) -} - -late_lint_methods!(late_lint_pass_impl, [], ['tcx]); - -fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>( - tcx: TyCtxt<'tcx>, - module_def_id: DefId, - pass: T, -) { - let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE); - - let context = LateContext { - tcx, - tables: &ty::TypeckTables::empty(None), - param_env: ty::ParamEnv::empty(), - access_levels, - lint_store: &tcx.lint_store, - last_node_with_lint_attrs: tcx.hir().as_local_hir_id(module_def_id).unwrap(), - generics: None, - only_module: true, - }; - - let mut cx = LateContextAndPass { context, pass }; - - let (module, span, hir_id) = tcx.hir().get_module(module_def_id); - cx.process_mod(module, span, hir_id); - - // Visit the crate attributes - if hir_id == hir::CRATE_HIR_ID { - walk_list!(cx, visit_attribute, tcx.hir().attrs(hir::CRATE_HIR_ID)); - } -} - -pub fn late_lint_mod<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>( - tcx: TyCtxt<'tcx>, - module_def_id: DefId, - builtin_lints: T, -) { - if tcx.sess.opts.debugging_opts.no_interleave_lints { - // These passes runs in late_lint_crate with -Z no_interleave_lints - return; - } - - late_lint_mod_pass(tcx, module_def_id, builtin_lints); - - let mut passes: Vec<_> = - tcx.lint_store.late_module_passes.iter().map(|pass| (pass)()).collect(); - - if !passes.is_empty() { - late_lint_mod_pass(tcx, module_def_id, LateLintPassObjects { lints: &mut passes[..] }); - } -} - -fn late_lint_pass_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tcx>, pass: T) { - let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE); - - let krate = tcx.hir().krate(); - - let context = LateContext { - tcx, - tables: &ty::TypeckTables::empty(None), - param_env: ty::ParamEnv::empty(), - access_levels, - lint_store: &tcx.lint_store, - last_node_with_lint_attrs: hir::CRATE_HIR_ID, - generics: None, - only_module: false, - }; - - let mut cx = LateContextAndPass { context, pass }; - - // Visit the whole crate. - cx.with_lint_attrs(hir::CRATE_HIR_ID, &krate.attrs, |cx| { - // since the root module isn't visited as an item (because it isn't an - // item), warn for it here. - lint_callback!(cx, check_crate, krate); - - hir_visit::walk_crate(cx, krate); - - lint_callback!(cx, check_crate_post, krate); - }) -} - -fn late_lint_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tcx>, builtin_lints: T) { - let mut passes = tcx.lint_store.late_passes.iter().map(|p| (p)()).collect::>(); - - if !tcx.sess.opts.debugging_opts.no_interleave_lints { - if !passes.is_empty() { - late_lint_pass_crate(tcx, LateLintPassObjects { lints: &mut passes[..] }); - } - - late_lint_pass_crate(tcx, builtin_lints); - } else { - for pass in &mut passes { - time(tcx.sess, &format!("running late lint: {}", pass.name()), || { - late_lint_pass_crate(tcx, LateLintPassObjects { lints: slice::from_mut(pass) }); - }); - } - - let mut passes: Vec<_> = - tcx.lint_store.late_module_passes.iter().map(|pass| (pass)()).collect(); - - for pass in &mut passes { - time(tcx.sess, &format!("running late module lint: {}", pass.name()), || { - late_lint_pass_crate(tcx, LateLintPassObjects { lints: slice::from_mut(pass) }); - }); - } - } -} - -/// Performs lint checking on a crate. -pub fn check_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>( - tcx: TyCtxt<'tcx>, - builtin_lints: impl FnOnce() -> T + Send, -) { - join( - || { - time(tcx.sess, "crate lints", || { - // Run whole crate non-incremental lints - late_lint_crate(tcx, builtin_lints()); - }); - }, - || { - time(tcx.sess, "module lints", || { - // Run per-module lints - par_iter(&tcx.hir().krate().modules).for_each(|(&module, _)| { - tcx.ensure().lint_mod(tcx.hir().local_def_id(module)); - }); - }); - }, - ); -} diff --git a/src/librustc/lint/mod.rs b/src/librustc/lint/mod.rs index f2d7d72360d44..ba9076f2e35b2 100644 --- a/src/librustc/lint/mod.rs +++ b/src/librustc/lint/mod.rs @@ -39,8 +39,7 @@ use syntax::source_map::{DesugaringKind, ExpnKind, MultiSpan}; use syntax::symbol::Symbol; pub use crate::lint::context::{ - check_crate, late_lint_mod, BufferedEarlyLint, CheckLintNameResult, EarlyContext, LateContext, - LintContext, LintStore, + BufferedEarlyLint, CheckLintNameResult, EarlyContext, LateContext, LintContext, LintStore, }; pub use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintId}; diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index 35d6d3abdd894..a796408491f9f 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -886,7 +886,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> { }, { time(sess, "lint checking", || { - lint::check_crate(tcx, || { + rustc_lint::check_crate(tcx, || { rustc_lint::BuiltinCombinedLateLintPass::new() }); }); diff --git a/src/librustc_lint/late.rs b/src/librustc_lint/late.rs new file mode 100644 index 0000000000000..1248cfd3bc269 --- /dev/null +++ b/src/librustc_lint/late.rs @@ -0,0 +1,473 @@ +//! Implementation of lint checking. +//! +//! The lint checking is mostly consolidated into one pass which runs +//! after all other analyses. Throughout compilation, lint warnings +//! can be added via the `add_lint` method on the Session structure. This +//! requires a span and an ID of the node that the lint is being added to. The +//! lint isn't actually emitted at that time because it is unknown what the +//! actual lint level at that location is. +//! +//! To actually emit lint warnings/errors, a separate pass is used. +//! A context keeps track of the current state of all lint levels. +//! Upon entering a node of the ast which can modify the lint settings, the +//! previous lint state is pushed onto a stack and the ast is then recursed +//! upon. As the ast is traversed, this keeps track of the current lint level +//! for all lint attributes. + +use rustc::hir; +use rustc::hir::def_id::{DefId, LOCAL_CRATE}; +use rustc::hir::intravisit as hir_visit; +use rustc::hir::intravisit::Visitor; +use rustc::lint::LateContext; +use rustc::lint::LintPass; +use rustc::lint::{LateLintPass, LateLintPassObject}; +use rustc::ty::{self, TyCtxt}; +use rustc::util::common::time; + +use rustc_data_structures::sync::{join, par_iter, ParallelIterator}; +use rustc_span::Span; +use std::slice; +use syntax::ast; + +use log::debug; +use syntax::walk_list; + +macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({ + $cx.pass.$f(&$cx.context, $($args),*); +}) } + +struct LateContextAndPass<'a, 'tcx, T: LateLintPass<'a, 'tcx>> { + context: LateContext<'a, 'tcx>, + pass: T, +} + +impl<'a, 'tcx, T: LateLintPass<'a, 'tcx>> LateContextAndPass<'a, 'tcx, T> { + /// Merge the lints specified by any lint attributes into the + /// current lint context, call the provided function, then reset the + /// lints in effect to their previous state. + fn with_lint_attrs(&mut self, id: hir::HirId, attrs: &'tcx [ast::Attribute], f: F) + where + F: FnOnce(&mut Self), + { + let prev = self.context.last_node_with_lint_attrs; + self.context.last_node_with_lint_attrs = id; + self.enter_attrs(attrs); + f(self); + self.exit_attrs(attrs); + self.context.last_node_with_lint_attrs = prev; + } + + fn with_param_env(&mut self, id: hir::HirId, f: F) + where + F: FnOnce(&mut Self), + { + let old_param_env = self.context.param_env; + self.context.param_env = + self.context.tcx.param_env(self.context.tcx.hir().local_def_id(id)); + f(self); + self.context.param_env = old_param_env; + } + + fn process_mod(&mut self, m: &'tcx hir::Mod<'tcx>, s: Span, n: hir::HirId) { + lint_callback!(self, check_mod, m, s, n); + hir_visit::walk_mod(self, m, n); + lint_callback!(self, check_mod_post, m, s, n); + } + + fn enter_attrs(&mut self, attrs: &'tcx [ast::Attribute]) { + debug!("late context: enter_attrs({:?})", attrs); + lint_callback!(self, enter_lint_attrs, attrs); + } + + fn exit_attrs(&mut self, attrs: &'tcx [ast::Attribute]) { + debug!("late context: exit_attrs({:?})", attrs); + lint_callback!(self, exit_lint_attrs, attrs); + } +} + +impl<'a, 'tcx, T: LateLintPass<'a, 'tcx>> hir_visit::Visitor<'tcx> + for LateContextAndPass<'a, 'tcx, T> +{ + /// 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<'this>(&'this mut self) -> hir_visit::NestedVisitorMap<'this, 'tcx> { + hir_visit::NestedVisitorMap::All(&self.context.tcx.hir()) + } + + fn visit_nested_body(&mut self, body: hir::BodyId) { + let old_tables = self.context.tables; + self.context.tables = self.context.tcx.body_tables(body); + let body = self.context.tcx.hir().body(body); + self.visit_body(body); + self.context.tables = old_tables; + } + + fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) { + self.with_lint_attrs(param.hir_id, ¶m.attrs, |cx| { + lint_callback!(cx, check_param, param); + hir_visit::walk_param(cx, param); + }); + } + + fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) { + lint_callback!(self, check_body, body); + hir_visit::walk_body(self, body); + lint_callback!(self, check_body_post, body); + } + + fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) { + let generics = self.context.generics.take(); + self.context.generics = it.kind.generics(); + self.with_lint_attrs(it.hir_id, &it.attrs, |cx| { + cx.with_param_env(it.hir_id, |cx| { + lint_callback!(cx, check_item, it); + hir_visit::walk_item(cx, it); + lint_callback!(cx, check_item_post, it); + }); + }); + self.context.generics = generics; + } + + fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) { + self.with_lint_attrs(it.hir_id, &it.attrs, |cx| { + cx.with_param_env(it.hir_id, |cx| { + lint_callback!(cx, check_foreign_item, it); + hir_visit::walk_foreign_item(cx, it); + lint_callback!(cx, check_foreign_item_post, it); + }); + }) + } + + fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) { + lint_callback!(self, check_pat, p); + hir_visit::walk_pat(self, p); + } + + fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) { + self.with_lint_attrs(e.hir_id, &e.attrs, |cx| { + lint_callback!(cx, check_expr, e); + hir_visit::walk_expr(cx, e); + lint_callback!(cx, check_expr_post, e); + }) + } + + fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) { + // statement attributes are actually just attributes on one of + // - item + // - local + // - expression + // so we keep track of lint levels there + lint_callback!(self, check_stmt, s); + hir_visit::walk_stmt(self, s); + } + + fn visit_fn( + &mut self, + fk: hir_visit::FnKind<'tcx>, + decl: &'tcx hir::FnDecl<'tcx>, + body_id: hir::BodyId, + span: Span, + id: hir::HirId, + ) { + // Wrap in tables here, not just in visit_nested_body, + // in order for `check_fn` to be able to use them. + let old_tables = self.context.tables; + self.context.tables = self.context.tcx.body_tables(body_id); + let body = self.context.tcx.hir().body(body_id); + lint_callback!(self, check_fn, fk, decl, body, span, id); + hir_visit::walk_fn(self, fk, decl, body_id, span, id); + lint_callback!(self, check_fn_post, fk, decl, body, span, id); + self.context.tables = old_tables; + } + + fn visit_variant_data( + &mut self, + s: &'tcx hir::VariantData<'tcx>, + _: ast::Name, + _: &'tcx hir::Generics<'tcx>, + _: hir::HirId, + _: Span, + ) { + lint_callback!(self, check_struct_def, s); + hir_visit::walk_struct_def(self, s); + lint_callback!(self, check_struct_def_post, s); + } + + fn visit_struct_field(&mut self, s: &'tcx hir::StructField<'tcx>) { + self.with_lint_attrs(s.hir_id, &s.attrs, |cx| { + lint_callback!(cx, check_struct_field, s); + hir_visit::walk_struct_field(cx, s); + }) + } + + fn visit_variant( + &mut self, + v: &'tcx hir::Variant<'tcx>, + g: &'tcx hir::Generics<'tcx>, + item_id: hir::HirId, + ) { + self.with_lint_attrs(v.id, &v.attrs, |cx| { + lint_callback!(cx, check_variant, v); + hir_visit::walk_variant(cx, v, g, item_id); + lint_callback!(cx, check_variant_post, v); + }) + } + + fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx>) { + lint_callback!(self, check_ty, t); + hir_visit::walk_ty(self, t); + } + + fn visit_name(&mut self, sp: Span, name: ast::Name) { + lint_callback!(self, check_name, sp, name); + } + + fn visit_mod(&mut self, m: &'tcx hir::Mod<'tcx>, s: Span, n: hir::HirId) { + if !self.context.only_module { + self.process_mod(m, s, n); + } + } + + fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) { + self.with_lint_attrs(l.hir_id, &l.attrs, |cx| { + lint_callback!(cx, check_local, l); + hir_visit::walk_local(cx, l); + }) + } + + fn visit_block(&mut self, b: &'tcx hir::Block<'tcx>) { + lint_callback!(self, check_block, b); + hir_visit::walk_block(self, b); + lint_callback!(self, check_block_post, b); + } + + fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) { + lint_callback!(self, check_arm, a); + hir_visit::walk_arm(self, a); + } + + fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) { + lint_callback!(self, check_generic_param, p); + hir_visit::walk_generic_param(self, p); + } + + fn visit_generics(&mut self, g: &'tcx hir::Generics<'tcx>) { + lint_callback!(self, check_generics, g); + hir_visit::walk_generics(self, g); + } + + fn visit_where_predicate(&mut self, p: &'tcx hir::WherePredicate<'tcx>) { + lint_callback!(self, check_where_predicate, p); + hir_visit::walk_where_predicate(self, p); + } + + fn visit_poly_trait_ref( + &mut self, + t: &'tcx hir::PolyTraitRef<'tcx>, + m: hir::TraitBoundModifier, + ) { + lint_callback!(self, check_poly_trait_ref, t, m); + hir_visit::walk_poly_trait_ref(self, t, m); + } + + fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) { + let generics = self.context.generics.take(); + self.context.generics = Some(&trait_item.generics); + self.with_lint_attrs(trait_item.hir_id, &trait_item.attrs, |cx| { + cx.with_param_env(trait_item.hir_id, |cx| { + lint_callback!(cx, check_trait_item, trait_item); + hir_visit::walk_trait_item(cx, trait_item); + lint_callback!(cx, check_trait_item_post, trait_item); + }); + }); + self.context.generics = generics; + } + + fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) { + let generics = self.context.generics.take(); + self.context.generics = Some(&impl_item.generics); + self.with_lint_attrs(impl_item.hir_id, &impl_item.attrs, |cx| { + cx.with_param_env(impl_item.hir_id, |cx| { + lint_callback!(cx, check_impl_item, impl_item); + hir_visit::walk_impl_item(cx, impl_item); + lint_callback!(cx, check_impl_item_post, impl_item); + }); + }); + self.context.generics = generics; + } + + fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) { + lint_callback!(self, check_lifetime, lt); + hir_visit::walk_lifetime(self, lt); + } + + fn visit_path(&mut self, p: &'tcx hir::Path<'tcx>, id: hir::HirId) { + lint_callback!(self, check_path, p, id); + hir_visit::walk_path(self, p); + } + + fn visit_attribute(&mut self, attr: &'tcx ast::Attribute) { + lint_callback!(self, check_attribute, attr); + } +} + +struct LateLintPassObjects<'a> { + lints: &'a mut [LateLintPassObject], +} + +#[allow(rustc::lint_pass_impl_without_macro)] +impl LintPass for LateLintPassObjects<'_> { + fn name(&self) -> &'static str { + panic!() + } +} + +macro_rules! expand_late_lint_pass_impl_methods { + ([$a:tt, $hir:tt], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => ( + $(fn $name(&mut self, context: &LateContext<$a, $hir>, $($param: $arg),*) { + for obj in self.lints.iter_mut() { + obj.$name(context, $($param),*); + } + })* + ) +} + +macro_rules! late_lint_pass_impl { + ([], [$hir:tt], $methods:tt) => ( + impl<'a, $hir> LateLintPass<'a, $hir> for LateLintPassObjects<'_> { + expand_late_lint_pass_impl_methods!(['a, $hir], $methods); + } + ) +} + +late_lint_methods!(late_lint_pass_impl, [], ['tcx]); + +fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>( + tcx: TyCtxt<'tcx>, + module_def_id: DefId, + pass: T, +) { + let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE); + + let context = LateContext { + tcx, + tables: &ty::TypeckTables::empty(None), + param_env: ty::ParamEnv::empty(), + access_levels, + lint_store: &tcx.lint_store, + last_node_with_lint_attrs: tcx.hir().as_local_hir_id(module_def_id).unwrap(), + generics: None, + only_module: true, + }; + + let mut cx = LateContextAndPass { context, pass }; + + let (module, span, hir_id) = tcx.hir().get_module(module_def_id); + cx.process_mod(module, span, hir_id); + + // Visit the crate attributes + if hir_id == hir::CRATE_HIR_ID { + walk_list!(cx, visit_attribute, tcx.hir().attrs(hir::CRATE_HIR_ID)); + } +} + +pub fn late_lint_mod<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>( + tcx: TyCtxt<'tcx>, + module_def_id: DefId, + builtin_lints: T, +) { + if tcx.sess.opts.debugging_opts.no_interleave_lints { + // These passes runs in late_lint_crate with -Z no_interleave_lints + return; + } + + late_lint_mod_pass(tcx, module_def_id, builtin_lints); + + let mut passes: Vec<_> = + tcx.lint_store.late_module_passes.iter().map(|pass| (pass)()).collect(); + + if !passes.is_empty() { + late_lint_mod_pass(tcx, module_def_id, LateLintPassObjects { lints: &mut passes[..] }); + } +} + +fn late_lint_pass_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tcx>, pass: T) { + let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE); + + let krate = tcx.hir().krate(); + + let context = LateContext { + tcx, + tables: &ty::TypeckTables::empty(None), + param_env: ty::ParamEnv::empty(), + access_levels, + lint_store: &tcx.lint_store, + last_node_with_lint_attrs: hir::CRATE_HIR_ID, + generics: None, + only_module: false, + }; + + let mut cx = LateContextAndPass { context, pass }; + + // Visit the whole crate. + cx.with_lint_attrs(hir::CRATE_HIR_ID, &krate.attrs, |cx| { + // since the root module isn't visited as an item (because it isn't an + // item), warn for it here. + lint_callback!(cx, check_crate, krate); + + hir_visit::walk_crate(cx, krate); + + lint_callback!(cx, check_crate_post, krate); + }) +} + +fn late_lint_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tcx>, builtin_lints: T) { + let mut passes = tcx.lint_store.late_passes.iter().map(|p| (p)()).collect::>(); + + if !tcx.sess.opts.debugging_opts.no_interleave_lints { + if !passes.is_empty() { + late_lint_pass_crate(tcx, LateLintPassObjects { lints: &mut passes[..] }); + } + + late_lint_pass_crate(tcx, builtin_lints); + } else { + for pass in &mut passes { + time(tcx.sess, &format!("running late lint: {}", pass.name()), || { + late_lint_pass_crate(tcx, LateLintPassObjects { lints: slice::from_mut(pass) }); + }); + } + + let mut passes: Vec<_> = + tcx.lint_store.late_module_passes.iter().map(|pass| (pass)()).collect(); + + for pass in &mut passes { + time(tcx.sess, &format!("running late module lint: {}", pass.name()), || { + late_lint_pass_crate(tcx, LateLintPassObjects { lints: slice::from_mut(pass) }); + }); + } + } +} + +/// Performs lint checking on a crate. +pub fn check_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>( + tcx: TyCtxt<'tcx>, + builtin_lints: impl FnOnce() -> T + Send, +) { + join( + || { + time(tcx.sess, "crate lints", || { + // Run whole crate non-incremental lints + late_lint_crate(tcx, builtin_lints()); + }); + }, + || { + time(tcx.sess, "module lints", || { + // Run per-module lints + par_iter(&tcx.hir().krate().modules).for_each(|(&module, _)| { + tcx.ensure().lint_mod(tcx.hir().local_def_id(module)); + }); + }); + }, + ); +} diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index 57f260e758dee..42fc353b2b064 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -25,6 +25,7 @@ extern crate rustc_session; mod array_into_iter; pub mod builtin; mod early; +mod late; mod non_ascii_idents; mod nonstandard_style; mod redundant_semicolon; @@ -59,13 +60,14 @@ use unused::*; /// Useful for other parts of the compiler. pub use builtin::SoftLints; pub use early::check_ast_crate; +pub use late::check_crate; pub fn provide(providers: &mut Providers<'_>) { *providers = Providers { lint_mod, ..*providers }; } fn lint_mod(tcx: TyCtxt<'_>, module_def_id: DefId) { - lint::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new()); + late::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new()); } macro_rules! pre_expansion_lint_passes { From 1fab03e90810d2290e8632e99f36f553fc717817 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 30 Dec 2019 19:02:52 +0100 Subject: [PATCH 3/3] Move lint levels machanism in librustc_lint. --- src/librustc/lint/levels.rs | 2 +- src/librustc/lint/mod.rs | 121 +----------------------------- src/librustc_interface/passes.rs | 1 - src/librustc_lint/levels.rs | 125 +++++++++++++++++++++++++++++++ src/librustc_lint/lib.rs | 2 + 5 files changed, 129 insertions(+), 122 deletions(-) create mode 100644 src/librustc_lint/levels.rs diff --git a/src/librustc/lint/levels.rs b/src/librustc/lint/levels.rs index edf7df16c87fa..97211f7082457 100644 --- a/src/librustc/lint/levels.rs +++ b/src/librustc/lint/levels.rs @@ -155,7 +155,7 @@ pub struct LintLevelsBuilder<'a> { pub struct BuilderPush { prev: u32, - pub(super) changed: bool, + pub changed: bool, } impl<'a> LintLevelsBuilder<'a> { diff --git a/src/librustc/lint/mod.rs b/src/librustc/lint/mod.rs index ba9076f2e35b2..31f3f0d1737e2 100644 --- a/src/librustc/lint/mod.rs +++ b/src/librustc/lint/mod.rs @@ -24,11 +24,8 @@ pub use self::LintSource::*; use rustc_data_structures::sync; use crate::hir; -use crate::hir::def_id::{CrateNum, LOCAL_CRATE}; -use crate::hir::intravisit; use crate::lint::builtin::BuiltinLintDiagnostics; use crate::session::{DiagnosticMessageId, Session}; -use crate::ty::query::Providers; use crate::ty::TyCtxt; use crate::util::nodemap::NodeMap; use errors::{DiagnosticBuilder, DiagnosticId}; @@ -375,7 +372,7 @@ mod context; pub mod internal; mod levels; -pub use self::levels::{LintLevelMap, LintLevelSets}; +pub use self::levels::{LintLevelMap, LintLevelSets, LintLevelsBuilder}; #[derive(Default)] pub struct LintBuffer { @@ -563,122 +560,6 @@ pub fn maybe_lint_level_root(tcx: TyCtxt<'_>, id: hir::HirId) -> bool { attrs.iter().any(|attr| Level::from_symbol(attr.name_or_empty()).is_some()) } -fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> &LintLevelMap { - assert_eq!(cnum, LOCAL_CRATE); - let store = &tcx.lint_store; - let mut builder = LintLevelMapBuilder { - levels: LintLevelSets::builder(tcx.sess, false, &store), - tcx: tcx, - store: store, - }; - let krate = tcx.hir().krate(); - - let push = builder.levels.push(&krate.attrs, &store); - builder.levels.register_id(hir::CRATE_HIR_ID); - for macro_def in krate.exported_macros { - builder.levels.register_id(macro_def.hir_id); - } - intravisit::walk_crate(&mut builder, krate); - builder.levels.pop(push); - - tcx.arena.alloc(builder.levels.build_map()) -} - -struct LintLevelMapBuilder<'a, 'tcx> { - levels: levels::LintLevelsBuilder<'tcx>, - tcx: TyCtxt<'tcx>, - store: &'a LintStore, -} - -impl LintLevelMapBuilder<'_, '_> { - fn with_lint_attrs(&mut self, id: hir::HirId, attrs: &[ast::Attribute], f: F) - where - F: FnOnce(&mut Self), - { - let push = self.levels.push(attrs, self.store); - if push.changed { - self.levels.register_id(id); - } - f(self); - self.levels.pop(push); - } -} - -impl intravisit::Visitor<'tcx> for LintLevelMapBuilder<'_, 'tcx> { - fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> { - intravisit::NestedVisitorMap::All(&self.tcx.hir()) - } - - fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) { - self.with_lint_attrs(param.hir_id, ¶m.attrs, |builder| { - intravisit::walk_param(builder, param); - }); - } - - fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) { - self.with_lint_attrs(it.hir_id, &it.attrs, |builder| { - intravisit::walk_item(builder, it); - }); - } - - fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) { - self.with_lint_attrs(it.hir_id, &it.attrs, |builder| { - intravisit::walk_foreign_item(builder, it); - }) - } - - fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) { - self.with_lint_attrs(e.hir_id, &e.attrs, |builder| { - intravisit::walk_expr(builder, e); - }) - } - - fn visit_struct_field(&mut self, s: &'tcx hir::StructField<'tcx>) { - self.with_lint_attrs(s.hir_id, &s.attrs, |builder| { - intravisit::walk_struct_field(builder, s); - }) - } - - fn visit_variant( - &mut self, - v: &'tcx hir::Variant<'tcx>, - g: &'tcx hir::Generics<'tcx>, - item_id: hir::HirId, - ) { - self.with_lint_attrs(v.id, &v.attrs, |builder| { - intravisit::walk_variant(builder, v, g, item_id); - }) - } - - fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) { - self.with_lint_attrs(l.hir_id, &l.attrs, |builder| { - intravisit::walk_local(builder, l); - }) - } - - fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) { - self.with_lint_attrs(a.hir_id, &a.attrs, |builder| { - intravisit::walk_arm(builder, a); - }) - } - - fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) { - self.with_lint_attrs(trait_item.hir_id, &trait_item.attrs, |builder| { - intravisit::walk_trait_item(builder, trait_item); - }); - } - - fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) { - self.with_lint_attrs(impl_item.hir_id, &impl_item.attrs, |builder| { - intravisit::walk_impl_item(builder, impl_item); - }); - } -} - -pub fn provide(providers: &mut Providers<'_>) { - providers.lint_levels = lint_levels; -} - /// Returns whether `span` originates in a foreign crate's external macro. /// /// This is used to test whether a lint should not even begin to figure out whether it should diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index a796408491f9f..22dc0b2de8bf0 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -692,7 +692,6 @@ pub fn default_provide(providers: &mut ty::query::Providers<'_>) { rustc_resolve::provide(providers); rustc_traits::provide(providers); rustc_metadata::provide(providers); - lint::provide(providers); rustc_lint::provide(providers); rustc_codegen_utils::provide(providers); rustc_codegen_ssa::provide(providers); diff --git a/src/librustc_lint/levels.rs b/src/librustc_lint/levels.rs new file mode 100644 index 0000000000000..c148d39c80c51 --- /dev/null +++ b/src/librustc_lint/levels.rs @@ -0,0 +1,125 @@ +use rustc::hir; +use rustc::hir::def_id::{CrateNum, LOCAL_CRATE}; +use rustc::hir::intravisit; +use rustc::lint::{LintLevelMap, LintLevelSets, LintLevelsBuilder, LintStore}; +use rustc::ty::query::Providers; +use rustc::ty::TyCtxt; +use syntax::ast; + +pub use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintId}; + +fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> &LintLevelMap { + assert_eq!(cnum, LOCAL_CRATE); + let store = &tcx.lint_store; + let mut builder = LintLevelMapBuilder { + levels: LintLevelSets::builder(tcx.sess, false, &store), + tcx: tcx, + store: store, + }; + let krate = tcx.hir().krate(); + + let push = builder.levels.push(&krate.attrs, &store); + builder.levels.register_id(hir::CRATE_HIR_ID); + for macro_def in krate.exported_macros { + builder.levels.register_id(macro_def.hir_id); + } + intravisit::walk_crate(&mut builder, krate); + builder.levels.pop(push); + + tcx.arena.alloc(builder.levels.build_map()) +} + +struct LintLevelMapBuilder<'a, 'tcx> { + levels: LintLevelsBuilder<'tcx>, + tcx: TyCtxt<'tcx>, + store: &'a LintStore, +} + +impl LintLevelMapBuilder<'_, '_> { + fn with_lint_attrs(&mut self, id: hir::HirId, attrs: &[ast::Attribute], f: F) + where + F: FnOnce(&mut Self), + { + let push = self.levels.push(attrs, self.store); + if push.changed { + self.levels.register_id(id); + } + f(self); + self.levels.pop(push); + } +} + +impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'_, 'tcx> { + fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> { + intravisit::NestedVisitorMap::All(&self.tcx.hir()) + } + + fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) { + self.with_lint_attrs(param.hir_id, ¶m.attrs, |builder| { + intravisit::walk_param(builder, param); + }); + } + + fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) { + self.with_lint_attrs(it.hir_id, &it.attrs, |builder| { + intravisit::walk_item(builder, it); + }); + } + + fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) { + self.with_lint_attrs(it.hir_id, &it.attrs, |builder| { + intravisit::walk_foreign_item(builder, it); + }) + } + + fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) { + self.with_lint_attrs(e.hir_id, &e.attrs, |builder| { + intravisit::walk_expr(builder, e); + }) + } + + fn visit_struct_field(&mut self, s: &'tcx hir::StructField<'tcx>) { + self.with_lint_attrs(s.hir_id, &s.attrs, |builder| { + intravisit::walk_struct_field(builder, s); + }) + } + + fn visit_variant( + &mut self, + v: &'tcx hir::Variant<'tcx>, + g: &'tcx hir::Generics<'tcx>, + item_id: hir::HirId, + ) { + self.with_lint_attrs(v.id, &v.attrs, |builder| { + intravisit::walk_variant(builder, v, g, item_id); + }) + } + + fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) { + self.with_lint_attrs(l.hir_id, &l.attrs, |builder| { + intravisit::walk_local(builder, l); + }) + } + + fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) { + self.with_lint_attrs(a.hir_id, &a.attrs, |builder| { + intravisit::walk_arm(builder, a); + }) + } + + fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) { + self.with_lint_attrs(trait_item.hir_id, &trait_item.attrs, |builder| { + intravisit::walk_trait_item(builder, trait_item); + }); + } + + fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) { + self.with_lint_attrs(impl_item.hir_id, &impl_item.attrs, |builder| { + intravisit::walk_impl_item(builder, impl_item); + }); + } +} + +pub fn provide(providers: &mut Providers<'_>) { + providers.lint_levels = lint_levels; +} diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index 42fc353b2b064..2a3b90f8c93b4 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -26,6 +26,7 @@ mod array_into_iter; pub mod builtin; mod early; mod late; +mod levels; mod non_ascii_idents; mod nonstandard_style; mod redundant_semicolon; @@ -63,6 +64,7 @@ pub use early::check_ast_crate; pub use late::check_crate; pub fn provide(providers: &mut Providers<'_>) { + levels::provide(providers); *providers = Providers { lint_mod, ..*providers }; }