Auto merge of #142455 - jdonszelmann:attempt-to-mitigate-delayed-lint-perf-problems, r=oli-obk

collect delayed lints in hir_crate_items

r? `@oli-obk`

Attempt to mitigate perf problems in rust-lang/rust#138164
This commit is contained in:
bors
2025-06-15 16:52:31 +00:00
6 changed files with 81 additions and 9 deletions

View File

@@ -193,13 +193,41 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
let _: R = tcx.ensure_ok().crate_inherent_impls_overlap_check(());
});
for owner_id in tcx.hir_crate_items(()).owners() {
if let Some(delayed_lints) = tcx.opt_ast_lowering_delayed_lints(owner_id) {
for lint in &delayed_lints.lints {
emit_delayed_lint(lint, tcx);
tcx.sess.time("emit_ast_lowering_delayed_lints", || {
// sanity check in debug mode that all lints are really noticed
// and we really will emit them all in the loop right below.
//
// during ast lowering, when creating items, foreign items, trait items and impl items
// we store in them whether they have any lints in their owner node that should be
// picked up by `hir_crate_items`. However, theoretically code can run between that
// boolean being inserted into the item and the owner node being created.
// We don't want any new lints to be emitted there
// (though honestly, you have to really try to manage to do that but still),
// but this check is there to catch that.
#[cfg(debug_assertions)]
{
// iterate over all owners
for owner_id in tcx.hir_crate_items(()).owners() {
// if it has delayed lints
if let Some(delayed_lints) = tcx.opt_ast_lowering_delayed_lints(owner_id) {
if !delayed_lints.lints.is_empty() {
// assert that delayed_lint_items also picked up this item to have lints
assert!(
tcx.hir_crate_items(()).delayed_lint_items().any(|i| i == owner_id)
);
}
}
}
}
}
for owner_id in tcx.hir_crate_items(()).delayed_lint_items() {
if let Some(delayed_lints) = tcx.opt_ast_lowering_delayed_lints(owner_id) {
for lint in &delayed_lints.lints {
emit_delayed_lint(lint, tcx);
}
}
}
});
tcx.par_hir_body_owners(|item_def_id| {
let def_kind = tcx.def_kind(item_def_id);