@@ -27,6 +27,8 @@ use rustc_serialize::{Decoder, Decodable, Encoder, Encodable};
2727use session:: { config, early_error, Session } ;
2828use ty:: { self , TyCtxt , Ty } ;
2929use ty:: layout:: { LayoutError , LayoutOf , TyLayout } ;
30+ use ty:: query:: { Providers , queries} ;
31+
3032use util:: nodemap:: FxHashMap ;
3133
3234use std:: default:: Default as StdDefault ;
@@ -35,6 +37,7 @@ use syntax::edition;
3537use syntax_pos:: { MultiSpan , Span , symbol:: { LocalInternedString , Symbol } } ;
3638use errors:: DiagnosticBuilder ;
3739use hir;
40+ use hir:: def_id:: DefId ;
3841use hir:: def_id:: LOCAL_CRATE ;
3942use hir:: intravisit as hir_visit;
4043use syntax:: util:: lev_distance:: find_best_match_for_name;
@@ -54,6 +57,7 @@ pub struct LintStore {
5457 pre_expansion_passes : Option < Vec < EarlyLintPassObject > > ,
5558 early_passes : Option < Vec < EarlyLintPassObject > > ,
5659 late_passes : Option < Vec < LateLintPassObject > > ,
60+ late_module_passes : Option < Vec < LateLintPassObject > > ,
5761
5862 /// Lints indexed by name.
5963 by_name : FxHashMap < String , TargetLint > ,
@@ -150,6 +154,7 @@ impl LintStore {
150154 pre_expansion_passes : Some ( vec ! [ ] ) ,
151155 early_passes : Some ( vec ! [ ] ) ,
152156 late_passes : Some ( vec ! [ ] ) ,
157+ late_module_passes : Some ( vec ! [ ] ) ,
153158 by_name : Default :: default ( ) ,
154159 future_incompatible : Default :: default ( ) ,
155160 lint_groups : Default :: default ( ) ,
@@ -192,9 +197,14 @@ impl LintStore {
192197 pub fn register_late_pass ( & mut self ,
193198 sess : Option < & Session > ,
194199 from_plugin : bool ,
200+ per_module : bool ,
195201 pass : LateLintPassObject ) {
196202 self . push_pass ( sess, from_plugin, & pass) ;
197- self . late_passes . as_mut ( ) . unwrap ( ) . push ( pass) ;
203+ if per_module {
204+ self . late_module_passes . as_mut ( ) . unwrap ( ) . push ( pass) ;
205+ } else {
206+ self . late_passes . as_mut ( ) . unwrap ( ) . push ( pass) ;
207+ }
198208 }
199209
200210 // Helper method for register_early/late_pass
@@ -501,6 +511,7 @@ pub struct LateContext<'a, 'tcx: 'a> {
501511 pub tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
502512
503513 /// Side-tables for the body we are in.
514+ // FIXME: Make this lazy to avoid running the TypeckTables query?
504515 pub tables : & ' a ty:: TypeckTables < ' tcx > ,
505516
506517 /// Parameter environment for the item we are in.
@@ -516,6 +527,9 @@ pub struct LateContext<'a, 'tcx: 'a> {
516527
517528 /// Generic type parameters in scope for the item we are in.
518529 pub generics : Option < & ' tcx hir:: Generics > ,
530+
531+ /// We are only looking at one module
532+ only_module : bool ,
519533}
520534
521535/// Context for lint checking of the AST, after expansion, before lowering to
@@ -787,6 +801,12 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
787801 pub fn current_lint_root ( & self ) -> ast:: NodeId {
788802 self . last_ast_node_with_lint_attrs
789803 }
804+
805+ fn process_mod ( & mut self , m : & ' tcx hir:: Mod , s : Span , n : ast:: NodeId ) {
806+ run_lints ! ( self , check_mod, m, s, n) ;
807+ hir_visit:: walk_mod ( self , m, n) ;
808+ run_lints ! ( self , check_mod_post, m, s, n) ;
809+ }
790810}
791811
792812impl < ' a , ' tcx > LayoutOf for LateContext < ' a , ' tcx > {
@@ -918,9 +938,9 @@ impl<'a, 'tcx> hir_visit::Visitor<'tcx> for LateContext<'a, 'tcx> {
918938 }
919939
920940 fn visit_mod ( & mut self , m : & ' tcx hir:: Mod , s : Span , n : ast:: NodeId ) {
921- run_lints ! ( self , check_mod , m , s , n ) ;
922- hir_visit :: walk_mod ( self , m , n) ;
923- run_lints ! ( self , check_mod_post , m , s , n ) ;
941+ if ! self . only_module {
942+ self . process_mod ( m , s , n) ;
943+ }
924944 }
925945
926946 fn visit_local ( & mut self , l : & ' tcx hir:: Local ) {
@@ -1191,6 +1211,50 @@ impl<'a> ast_visit::Visitor<'a> for EarlyContext<'a> {
11911211}
11921212
11931213
1214+ pub fn lint_mod < ' tcx > ( tcx : TyCtxt < ' _ , ' tcx , ' tcx > , module_def_id : DefId ) {
1215+ // Look for the parents of the module and for attributes on them
1216+ // to populate last_ast_node_with_lint_attrs
1217+
1218+ // Restricts this to only items in this module
1219+ let access_levels = & tcx. privacy_access_levels ( LOCAL_CRATE ) ;
1220+
1221+ let store = & tcx. sess . lint_store ;
1222+ let passes = store. borrow_mut ( ) . late_module_passes . take ( ) ;
1223+
1224+ let mut cx = LateContext {
1225+ tcx,
1226+ tables : & ty:: TypeckTables :: empty ( None ) ,
1227+ param_env : ty:: ParamEnv :: empty ( ) ,
1228+ access_levels,
1229+ lint_sess : LintSession {
1230+ lints : store. borrow ( ) ,
1231+ passes,
1232+ } ,
1233+ // Invalid for modules.
1234+ // FIXME: Have the modules require the parent module's attribute
1235+ last_ast_node_with_lint_attrs : ast:: CRATE_NODE_ID ,
1236+
1237+ generics : None ,
1238+
1239+ only_module : true ,
1240+ } ;
1241+
1242+ let ( module, span, node_id) = tcx. hir ( ) . get_module ( module_def_id) ;
1243+ cx. process_mod ( module, span, node_id) ;
1244+
1245+ // Put the lint store levels and passes back in the session.
1246+ let passes = cx. lint_sess . passes ;
1247+ drop ( cx. lint_sess . lints ) ;
1248+ store. borrow_mut ( ) . late_module_passes = passes;
1249+ }
1250+
1251+ pub ( crate ) fn provide ( providers : & mut Providers < ' _ > ) {
1252+ * providers = Providers {
1253+ lint_mod,
1254+ ..* providers
1255+ } ;
1256+ }
1257+
11941258/// Perform lint checking on a crate.
11951259///
11961260/// Consumes the `lint_store` field of the `Session`.
@@ -1212,6 +1276,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
12121276 } ,
12131277 last_ast_node_with_lint_attrs : ast:: CRATE_NODE_ID ,
12141278 generics : None ,
1279+ only_module : false ,
12151280 } ;
12161281
12171282 // Visit the whole crate.
@@ -1229,6 +1294,11 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
12291294
12301295 // Put the lint store levels and passes back in the session.
12311296 tcx. sess . lint_store . borrow_mut ( ) . late_passes = passes;
1297+
1298+ // Run per-module lints
1299+ for & module in tcx. hir ( ) . krate ( ) . modules . keys ( ) {
1300+ queries:: lint_mod:: ensure ( tcx, tcx. hir ( ) . local_def_id ( module) ) ;
1301+ }
12321302}
12331303
12341304pub fn check_ast_crate (
0 commit comments