Skip to content

Commit ca674ea

Browse files
committed
Auto merge of #57293 - Zoxc:incr-passes3, r=<try>
Make some lints incremental Blocked on #57253 r? @michaelwoerister
2 parents 54479c6 + 4093bec commit ca674ea

File tree

13 files changed

+168
-55
lines changed

13 files changed

+168
-55
lines changed

src/librustc/dep_graph/dep_node.rs

+1
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ rustc_dep_node_append!([define_dep_nodes!][ <'tcx>
474474
[] UnsafetyCheckResult(DefId),
475475
[] UnsafeDeriveOnReprPacked(DefId),
476476

477+
[] LintMod(DefId),
477478
[] CheckModAttrs(DefId),
478479
[] CheckModLoops(DefId),
479480
[] CheckModUnstableApiUsage(DefId),

src/librustc/hir/map/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -580,17 +580,17 @@ impl<'hir> Map<'hir> {
580580
&self.forest.krate.attrs
581581
}
582582

583-
pub fn get_module(&self, module: DefId) -> (&'hir Mod, Span, NodeId)
584-
{
583+
pub fn get_module(&self, module: DefId) -> (&'hir Mod, Span, HirId) {
585584
let node_id = self.as_local_node_id(module).unwrap();
585+
let hir_id = self.node_to_hir_id(node_id);
586586
self.read(node_id);
587587
match self.find_entry(node_id).unwrap().node {
588588
Node::Item(&Item {
589589
span,
590590
node: ItemKind::Mod(ref m),
591591
..
592-
}) => (m, span, node_id),
593-
Node::Crate => (&self.forest.krate.module, self.forest.krate.span, node_id),
592+
}) => (m, span, hir_id),
593+
Node::Crate => (&self.forest.krate.module, self.forest.krate.span, hir_id),
594594
_ => panic!("not a module")
595595
}
596596
}
@@ -1013,7 +1013,7 @@ impl<'hir> Map<'hir> {
10131013
/// corresponding to the Node ID
10141014
pub fn attrs(&self, id: NodeId) -> &'hir [ast::Attribute] {
10151015
self.read(id); // reveals attributes on the node
1016-
let attrs = match self.find(id) {
1016+
let attrs = match self.find_entry(id).map(|entry| entry.node) {
10171017
Some(Node::Local(l)) => Some(&l.attrs[..]),
10181018
Some(Node::Item(i)) => Some(&i.attrs[..]),
10191019
Some(Node::ForeignItem(fi)) => Some(&fi.attrs[..]),
@@ -1027,6 +1027,7 @@ impl<'hir> Map<'hir> {
10271027
// Unit/tuple structs/variants take the attributes straight from
10281028
// the struct/variant definition.
10291029
Some(Node::Ctor(..)) => return self.attrs(self.get_parent(id)),
1030+
Some(Node::Crate) => Some(&self.forest.krate.attrs[..]),
10301031
_ => None
10311032
};
10321033
attrs.unwrap_or(&[])

src/librustc/lint/context.rs

+77-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::rustc_serialize::{Decoder, Decodable, Encoder, Encodable};
2727
use crate::session::{config, early_error, Session};
2828
use crate::ty::{self, TyCtxt, Ty};
2929
use crate::ty::layout::{LayoutError, LayoutOf, TyLayout};
30+
use crate::ty::query::Providers;
3031
use crate::util::nodemap::FxHashMap;
3132
use crate::util::common::time;
3233

@@ -36,8 +37,9 @@ use syntax::edition;
3637
use syntax_pos::{MultiSpan, Span, symbol::{LocalInternedString, Symbol}};
3738
use errors::DiagnosticBuilder;
3839
use crate::hir;
39-
use crate::hir::def_id::LOCAL_CRATE;
40+
use crate::hir::def_id::{DefId, LOCAL_CRATE};
4041
use crate::hir::intravisit as hir_visit;
42+
use crate::hir::intravisit::Visitor;
4143
use syntax::util::lev_distance::find_best_match_for_name;
4244
use 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

808827
impl<'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+
12471315
struct EarlyLintPassObjects<'a> {
12481316
lints: &'a mut [EarlyLintPassObject],
12491317
}

src/librustc/lint/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'a, 'tcx> {
824824

825825
pub fn provide(providers: &mut Providers<'_>) {
826826
providers.lint_levels = lint_levels;
827+
context::provide(providers);
827828
}
828829

829830
/// Returns whether `span` originates in a foreign crate's external macro.

src/librustc/ty/query/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ rustc_query_append! { [define_queries!][ <'tcx>
245245
},
246246

247247
Other {
248+
[] fn lint_mod: LintMod(DefId) -> (),
249+
248250
/// Checks the attributes in the module
249251
[] fn check_mod_attrs: CheckModAttrs(DefId) -> (),
250252

src/librustc/ty/query/plumbing.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,7 @@ pub fn force_from_dep_node<'tcx>(
12721272
DepKind::MirBorrowCheck => { force!(mir_borrowck, def_id!()); }
12731273
DepKind::UnsafetyCheckResult => { force!(unsafety_check_result, def_id!()); }
12741274
DepKind::UnsafeDeriveOnReprPacked => { force!(unsafe_derive_on_repr_packed, def_id!()); }
1275+
DepKind::LintMod => { force!(lint_mod, def_id!()); }
12751276
DepKind::CheckModAttrs => { force!(check_mod_attrs, def_id!()); }
12761277
DepKind::CheckModLoops => { force!(check_mod_loops, def_id!()); }
12771278
DepKind::CheckModUnstableApiUsage => { force!(check_mod_unstable_api_usage, def_id!()); }

src/librustc_interface/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ pub fn register_plugins<'a>(
328328
ls.register_early_pass(Some(sess), true, false, pass);
329329
}
330330
for pass in late_lint_passes {
331-
ls.register_late_pass(Some(sess), true, pass);
331+
ls.register_late_pass(Some(sess), true, false, pass);
332332
}
333333

334334
for (name, (to, deprecated_name)) in lint_groups {

src/librustc_lint/builtin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,7 @@ fn check_const(cx: &LateContext<'_, '_>, body_id: hir::BodyId) {
13601360
promoted: None
13611361
};
13621362
// trigger the query once for all constants since that will already report the errors
1363+
// FIXME: Use ensure here
13631364
let _ = cx.tcx.const_eval(param_env.and(cid));
13641365
}
13651366

src/librustc_lint/lib.rs

+45-10
Original file line numberDiff line numberDiff line change
@@ -125,37 +125,72 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
125125
store.register_early_pass(sess, false, true, box BuiltinCombinedEarlyLintPass::new());
126126
}
127127

128-
late_lint_methods!(declare_combined_late_lint_pass, [BuiltinCombinedLateLintPass, [
128+
late_lint_methods!(declare_combined_late_lint_pass, [BuiltinCombinedModuleLateLintPass, [
129129
HardwiredLints: HardwiredLints,
130130
WhileTrue: WhileTrue,
131131
ImproperCTypes: ImproperCTypes,
132132
VariantSizeDifferences: VariantSizeDifferences,
133133
BoxPointers: BoxPointers,
134-
UnusedAttributes: UnusedAttributes,
135134
PathStatements: PathStatements,
135+
136+
// Depends on referenced function signatures in expressions
136137
UnusedResults: UnusedResults,
137-
NonSnakeCase: NonSnakeCase,
138+
138139
NonUpperCaseGlobals: NonUpperCaseGlobals,
139140
NonShorthandFieldPatterns: NonShorthandFieldPatterns,
140141
UnusedAllocation: UnusedAllocation,
142+
143+
// Depends on types used in type definitions
141144
MissingCopyImplementations: MissingCopyImplementations,
142-
UnstableFeatures: UnstableFeatures,
143-
InvalidNoMangleItems: InvalidNoMangleItems,
145+
144146
PluginAsLibrary: PluginAsLibrary,
147+
148+
// Depends on referenced function signatures in expressions
145149
MutableTransmutes: MutableTransmutes,
150+
151+
// Depends on types of fields, checks if they implement Drop
146152
UnionsWithDropFields: UnionsWithDropFields,
147-
UnreachablePub: UnreachablePub,
148-
UnnameableTestItems: UnnameableTestItems::new(),
153+
149154
TypeAliasBounds: TypeAliasBounds,
150-
UnusedBrokenConst: UnusedBrokenConst,
155+
151156
TrivialConstraints: TrivialConstraints,
152157
TypeLimits: TypeLimits::new(),
158+
159+
NonSnakeCase: NonSnakeCase,
160+
InvalidNoMangleItems: InvalidNoMangleItems,
161+
162+
// Depends on access levels
163+
UnreachablePub: UnreachablePub,
164+
165+
ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
166+
]], ['tcx]);
167+
168+
store.register_late_pass(sess, false, true, box BuiltinCombinedModuleLateLintPass::new());
169+
170+
late_lint_methods!(declare_combined_late_lint_pass, [BuiltinCombinedLateLintPass, [
171+
// FIXME: Look into regression when this is used as a module lint
172+
// May Depend on constants elsewhere
173+
UnusedBrokenConst: UnusedBrokenConst,
174+
175+
// Uses attr::is_used which is untracked, can't be an incremental module pass.
176+
UnusedAttributes: UnusedAttributes,
177+
178+
// Needs to run after UnusedAttributes as it marks all `feature` attributes as used.
179+
UnstableFeatures: UnstableFeatures,
180+
181+
// Tracks state across modules
182+
UnnameableTestItems: UnnameableTestItems::new(),
183+
184+
// Tracks attributes of parents
153185
MissingDoc: MissingDoc::new(),
186+
187+
// Depends on access levels
188+
// FIXME: Turn the computation of types which implement Debug into a query
189+
// and change this to a module lint pass
154190
MissingDebugImplementations: MissingDebugImplementations::new(),
155-
ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
156191
]], ['tcx]);
157192

158-
store.register_late_pass(sess, false, box BuiltinCombinedLateLintPass::new());
193+
store.register_late_pass(sess, false, false, box BuiltinCombinedLateLintPass::new());
159194

160195
add_lint_group!(sess,
161196
"nonstandard_style",

src/librustc_lint/nonstandard_style.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,15 @@ impl LintPass for NonSnakeCase {
267267
}
268268

269269
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSnakeCase {
270-
fn check_crate(&mut self, cx: &LateContext<'_, '_>, cr: &hir::Crate) {
270+
fn check_mod(&mut self, cx: &LateContext<'_, '_>, _: &'tcx hir::Mod, _: Span, id: hir::HirId) {
271+
if id != hir::CRATE_HIR_ID {
272+
return;
273+
}
274+
271275
let crate_ident = if let Some(name) = &cx.tcx.sess.opts.crate_name {
272276
Some(Ident::from_str(name))
273277
} else {
274-
attr::find_by_name(&cr.attrs, "crate_name")
278+
attr::find_by_name(&cx.tcx.hir().attrs_by_hir_id(hir::CRATE_HIR_ID), "crate_name")
275279
.and_then(|attr| attr.meta())
276280
.and_then(|meta| {
277281
meta.name_value_literal().and_then(|lit| {

src/librustc_privacy/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1790,8 +1790,7 @@ fn check_mod_privacy<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>, module_def_id: DefId) {
17901790
current_item: hir::DUMMY_HIR_ID,
17911791
empty_tables: &empty_tables,
17921792
};
1793-
let (module, span, node_id) = tcx.hir().get_module(module_def_id);
1794-
let hir_id = tcx.hir().node_to_hir_id(node_id);
1793+
let (module, span, hir_id) = tcx.hir().get_module(module_def_id);
17951794
intravisit::walk_mod(&mut visitor, module, hir_id);
17961795

17971796
// Check privacy of explicitly written types and traits as well as

0 commit comments

Comments
 (0)