Skip to content

Commit 2557a2e

Browse files
Auto merge of #142937 - compiler-errors:ignore-lints, r=<try>
[perf] Don't run some `MirLints` if lints are not enabled <!-- homu-ignore:start --> <!-- If this PR is related to an unstable feature or an otherwise tracked effort, please link to the relevant tracking issue here. If you don't know of a related tracking issue or there are none, feel free to ignore this. This PR will get automatically assigned to a reviewer. In case you would like a specific user to review your work, you can assign it to them by using r? <reviewer name> --> <!-- homu-ignore:end -->
2 parents 706f244 + e97279b commit 2557a2e

38 files changed

+107
-83
lines changed

compiler/rustc_mir_transform/src/add_retag.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ fn may_contain_reference<'tcx>(ty: Ty<'tcx>, depth: u32, tcx: TyCtxt<'tcx>) -> b
4949
}
5050

5151
impl<'tcx> crate::MirPass<'tcx> for AddRetag {
52-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
53-
sess.opts.unstable_opts.mir_emit_retag
52+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
53+
tcx.sess.opts.unstable_opts.mir_emit_retag
5454
}
5555

5656
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/check_alignment.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ use rustc_middle::mir::interpret::Scalar;
44
use rustc_middle::mir::visit::PlaceContext;
55
use rustc_middle::mir::*;
66
use rustc_middle::ty::{Ty, TyCtxt};
7-
use rustc_session::Session;
87

98
use crate::check_pointers::{BorrowedFieldProjectionMode, PointerCheck, check_pointers};
109

1110
pub(super) struct CheckAlignment;
1211

1312
impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
14-
fn is_enabled(&self, sess: &Session) -> bool {
15-
sess.ub_checks()
13+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
14+
tcx.sess.ub_checks()
1615
}
1716

1817
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/check_call_recursion.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_hir::LangItem;
77
use rustc_hir::def::DefKind;
88
use rustc_middle::mir::{self, BasicBlock, BasicBlocks, Body, Terminator, TerminatorKind};
99
use rustc_middle::ty::{self, GenericArg, GenericArgs, Instance, Ty, TyCtxt};
10+
use rustc_session::lint::LintId;
1011
use rustc_session::lint::builtin::UNCONDITIONAL_RECURSION;
1112
use rustc_span::Span;
1213

@@ -16,6 +17,10 @@ use crate::pass_manager::MirLint;
1617
pub(super) struct CheckCallRecursion;
1718

1819
impl<'tcx> MirLint<'tcx> for CheckCallRecursion {
20+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
21+
!tcx.lints_that_dont_need_to_run(()).contains(&LintId::of(UNCONDITIONAL_RECURSION))
22+
}
23+
1924
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
2025
let def_id = body.source.def_id().expect_local();
2126

@@ -38,6 +43,10 @@ impl<'tcx> MirLint<'tcx> for CheckCallRecursion {
3843
pub(super) struct CheckDropRecursion;
3944

4045
impl<'tcx> MirLint<'tcx> for CheckDropRecursion {
46+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
47+
!tcx.lints_that_dont_need_to_run(()).contains(&LintId::of(UNCONDITIONAL_RECURSION))
48+
}
49+
4150
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
4251
let def_id = body.source.def_id().expect_local();
4352

compiler/rustc_mir_transform/src/check_const_item_mutation.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc_hir::HirId;
22
use rustc_middle::mir::visit::Visitor;
33
use rustc_middle::mir::*;
44
use rustc_middle::ty::TyCtxt;
5+
use rustc_session::lint::LintId;
56
use rustc_session::lint::builtin::CONST_ITEM_MUTATION;
67
use rustc_span::Span;
78
use rustc_span::def_id::DefId;
@@ -11,6 +12,10 @@ use crate::errors;
1112
pub(super) struct CheckConstItemMutation;
1213

1314
impl<'tcx> crate::MirLint<'tcx> for CheckConstItemMutation {
15+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
16+
!tcx.lints_that_dont_need_to_run(()).contains(&LintId::of(CONST_ITEM_MUTATION))
17+
}
18+
1419
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
1520
let mut checker = ConstMutationChecker { body, tcx, target_local: None };
1621
checker.visit_body(body);

compiler/rustc_mir_transform/src/check_null.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ use rustc_index::IndexVec;
22
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext};
33
use rustc_middle::mir::*;
44
use rustc_middle::ty::{Ty, TyCtxt};
5-
use rustc_session::Session;
65

