-
Notifications
You must be signed in to change notification settings - Fork 13k
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
(Big performance change) Do not run lints that cannot emit #125116
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b4da058
Do not run lints that cannot emit
blyxyas edc6577
Change lints_to_emit to lints_that_actually_run
blyxyas 71b4d10
Follow review comments (optimize the filtering)
blyxyas 637d5cc
Remove module passes filtering
blyxyas 8a40884
Unify syntax (all to @eval_always)
blyxyas ddad55f
Apply review comments + use `shallow_lint_levels_on`
blyxyas 1dcfa27
Move COGNITIVE_COMPLEXITY to use macro again
blyxyas 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
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
use rustc_ast_pretty::pprust; | ||
use rustc_data_structures::fx::FxIndexMap; | ||
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet}; | ||
use rustc_errors::{Diag, LintDiagnostic, MultiSpan}; | ||
use rustc_feature::{Features, GateIssue}; | ||
use rustc_hir::HirId; | ||
use rustc_hir::intravisit::{self, Visitor}; | ||
use rustc_hir::{CRATE_HIR_ID, HirId}; | ||
use rustc_index::IndexVec; | ||
use rustc_middle::bug; | ||
use rustc_middle::hir::nested_filter; | ||
|
@@ -115,6 +115,38 @@ impl LintLevelSets { | |
} | ||
} | ||
|
||
fn lints_that_dont_need_to_run(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet<LintId> { | ||
let store = unerased_lint_store(&tcx.sess); | ||
|
||
let map = tcx.shallow_lint_levels_on(rustc_hir::CRATE_OWNER_ID); | ||
|
||
let dont_need_to_run: FxIndexSet<LintId> = store | ||
.get_lints() | ||
.into_iter() | ||
.filter_map(|lint| { | ||
if !lint.eval_always { | ||
let lint_level = map.lint_level_id_at_node(tcx, LintId::of(lint), CRATE_HIR_ID); | ||
if matches!(lint_level, (Level::Allow, ..)) | ||
|| (matches!(lint_level, (.., LintLevelSource::Default))) | ||
&& lint.default_level(tcx.sess.edition()) == Level::Allow | ||
{ | ||
Some(LintId::of(lint)) | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect(); | ||
|
||
let mut visitor = LintLevelMaximum { tcx, dont_need_to_run }; | ||
visitor.process_opts(); | ||
tcx.hir().walk_attributes(&mut visitor); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be implemented by calling |
||
|
||
visitor.dont_need_to_run | ||
} | ||
|
||
#[instrument(level = "trace", skip(tcx), ret)] | ||
fn shallow_lint_levels_on(tcx: TyCtxt<'_>, owner: hir::OwnerId) -> ShallowLintLevelMap { | ||
let store = unerased_lint_store(tcx.sess); | ||
|
@@ -301,6 +333,83 @@ impl<'tcx> Visitor<'tcx> for LintLevelsBuilder<'_, LintLevelQueryMap<'tcx>> { | |
} | ||
} | ||
|
||
/// Visitor with the only function of visiting every item-like in a crate and | ||
/// computing the highest level that every lint gets put to. | ||
/// | ||
/// E.g., if a crate has a global #![allow(lint)] attribute, but a single item | ||
/// uses #[warn(lint)], this visitor will set that lint level as `Warn` | ||
struct LintLevelMaximum<'tcx> { | ||
tcx: TyCtxt<'tcx>, | ||
/// The actual list of detected lints. | ||
dont_need_to_run: FxIndexSet<LintId>, | ||
} | ||
|
||
impl<'tcx> LintLevelMaximum<'tcx> { | ||
fn process_opts(&mut self) { | ||
let store = unerased_lint_store(self.tcx.sess); | ||
for (lint_group, level) in &self.tcx.sess.opts.lint_opts { | ||
if *level != Level::Allow { | ||
let Ok(lints) = store.find_lints(lint_group) else { | ||
return; | ||
}; | ||
for lint in lints { | ||
self.dont_need_to_run.swap_remove(&lint); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<'tcx> Visitor<'tcx> for LintLevelMaximum<'tcx> { | ||
type NestedFilter = nested_filter::All; | ||
|
||
fn nested_visit_map(&mut self) -> Self::Map { | ||
self.tcx.hir() | ||
} | ||
|
||
/// FIXME(blyxyas): In a future revision, we should also graph #![allow]s, | ||
/// but that is handled with more care | ||
fn visit_attribute(&mut self, attribute: &'tcx ast::Attribute) { | ||
if matches!( | ||
Level::from_attr(attribute), | ||
Some( | ||
Level::Warn | ||
| Level::Deny | ||
| Level::Forbid | ||
| Level::Expect(..) | ||
| Level::ForceWarn(..), | ||
) | ||
) { | ||
let store = unerased_lint_store(self.tcx.sess); | ||
let Some(meta) = attribute.meta() else { return }; | ||
// Lint attributes are always a metalist inside a | ||
// metalist (even with just one lint). | ||
blyxyas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let Some(meta_item_list) = meta.meta_item_list() else { return }; | ||
|
||
for meta_list in meta_item_list { | ||
// Convert Path to String | ||
let Some(meta_item) = meta_list.meta_item() else { return }; | ||
let ident: &str = &meta_item | ||
.path | ||
.segments | ||
.iter() | ||
.map(|segment| segment.ident.as_str()) | ||
.collect::<Vec<&str>>() | ||
.join("::"); | ||
let Ok(lints) = store.find_lints( | ||
// Lint attributes can only have literals | ||
ident, | ||
) else { | ||
return; | ||
}; | ||
for lint in lints { | ||
self.dont_need_to_run.swap_remove(&lint); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub struct LintLevelsBuilder<'s, P> { | ||
sess: &'s Session, | ||
features: &'s Features, | ||
|
@@ -931,7 +1040,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> { | |
} | ||
|
||
pub(crate) fn provide(providers: &mut Providers) { | ||
*providers = Providers { shallow_lint_levels_on, ..*providers }; | ||
*providers = Providers { shallow_lint_levels_on, lints_that_dont_need_to_run, ..*providers }; | ||
} | ||
|
||
pub(crate) fn parse_lint_and_tool_name(lint_name: &str) -> (Option<Symbol>, &str) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Future-compatibility lints that show up in cargo's reports are supposed to be emitted even if they are allowed in the crate. Isn't that something that this logic needs to account for somehow? Or do we account for this already in a different way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I opened #133108 to fix this.