@@ -27,6 +27,7 @@ use crate::rustc_serialize::{Decoder, Decodable, Encoder, Encodable};
27
27
use crate :: session:: { config, early_error, Session } ;
28
28
use crate :: ty:: { self , TyCtxt , Ty } ;
29
29
use crate :: ty:: layout:: { LayoutError , LayoutOf , TyLayout } ;
30
+ use crate :: ty:: query:: Providers ;
30
31
use crate :: util:: nodemap:: FxHashMap ;
31
32
use crate :: util:: common:: time;
32
33
@@ -36,7 +37,7 @@ use syntax::edition;
36
37
use syntax_pos:: { MultiSpan , Span , symbol:: { LocalInternedString , Symbol } } ;
37
38
use errors:: DiagnosticBuilder ;
38
39
use crate :: hir;
39
- use crate :: hir:: def_id:: LOCAL_CRATE ;
40
+ use crate :: hir:: def_id:: { DefId , LOCAL_CRATE } ;
40
41
use crate :: hir:: intravisit as hir_visit;
41
42
use syntax:: util:: lev_distance:: find_best_match_for_name;
42
43
use syntax:: visit as ast_visit;
@@ -55,6 +56,7 @@ pub struct LintStore {
55
56
pre_expansion_passes : Option < Vec < EarlyLintPassObject > > ,
56
57
early_passes : Option < Vec < EarlyLintPassObject > > ,
57
58
late_passes : Option < Vec < LateLintPassObject > > ,
59
+ late_module_passes : Option < Vec < LateLintPassObject > > ,
58
60
59
61
/// Lints indexed by name.
60
62
by_name : FxHashMap < String , TargetLint > ,
@@ -150,6 +152,7 @@ impl LintStore {
150
152
pre_expansion_passes : Some ( vec ! [ ] ) ,
151
153
early_passes : Some ( vec ! [ ] ) ,
152
154
late_passes : Some ( vec ! [ ] ) ,
155
+ late_module_passes : Some ( vec ! [ ] ) ,
153
156
by_name : Default :: default ( ) ,
154
157
future_incompatible : Default :: default ( ) ,
155
158
lint_groups : Default :: default ( ) ,
@@ -199,9 +202,14 @@ impl LintStore {
199
202
pub fn register_late_pass ( & mut self ,
200
203
sess : Option < & Session > ,
201
204
from_plugin : bool ,
205
+ per_module : bool ,
202
206
pass : LateLintPassObject ) {
203
207
self . push_pass ( sess, from_plugin, & pass) ;
204
- self . late_passes . as_mut ( ) . unwrap ( ) . push ( pass) ;
208
+ if per_module {
209
+ self . late_module_passes . as_mut ( ) . unwrap ( ) . push ( pass) ;
210
+ } else {
211
+ self . late_passes . as_mut ( ) . unwrap ( ) . push ( pass) ;
212
+ }
205
213
}
206
214
207
215
// Helper method for register_early/late_pass
@@ -508,6 +516,7 @@ pub struct LateContext<'a, 'tcx: 'a> {
508
516
pub tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
509
517
510
518
/// Side-tables for the body we are in.
519
+ // FIXME: Make this lazy to avoid running the TypeckTables query?
511
520
pub tables : & ' a ty:: TypeckTables < ' tcx > ,
512
521
513
522
/// Parameter environment for the item we are in.
@@ -523,6 +532,9 @@ pub struct LateContext<'a, 'tcx: 'a> {
523
532
524
533
/// Generic type parameters in scope for the item we are in.
525
534
pub generics : Option < & ' tcx hir:: Generics > ,
535
+
536
+ /// We are only looking at one module
537
+ only_module : bool ,
526
538
}
527
539
528
540
/// Context for lint checking of the AST, after expansion, before lowering to
@@ -803,6 +815,12 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
803
815
pub fn current_lint_root ( & self ) -> hir:: HirId {
804
816
self . last_node_with_lint_attrs
805
817
}
818
+
819
+ fn process_mod ( & mut self , m : & ' tcx hir:: Mod , s : Span , n : hir:: HirId ) {
820
+ run_lints ! ( self , check_mod, m, s, n) ;
821
+ hir_visit:: walk_mod ( self , m, n) ;
822
+ run_lints ! ( self , check_mod_post, m, s, n) ;
823
+ }
806
824
}
807
825
808
826
impl < ' a , ' tcx > LayoutOf for LateContext < ' a , ' tcx > {
@@ -934,9 +952,9 @@ impl<'a, 'tcx> hir_visit::Visitor<'tcx> for LateContext<'a, 'tcx> {
934
952
}
935
953
936
954
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 ) ;
955
+ if ! self . only_module {
956
+ self . process_mod ( m , s , n) ;
957
+ }
940
958
}
941
959
942
960
fn visit_local ( & mut self , l : & ' tcx hir:: Local ) {
@@ -1203,11 +1221,43 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
1203
1221
}
1204
1222
}
1205
1223
1224
+ pub fn lint_mod < ' tcx > ( tcx : TyCtxt < ' _ , ' tcx , ' tcx > , module_def_id : DefId ) {
1225
+ let access_levels = & tcx. privacy_access_levels ( LOCAL_CRATE ) ;
1206
1226
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 > ) {
1227
+ let store = & tcx. sess . lint_store ;
1228
+ let passes = store. borrow_mut ( ) . late_module_passes . take ( ) ;
1229
+
1230
+ let mut cx = LateContext {
1231
+ tcx,
1232
+ tables : & ty:: TypeckTables :: empty ( None ) ,
1233
+ param_env : ty:: ParamEnv :: empty ( ) ,
1234
+ access_levels,
1235
+ lint_sess : LintSession {
1236
+ lints : store. borrow ( ) ,
1237
+ passes,
1238
+ } ,
1239
+ last_node_with_lint_attrs : tcx. hir ( ) . as_local_hir_id ( module_def_id) . unwrap ( ) ,
1240
+ generics : None ,
1241
+ only_module : true ,
1242
+ } ;
1243
+
1244
+ let ( module, span, hir_id) = tcx. hir ( ) . get_module ( module_def_id) ;
1245
+ cx. process_mod ( module, span, hir_id) ;
1246
+
1247
+ // Put the lint store levels and passes back in the session.
1248
+ let passes = cx. lint_sess . passes ;
1249
+ drop ( cx. lint_sess . lints ) ;
1250
+ store. borrow_mut ( ) . late_module_passes = passes;
1251
+ }
1252
+
1253
+ pub ( crate ) fn provide ( providers : & mut Providers < ' _ > ) {
1254
+ * providers = Providers {
1255
+ lint_mod,
1256
+ ..* providers
1257
+ } ;
1258
+ }
1259
+
1260
+ fn lint_crate < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ) {
1211
1261
let access_levels = & tcx. privacy_access_levels ( LOCAL_CRATE ) ;
1212
1262
1213
1263
let krate = tcx. hir ( ) . krate ( ) ;
@@ -1225,6 +1275,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
1225
1275
} ,
1226
1276
last_node_with_lint_attrs : hir:: CRATE_HIR_ID ,
1227
1277
generics : None ,
1278
+ only_module : false ,
1228
1279
} ;
1229
1280
1230
1281
// Visit the whole crate.
@@ -1244,6 +1295,17 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
1244
1295
tcx. sess . lint_store . borrow_mut ( ) . late_passes = passes;
1245
1296
}
1246
1297
1298
+ /// Performs lint checking on a crate.
1299
+ pub fn check_crate < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ) {
1300
+ // Run per-module lints
1301
+ for & module in tcx. hir ( ) . krate ( ) . modules . keys ( ) {
1302
+ tcx. ensure ( ) . lint_mod ( tcx. hir ( ) . local_def_id ( module) ) ;
1303
+ }
1304
+
1305
+ // Run whole crate non-incremental lints
1306
+ lint_crate ( tcx) ;
1307
+ }
1308
+
1247
1309
struct EarlyLintPassObjects < ' a > {
1248
1310
lints : & ' a mut [ EarlyLintPassObject ] ,
1249
1311
}
0 commit comments