76
use crate::check_pointers::{BorrowedFieldProjectionMode, PointerCheck, check_pointers};
87

98
pub(super) struct CheckNull;
109

1110
impl<'tcx> crate::MirPass<'tcx> for CheckNull {
12-
fn is_enabled(&self, sess: &Session) -> bool {
13-
sess.ub_checks()
11+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
12+
tcx.sess.ub_checks()
1413
}
1514

1615
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/copy_prop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use crate::ssa::SsaLocals;
2020
pub(super) struct CopyProp;
2121

2222
impl<'tcx> crate::MirPass<'tcx> for CopyProp {
23-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
24-
sess.mir_opt_level() >= 1
23+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
24+
tcx.sess.mir_opt_level() >= 1
2525
}
2626

2727
#[instrument(level = "trace", skip(self, tcx, body))]

compiler/rustc_mir_transform/src/coverage/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use crate::coverage::mappings::ExtractedMappings;
2929
pub(super) struct InstrumentCoverage;
3030

3131
impl<'tcx> crate::MirPass<'tcx> for InstrumentCoverage {
32-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
33-
sess.instrument_coverage()
32+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
33+
tcx.sess.instrument_coverage()
3434
}
3535

3636
fn run_pass(&self, tcx: TyCtxt<'tcx>, mir_body: &mut mir::Body<'tcx>) {

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const PLACE_LIMIT: usize = 100;
3535
pub(super) struct DataflowConstProp;
3636

3737
impl<'tcx> crate::MirPass<'tcx> for DataflowConstProp {
38-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
39-
sess.mir_opt_level() >= 3
38+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
39+
tcx.sess.mir_opt_level() >= 3
4040
}
4141

4242
#[instrument(skip_all level = "debug")]

compiler/rustc_mir_transform/src/dead_store_elimination.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ impl<'tcx> crate::MirPass<'tcx> for DeadStoreElimination {
140140
}
141141
}
142142

143-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
144-
sess.mir_opt_level() >= 2
143+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
144+
tcx.sess.mir_opt_level() >= 2
145145
}
146146

147147
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/dest_prop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ use tracing::{debug, trace};
149149
pub(super) struct DestinationPropagation;
150150

