Skip to content

Do not run per-module late lints if they can be all skipped #139597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion compiler/rustc_lint/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,16 @@ pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
let store = unerased_lint_store(tcx.sess);

if store.late_module_passes.is_empty() {
late_lint_mod_inner(tcx, module_def_id, context, builtin_lints);
// If all builtin lints can be skipped, there is no point in running `late_lint_mod_inner`
// at all. This happens often for dependencies built with `--cap-lints=allow`.
let dont_need_to_run = tcx.lints_that_dont_need_to_run(());
let can_skip_lints = builtin_lints
.get_lints()
.iter()
.all(|lint| dont_need_to_run.contains(&LintId::of(lint)));
if !can_skip_lints {
late_lint_mod_inner(tcx, module_def_id, context, builtin_lints);
}
} else {
let builtin_lints = Box::new(builtin_lints) as Box<dyn LateLintPass<'tcx>>;
let mut binding = store
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not do the same thing here that we do in late_lint_crate and filter out passes that only contain stuff that doesn't need to be run?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do that, but I think that would be slower. When we run everything, it is built in the compile-time.prepared structure, which merged all lints together. If we filtered individual lints, they need to be built using a runtime-prepared structure. For the Clippy lints that makes sense, but here I essentially only want to distinguish between "everything is disabled due to --cap-lints=allow" and "something might be disabled, but let's still run in the merged mode".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i meant in the else branch (but github doesnt let me comment on the right line ☹️) we make a RuntimeCombinedLateLintPass but don't do any filtering out of dyn LateLintPasss that are just only allowed lints. e.g.

        let mut binding = store
            .late_module_passes
            .iter()
            .map(|mk_pass| (mk_pass)(tcx))
            .chain(std::iter::once(builtin_lints))
            .filter(|pass| { ... }) // new, filter out passes that only contain allowed lints
            .collect::<Vec<_>>();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But builtin_lints is a single LintPass that combines all the builtin late lints together. In other words, in its e.g. check_mod implementation, it calls:

LINT_1.check_mod();
LINT_2.check_mod();

etc. So we cannot filter these lints from the outside. In theory, we could filter them inside these lint visitor functions, but that would mean doing that check for every call of each visitor function times the number of lints we have, which... doesn't sound fast 😅

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_lint/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ macro_rules! expand_combined_late_lint_pass_methods {
/// Combines multiple lints passes into a single lint pass, at compile time,
/// for maximum speed. Each `check_foo` method in `$methods` within this pass
/// simply calls `check_foo` once per `$pass`. Compare with
/// `LateLintPassObjects`, which is similar, but combines lint passes at
/// `RuntimeCombinedLateLintPass`, which is similar, but combines lint passes at
/// runtime.
#[macro_export]
macro_rules! declare_combined_late_lint_pass {
Expand Down Expand Up @@ -123,10 +123,10 @@ macro_rules! declare_combined_late_lint_pass {
#[allow(rustc::lint_pass_impl_without_macro)]
impl $crate::LintPass for $name {
fn name(&self) -> &'static str {
panic!()
stringify!($name)
}
fn get_lints(&self) -> LintVec {
panic!()
$name::get_lints()
}
}
)
Expand Down
Loading