@@ -27,6 +27,7 @@ use crate::rustc_serialize::{Decoder, Decodable, Encoder, Encodable};
2727use crate :: session:: { config, early_error, Session } ;
2828use crate :: ty:: { self , TyCtxt , Ty } ;
2929use crate :: ty:: layout:: { LayoutError , LayoutOf , TyLayout } ;
30+ use crate :: ty:: query:: Providers ;
3031use crate :: util:: nodemap:: FxHashMap ;
3132use crate :: util:: common:: time;
3233
@@ -36,8 +37,9 @@ use syntax::edition;
3637use syntax_pos:: { MultiSpan , Span , symbol:: { LocalInternedString , Symbol } } ;
3738use errors:: DiagnosticBuilder ;
3839use crate :: hir;
39- use crate :: hir:: def_id:: LOCAL_CRATE ;
40+ use crate :: hir:: def_id:: { DefId , LOCAL_CRATE } ;
4041use crate :: hir:: intravisit as hir_visit;
42+ use crate :: hir:: intravisit:: Visitor ;
4143use syntax:: util:: lev_distance:: find_best_match_for_name;
4244use syntax:: visit as ast_visit;
4345
@@ -55,6 +57,7 @@ pub struct LintStore {
5557 pre_expansion_passes : Option < Vec < EarlyLintPassObject > > ,
5658 early_passes : Option < Vec < EarlyLintPassObject > > ,
5759 late_passes : Option < Vec < LateLintPassObject > > ,
60+ late_module_passes : Option < Vec < LateLintPassObject > > ,
5861
5962 /// Lints indexed by name.
6063 by_name : FxHashMap < String , TargetLint > ,
@@ -150,6 +153,7 @@ impl LintStore {
150153 pre_expansion_passes : Some ( vec ! [ ] ) ,
151154 early_passes : Some ( vec ! [ ] ) ,
152155 late_passes : Some ( vec ! [ ] ) ,
156+ late_module_passes : Some ( vec ! [ ] ) ,
153157 by_name : Default :: default ( ) ,
154158 future_incompatible : Default :: default ( ) ,
155159 lint_groups : Default :: default ( ) ,
@@ -199,9 +203,14 @@ impl LintStore {
199203 pub fn register_late_pass ( & mut self ,
200204 sess : Option < & Session > ,
201205 from_plugin : bool ,
206+ per_module : bool ,
202207 pass : LateLintPassObject ) {
203208 self . push_pass ( sess, from_plugin, & pass) ;
204- self . late_passes . as_mut ( ) . unwrap ( ) . push ( pass) ;
209+ if per_module {
210+ self . late_module_passes . as_mut ( ) . unwrap ( ) . push ( pass) ;
211+ } else {
212+ self . late_passes . as_mut ( ) . unwrap ( ) . push ( pass) ;
213+ }
205214 }
206215
207216 // Helper method for register_early/late_pass
@@ -508,6 +517,7 @@ pub struct LateContext<'a, 'tcx: 'a> {
508517 pub tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
509518
510519 /// Side-tables for the body we are in.
520+ // FIXME: Make this lazy to avoid running the TypeckTables query?
511521 pub tables : & ' a ty:: TypeckTables < ' tcx > ,
512522
513523 /// Parameter environment for the item we are in.
@@ -523,6 +533,9 @@ pub struct LateContext<'a, 'tcx: 'a> {
523533
524534 /// Generic type parameters in scope for the item we are in.
525535 pub generics : Option < & ' tcx hir:: Generics > ,
536+
537+ /// We are only looking at one module
538+ only_module : bool ,
526539}
527540
528541/// Context for lint checking of the AST, after expansion, before lowering to
@@ -803,6 +816,12 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
803816 pub fn current_lint_root ( & self ) -> hir:: HirId {
804817 self . last_node_with_lint_attrs
805818 }
819+
820+ fn process_mod ( & mut self , m : & ' tcx hir:: Mod , s : Span , n : hir:: HirId ) {
821+ run_lints ! ( self , check_mod, m, s, n) ;
822+ hir_visit:: walk_mod ( self , m, n) ;
823+ run_lints ! ( self , check_mod_post, m, s, n) ;
824+ }
806825}
807826
808827impl < ' a , ' tcx > LayoutOf for LateContext < ' a , ' tcx > {
@@ -934,9 +953,9 @@ impl<'a, 'tcx> hir_visit::Visitor<'tcx> for LateContext<'a, 'tcx> {
934953 }
935954
936955 fn visit_mod ( & mut self , m : & ' tcx hir:: Mod , s : Span , n : hir:: HirId ) {
937- run_lints ! ( self , check_mod , m , s , n ) ;
938- hir_visit :: walk_mod ( self , m , n) ;
939- run_lints ! ( self , check_mod_post , m , s , n ) ;
956+ if ! self . only_module {
957+ self . process_mod ( m , s , n) ;
958+ }
940959 }
941960
942961 fn visit_local ( & mut self , l : & ' tcx hir:: Local ) {
@@ -1203,11 +1222,48 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
12031222 }
12041223}
12051224
1225+ pub fn lint_mod < ' tcx > ( tcx : TyCtxt < ' _ , ' tcx , ' tcx > , module_def_id : DefId ) {
1226+ let access_levels = & tcx. privacy_access_levels ( LOCAL_CRATE ) ;
12061227
1207- /// Performs lint checking on a crate.
1208- ///
1209- /// Consumes the `lint_store` field of the `Session`.
1210- pub fn check_crate < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ) {
1228+ let store = & tcx. sess . lint_store ;
1229+ let passes = store. borrow_mut ( ) . late_module_passes . take ( ) ;
1230+
1231+ let mut cx = LateContext {
1232+ tcx,
1233+ tables : & ty:: TypeckTables :: empty ( None ) ,
1234+ param_env : ty:: ParamEnv :: empty ( ) ,
1235+ access_levels,
1236+ lint_sess : LintSession {
1237+ lints : store. borrow ( ) ,
1238+ passes,
1239+ } ,
1240+ last_node_with_lint_attrs : tcx. hir ( ) . as_local_hir_id ( module_def_id) . unwrap ( ) ,
1241+ generics : None ,
1242+ only_module : true ,
1243+ } ;
1244+
1245+ let ( module, span, hir_id) = tcx. hir ( ) . get_module ( module_def_id) ;
1246+ cx. process_mod ( module, span, hir_id) ;
1247+
1248+ // Visit the crate attributes
1249+ if hir_id == hir:: CRATE_HIR_ID {
1250+ walk_list ! ( cx, visit_attribute, cx. tcx. hir( ) . attrs_by_hir_id( hir:: CRATE_HIR_ID ) ) ;
1251+ }
1252+
1253+ // Put the lint store levels and passes back in the session.
1254+ let passes = cx. lint_sess . passes ;
1255+ drop ( cx. lint_sess . lints ) ;
1256+ store. borrow_mut ( ) . late_module_passes = passes;
1257+ }
1258+
1259+ pub ( crate ) fn provide ( providers : & mut Providers < ' _ > ) {
1260+ * providers = Providers {
1261+ lint_mod,
1262+ ..* providers
1263+ } ;
1264+ }
1265+
1266+ fn lint_crate < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ) {
12111267 let access_levels = & tcx. privacy_access_levels ( LOCAL_CRATE ) ;
12121268
12131269 let krate = tcx. hir ( ) . krate ( ) ;
@@ -1225,6 +1281,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
12251281 } ,
12261282 last_node_with_lint_attrs : hir:: CRATE_HIR_ID ,
12271283 generics : None ,
1284+ only_module : false ,
12281285 } ;
12291286
12301287 // Visit the whole crate.
@@ -1244,6 +1301,17 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
12441301 tcx. sess . lint_store . borrow_mut ( ) . late_passes = passes;
12451302}
12461303
1304+ /// Performs lint checking on a crate.
1305+ pub fn check_crate < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ) {
1306+ // Run per-module lints
1307+ for & module in tcx. hir ( ) . krate ( ) . modules . keys ( ) {
1308+ tcx. ensure ( ) . lint_mod ( tcx. hir ( ) . local_def_id ( module) ) ;
1309+ }
1310+
1311+ // Run whole crate non-incremental lints
1312+ lint_crate ( tcx) ;
1313+ }
1314+
12471315struct EarlyLintPassObjects < ' a > {
12481316 lints : & ' a mut [ EarlyLintPassObject ] ,
12491317}
0 commit comments