151151
impl<'tcx> crate::MirPass<'tcx> for DestinationPropagation {
152-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
152+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
153153
// For now, only run at MIR opt level 3. Two things need to be changed before this can be
154154
// turned on by default:
155155
// 1. Because of the overeager removal of storage statements, this can cause stack space
@@ -158,7 +158,7 @@ impl<'tcx> crate::MirPass<'tcx> for DestinationPropagation {
158158
// 2. Despite being an overall perf improvement, this still causes a 30% regression in
159159
// keccak. We can temporarily fix this by bounding function size, but in the long term
160160
// we should fix this by being smarter about invalidating analysis results.
161-
sess.mir_opt_level() >= 3
161+
tcx.sess.mir_opt_level() >= 3
162162
}
163163

164164
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ use crate::patch::MirPatch;
9393
pub(super) struct EarlyOtherwiseBranch;
9494

9595
impl<'tcx> crate::MirPass<'tcx> for EarlyOtherwiseBranch {
96-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
97-
sess.mir_opt_level() >= 2
96+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
97+
tcx.sess.mir_opt_level() >= 2
9898
}
9999

100100
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ impl<'a, P: std::fmt::Debug> LintDiagnostic<'a, ()> for AssertLint<P> {
9292
}
9393

9494
impl AssertLintKind {
95+
pub(crate) fn all_lints() -> [&'static Lint; 2] {
96+
[lint::builtin::ARITHMETIC_OVERFLOW, lint::builtin::UNCONDITIONAL_PANIC]
97+
}
98+
9599
pub(crate) fn lint(&self) -> &'static Lint {
96100
match self {
97101
AssertLintKind::ArithmeticOverflow => lint::builtin::ARITHMETIC_OVERFLOW,

compiler/rustc_mir_transform/src/function_item_references.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_hir::def_id::DefId;
44
use rustc_middle::mir::visit::Visitor;
55
use rustc_middle::mir::*;
66
use rustc_middle::ty::{self, EarlyBinder, GenericArgsRef, Ty, TyCtxt};
7+
use rustc_session::lint::LintId;
78
use rustc_session::lint::builtin::FUNCTION_ITEM_REFERENCES;
89
use rustc_span::source_map::Spanned;
910
use rustc_span::{Span, sym};
@@ -13,6 +14,10 @@ use crate::errors;
1314
pub(super) struct FunctionItemReferences;
1415

1516
impl<'tcx> crate::MirLint<'tcx> for FunctionItemReferences {
17+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
18+
!tcx.lints_that_dont_need_to_run(()).contains(&LintId::of(FUNCTION_ITEM_REFERENCES))
19+
}
20+
1621
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
1722
let mut checker = FunctionItemRefChecker { tcx, body };
1823
checker.visit_body(body);

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ use crate::ssa::SsaLocals;
114114
pub(super) struct GVN;
115115

116116
impl<'tcx> crate::MirPass<'tcx> for GVN {
117-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
118-
sess.mir_opt_level() >= 2
117+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
118+
tcx.sess.mir_opt_level() >= 2
119119
}
120120

121121
#[instrument(level = "trace", skip(self, tcx, body))]

compiler/rustc_mir_transform/src/inline.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,17 @@ struct CallSite<'tcx> {
4343
pub struct Inline;
4444

4545
impl<'tcx> crate::MirPass<'tcx> for Inline {
46-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
47-
if let Some(enabled) = sess.opts.unstable_opts.inline_mir {
46+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
47+
if let Some(enabled) = tcx.sess.opts.unstable_opts.inline_mir {
4848
return enabled;
4949
}
5050

51-
match sess.mir_opt_level() {
51+
match tcx.sess.mir_opt_level() {
5252
0 | 1 => false,
5353
2 => {
54-
(sess.opts.optimize == OptLevel::More || sess.opts.optimize == OptLevel::Aggressive)
55-
&& sess.opts.incremental == None
54+
(tcx.sess.opts.optimize == OptLevel::More
55+
|| tcx.sess.opts.optimize == OptLevel::Aggressive)
56+
&& tcx.sess.opts.incremental == None
5657
}
5758
_ => true,
5859
}
@@ -82,7 +83,7 @@ impl ForceInline {
8283
}
8384

8485
impl<'tcx> crate::MirPass<'tcx> for ForceInline {
85-
fn is_enabled(&self, _: &rustc_session::Session) -> bool {
86+
fn is_enabled(&self, _: TyCtxt<'tcx>) -> bool {
8687
true
8788
}
8889

compiler/rustc_mir_transform/src/instsimplify.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
2424
}
2525
}
2626

27-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
28-
sess.mir_opt_level() > 0
27+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
28+
tcx.sess.mir_opt_level() > 0
2929
}
3030

3131
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/jump_threading.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ const MAX_COST: usize = 100;
6161
const MAX_PLACES: usize = 100;
6262

6363
impl<'tcx> crate::MirPass<'tcx> for JumpThreading {
64-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
65-
sess.mir_opt_level() >= 2
64+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
65+
tcx.sess.mir_opt_level() >= 2
6666
}
6767

6868
#[instrument(skip_all level = "debug")]

compiler/rustc_mir_transform/src/known_panics_lint.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceC
1919
use rustc_middle::mir::*;
2020
use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
2121
use rustc_middle::ty::{self, ConstInt, ScalarInt, Ty, TyCtxt, TypeVisitableExt};
22+
use rustc_session::lint::LintId;
2223
use rustc_span::Span;
2324
use tracing::{debug, instrument, trace};
2425

@@ -27,6 +28,11 @@ use crate::errors::{AssertLint, AssertLintKind};
2728
pub(super) struct KnownPanicsLint;
2829

2930
impl<'tcx> crate::MirLint<'tcx> for KnownPanicsLint {
31+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
32+
let ignored_lints = tcx.lints_that_dont_need_to_run(());
33+
!AssertLintKind::all_lints().iter().all(|lint| ignored_lints.contains(&LintId::of(lint)))
34+
}
35+
3036
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
3137
if body.tainted_by_errors.is_some() {
3238
return;

compiler/rustc_mir_transform/src/large_enums.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rustc_middle::mir::interpret::AllocId;
44
use rustc_middle::mir::*;
55
use rustc_middle::ty::util::IntTypeExt;
66
use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt};
7-
use rustc_session::Session;
87

98
use crate::patch::MirPatch;
109

@@ -29,11 +28,11 @@ pub(super) struct EnumSizeOpt {
2928
}
3029

3130
impl<'tcx> crate::MirPass<'tcx> for EnumSizeOpt {
32-
fn is_enabled(&self, sess: &Session) -> bool {
31+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
3332
// There are some differences in behavior on wasm and ARM that are not properly
3433
// understood, so we conservatively treat this optimization as unsound:
3534
// https://github.com/rust-lang/rust/pull/85158#issuecomment-1101836457
36-
sess.opts.unstable_opts.unsound_mir_opts || sess.mir_opt_level() >= 3
35+
tcx.sess.opts.unstable_opts.unsound_mir_opts || tcx.sess.mir_opt_level() >= 3
3736
}
3837

3938
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/lower_slice_len.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use rustc_middle::ty::TyCtxt;
88
pub(super) struct LowerSliceLenCalls;
99

1010
impl<'tcx> crate::MirPass<'tcx> for LowerSliceLenCalls {
11-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
12-
sess.mir_opt_level() > 0
11+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
12+
tcx.sess.mir_opt_level() > 0
1313
}
1414

1515
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/match_branches.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use crate::patch::MirPatch;
1313
pub(super) struct MatchBranchSimplification;
1414

1515
impl<'tcx> crate::MirPass<'tcx> for MatchBranchSimplification {
16-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
17-
sess.mir_opt_level() >= 1
16+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
17+
tcx.sess.mir_opt_level() >= 1
1818
}
1919

2020
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/mentioned_items.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use rustc_middle::mir::visit::Visitor;
22
use rustc_middle::mir::{self, Location, MentionedItem};
33
use rustc_middle::ty::adjustment::PointerCoercion;
44
use rustc_middle::ty::{self, TyCtxt};
5-
use rustc_session::Session;
65
use rustc_span::source_map::Spanned;
76

87
pub(super) struct MentionedItems;
@@ -14,7 +13,7 @@ struct MentionedItemsVisitor<'a, 'tcx> {
1413
}
1514

1615
impl<'tcx> crate::MirPass<'tcx> for MentionedItems {
17-
fn is_enabled(&self, _sess: &Session) -> bool {
16+
fn is_enabled(&self, _tcx: TyCtxt<'tcx>) -> bool {
1817
// If this pass is skipped the collector assume that nothing got mentioned! We could
1918
// potentially skip it in opt-level 0 if we are sure that opt-level will never *remove* uses
2019
// of anything, but that still seems fragile. Furthermore, even debug builds use level 1, so

compiler/rustc_mir_transform/src/multiple_return_terminators.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::simplify;
1010
pub(super) struct MultipleReturnTerminators;
1111

1212
impl<'tcx> crate::MirPass<'tcx> for MultipleReturnTerminators {
13-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
14-
sess.mir_opt_level() >= 4
13+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
14+
tcx.sess.mir_opt_level() >= 4
1515
}
1616

1717
fn run_pass(&self, _: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/nrvo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ use tracing::{debug, trace};
3333
pub(super) struct RenameReturnPlace;
3434

3535
impl<'tcx> crate::MirPass<'tcx> for RenameReturnPlace {
36-
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
36+
fn is_enabled(&self, tcx: TyCtxt<'tcx>) -> bool {
3737
// unsound: #111005
38-
sess.mir_opt_level() > 0 && sess.opts.unstable_opts.unsound_mir_opts
38+
tcx.sess.mir_opt_level() > 0 && tcx.sess.opts.unstable_opts.unsound_mir_opts
3939
}
4040

4141
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut mir::Body<'tcx>) {

0 commit comments

Comments
 (0)