Skip to content

Commit 1c4c089

Browse files
authored
Rollup merge of rust-lang#129929 - nnethercote:rustc_mir_transform-cleanups-2, r=cjgillot
`rustc_mir_transform` cleanups, round 2 More cleanups in the style of rust-lang#129738. r? `@cjgillot`
2 parents 59fe4e9 + 5445953 commit 1c4c089

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+277
-311
lines changed

compiler/rustc_mir_transform/src/abort_unwinding_calls.rs

+15-28
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_target::spec::PanicStrategy;
2020
/// This forces all unwinds, in panic=abort mode happening in foreign code, to
2121
/// trigger a process abort.
2222
#[derive(PartialEq)]
23-
pub struct AbortUnwindingCalls;
23+
pub(super) struct AbortUnwindingCalls;
2424

2525
impl<'tcx> crate::MirPass<'tcx> for AbortUnwindingCalls {
2626
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
@@ -50,9 +50,7 @@ impl<'tcx> crate::MirPass<'tcx> for AbortUnwindingCalls {
5050
// with a function call, and whose function we're calling may unwind.
5151
// This will filter to functions with `extern "C-unwind"` ABIs, for
5252
// example.
53-
let mut calls_to_terminate = Vec::new();
54-
let mut cleanups_to_remove = Vec::new();
55-
for (id, block) in body.basic_blocks.iter_enumerated() {
53+
for block in body.basic_blocks.as_mut() {
5654
if block.is_cleanup {
5755
continue;
5856
}
@@ -61,7 +59,7 @@ impl<'tcx> crate::MirPass<'tcx> for AbortUnwindingCalls {
6159

6260
let call_can_unwind = match &terminator.kind {
6361
TerminatorKind::Call { func, .. } => {
64-
let ty = func.ty(body, tcx);
62+
let ty = func.ty(&body.local_decls, tcx);
6563
let sig = ty.fn_sig(tcx);
6664
let fn_def_id = match ty.kind() {
6765
ty::FnPtr(..) => None,
@@ -86,33 +84,22 @@ impl<'tcx> crate::MirPass<'tcx> for AbortUnwindingCalls {
8684
_ => continue,
8785
};
8886

89-
// If this function call can't unwind, then there's no need for it
90-
// to have a landing pad. This means that we can remove any cleanup
91-
// registered for it.
9287
if !call_can_unwind {
93-
cleanups_to_remove.push(id);
94-
continue;
95-
}
96-
97-
// Otherwise if this function can unwind, then if the outer function
98-
// can also unwind there's nothing to do. If the outer function
99-
// can't unwind, however, we need to change the landing pad for this
100-
// function call to one that aborts.
101-
if !body_can_unwind {
102-
calls_to_terminate.push(id);
88+
// If this function call can't unwind, then there's no need for it
89+
// to have a landing pad. This means that we can remove any cleanup
90+
// registered for it.
91+
let cleanup = block.terminator_mut().unwind_mut().unwrap();
92+
*cleanup = UnwindAction::Unreachable;
93+
} else if !body_can_unwind {
94+
// Otherwise if this function can unwind, then if the outer function
95+
// can also unwind there's nothing to do. If the outer function
96+
// can't unwind, however, we need to change the landing pad for this
97+
// function call to one that aborts.
98+
let cleanup = block.terminator_mut().unwind_mut().unwrap();
99+
*cleanup = UnwindAction::Terminate(UnwindTerminateReason::Abi);
103100
}
104101
}
105102

106-
for id in calls_to_terminate {
107-
let cleanup = body.basic_blocks_mut()[id].terminator_mut().unwind_mut().unwrap();
108-
*cleanup = UnwindAction::Terminate(UnwindTerminateReason::Abi);
109-
}
110-
111-
for id in cleanups_to_remove {
112-
let cleanup = body.basic_blocks_mut()[id].terminator_mut().unwind_mut().unwrap();
113-
*cleanup = UnwindAction::Unreachable;
114-
}
115-
116103
// We may have invalidated some `cleanup` blocks so clean those up now.
117104
super::simplify::remove_dead_blocks(body);
118105
}

compiler/rustc_mir_transform/src/add_call_guards.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use rustc_middle::ty::TyCtxt;
44
use tracing::debug;
55

66
#[derive(PartialEq)]
7-
pub enum AddCallGuards {
7+
pub(super) enum AddCallGuards {
88
AllCallEdges,
99
CriticalCallEdges,
1010
}
11-
pub use self::AddCallGuards::*;
11+
pub(super) use self::AddCallGuards::*;
1212

1313
/**
1414
* Breaks outgoing critical edges for call terminators in the MIR.
@@ -37,7 +37,7 @@ impl<'tcx> crate::MirPass<'tcx> for AddCallGuards {
3737
}
3838

3939
impl AddCallGuards {
40-
pub fn add_call_guards(&self, body: &mut Body<'_>) {
40+
pub(super) fn add_call_guards(&self, body: &mut Body<'_>) {
4141
let mut pred_count: IndexVec<_, _> =
4242
body.basic_blocks.predecessors().iter().map(|ps| ps.len()).collect();
4343
pred_count[START_BLOCK] += 1;

compiler/rustc_mir_transform/src/add_moves_for_packed_drops.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::util;
3535
///
3636
/// The storage instructions are required to avoid stack space
3737
/// blowup.
38-
pub struct AddMovesForPackedDrops;
38+
pub(super) struct AddMovesForPackedDrops;
3939

4040
impl<'tcx> crate::MirPass<'tcx> for AddMovesForPackedDrops {
4141
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
@@ -44,7 +44,7 @@ impl<'tcx> crate::MirPass<'tcx> for AddMovesForPackedDrops {
4444
}
4545
}
4646

47-
pub fn add_moves_for_packed_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
47+
fn add_moves_for_packed_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
4848
let patch = add_moves_for_packed_drops_patch(tcx, body);
4949
patch.apply(body);
5050
}

compiler/rustc_mir_transform/src/add_retag.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::LangItem;
88
use rustc_middle::mir::*;
99
use rustc_middle::ty::{self, Ty, TyCtxt};
1010

11-
pub struct AddRetag;
11+
pub(super) struct AddRetag;
1212

1313
/// Determine whether this type may contain a reference (or box), and thus needs retagging.
1414
/// We will only recurse `depth` times into Tuples/ADTs to bound the cost of this.

compiler/rustc_mir_transform/src/add_subtyping_projections.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
use rustc_index::IndexVec;
21
use rustc_middle::mir::patch::MirPatch;
32
use rustc_middle::mir::visit::MutVisitor;
43
use rustc_middle::mir::*;
54
use rustc_middle::ty::TyCtxt;
65

7-
pub struct Subtyper;
6+
pub(super) struct Subtyper;
87

9-
pub struct SubTypeChecker<'a, 'tcx> {
8+
struct SubTypeChecker<'a, 'tcx> {
109
tcx: TyCtxt<'tcx>,
1110
patcher: MirPatch<'tcx>,
12-
local_decls: &'a IndexVec<Local, LocalDecl<'tcx>>,
11+
local_decls: &'a LocalDecls<'tcx>,
1312
}
1413

1514
impl<'a, 'tcx> MutVisitor<'tcx> for SubTypeChecker<'a, 'tcx> {
@@ -52,7 +51,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for SubTypeChecker<'a, 'tcx> {
5251
// // gets transformed to
5352
// let temp: rval_ty = rval;
5453
// let place: place_ty = temp as place_ty;
55-
pub fn subtype_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
54+
fn subtype_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
5655
let patch = MirPatch::new(body);
5756
let mut checker = SubTypeChecker { tcx, patcher: patch, local_decls: &body.local_decls };
5857

compiler/rustc_mir_transform/src/check_alignment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
77
use rustc_session::Session;
88
use tracing::{debug, trace};
99

10-
pub struct CheckAlignment;
10+
pub(super) struct CheckAlignment;
1111

1212
impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
1313
fn is_enabled(&self, sess: &Session) -> bool {

compiler/rustc_mir_transform/src/check_const_item_mutation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_span::Span;
88

99
use crate::errors;
1010

11-
pub struct CheckConstItemMutation;
11+
pub(super) struct CheckConstItemMutation;
1212

1313
impl<'tcx> crate::MirLint<'tcx> for CheckConstItemMutation {
1414
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {

compiler/rustc_mir_transform/src/check_packed_ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_middle::ty::{self, TyCtxt};
55

66
use crate::{errors, util};
77

8-
pub struct CheckPackedRef;
8+
pub(super) struct CheckPackedRef;
99

1010
impl<'tcx> crate::MirLint<'tcx> for CheckPackedRef {
1111
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {

compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_middle::mir::{Body, BorrowKind, CastKind, Rvalue, StatementKind, Termi
2121
use rustc_middle::ty::adjustment::PointerCoercion;
2222
use rustc_middle::ty::TyCtxt;
2323

24-
pub struct CleanupPostBorrowck;
24+
pub(super) struct CleanupPostBorrowck;
2525

2626
impl<'tcx> crate::MirPass<'tcx> for CleanupPostBorrowck {
2727
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

compiler/rustc_mir_transform/src/copy_prop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::ssa::SsaLocals;
1717
/// where each of the locals is only assigned once.
1818
///
1919
/// We want to replace all those locals by `_a`, either copied or moved.
20-
pub struct CopyProp;
20+
pub(super) struct CopyProp;
2121

2222
impl<'tcx> crate::MirPass<'tcx> for CopyProp {
2323
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {

compiler/rustc_mir_transform/src/coroutine.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
mod by_move_body;
5454
use std::{iter, ops};
5555

56-
pub use by_move_body::coroutine_by_move_body_def_id;
56+
pub(super) use by_move_body::coroutine_by_move_body_def_id;
5757
use rustc_data_structures::fx::FxHashSet;
5858
use rustc_errors::pluralize;
5959
use rustc_hir as hir;
@@ -85,7 +85,7 @@ use tracing::{debug, instrument, trace};
8585
use crate::deref_separator::deref_finder;
8686
use crate::{abort_unwinding_calls, errors, pass_manager as pm, simplify};
8787

88-
pub struct StateTransform;
88+
pub(super) struct StateTransform;
8989

9090
struct RenameLocalVisitor<'tcx> {
9191
from: Local,
@@ -1199,7 +1199,7 @@ fn insert_panic_block<'tcx>(
11991199
message: AssertMessage<'tcx>,
12001200
) -> BasicBlock {
12011201
let assert_block = BasicBlock::new(body.basic_blocks.len());
1202-
let term = TerminatorKind::Assert {
1202+
let kind = TerminatorKind::Assert {
12031203
cond: Operand::Constant(Box::new(ConstOperand {
12041204
span: body.span,
12051205
user_ty: None,
@@ -1211,14 +1211,7 @@ fn insert_panic_block<'tcx>(
12111211
unwind: UnwindAction::Continue,
12121212
};
12131213

1214-
let source_info = SourceInfo::outermost(body.span);
1215-
body.basic_blocks_mut().push(BasicBlockData {
1216-
statements: Vec::new(),
1217-
terminator: Some(Terminator { source_info, kind: term }),
1218-
is_cleanup: false,
1219-
});
1220-
1221-
assert_block
1214+
insert_term_block(body, kind)
12221215
}
12231216

12241217
fn can_return<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {

compiler/rustc_mir_transform/src/coroutine/by_move_body.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ use rustc_middle::ty::{self, InstanceKind, Ty, TyCtxt, TypeVisitableExt};
8282
use rustc_span::symbol::kw;
8383
use rustc_target::abi::{FieldIdx, VariantIdx};
8484

85-
pub fn coroutine_by_move_body_def_id<'tcx>(
85+
pub(crate) fn coroutine_by_move_body_def_id<'tcx>(
8686
tcx: TyCtxt<'tcx>,
8787
coroutine_def_id: LocalDefId,
8888
) -> DefId {

compiler/rustc_mir_transform/src/cost_checker.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const CONST_SWITCH_BONUS: usize = 10;
1212

1313
/// Verify that the callee body is compatible with the caller.
1414
#[derive(Clone)]
15-
pub(crate) struct CostChecker<'b, 'tcx> {
15+
pub(super) struct CostChecker<'b, 'tcx> {
1616
tcx: TyCtxt<'tcx>,
1717
param_env: ParamEnv<'tcx>,
1818
penalty: usize,
@@ -22,7 +22,7 @@ pub(crate) struct CostChecker<'b, 'tcx> {
2222
}
2323

2424
impl<'b, 'tcx> CostChecker<'b, 'tcx> {
25-
pub fn new(
25+
pub(super) fn new(
2626
tcx: TyCtxt<'tcx>,
2727
param_env: ParamEnv<'tcx>,
2828
instance: Option<ty::Instance<'tcx>>,
@@ -36,7 +36,7 @@ impl<'b, 'tcx> CostChecker<'b, 'tcx> {
3636
/// Needed because the `CostChecker` is used sometimes for just blocks,
3737
/// and even the full `Inline` doesn't call `visit_body`, so there's nowhere
3838
/// to put this logic in the visitor.
39-
pub fn add_function_level_costs(&mut self) {
39+
pub(super) fn add_function_level_costs(&mut self) {
4040
fn is_call_like(bbd: &BasicBlockData<'_>) -> bool {
4141
use TerminatorKind::*;
4242
match bbd.terminator().kind {
@@ -64,7 +64,7 @@ impl<'b, 'tcx> CostChecker<'b, 'tcx> {
6464
}
6565
}
6666

67-
pub fn cost(&self) -> usize {
67+
pub(super) fn cost(&self) -> usize {
6868
usize::saturating_sub(self.penalty, self.bonus)
6969
}
7070

compiler/rustc_mir_transform/src/coverage/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub mod query;
1+
pub(super) mod query;
22

33
mod counters;
44
mod graph;
@@ -32,7 +32,7 @@ use crate::coverage::mappings::ExtractedMappings;
3232
/// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected
3333
/// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen
3434
/// to construct the coverage map.
35-
pub struct InstrumentCoverage;
35+
pub(super) struct InstrumentCoverage;
3636

3737
impl<'tcx> crate::MirPass<'tcx> for InstrumentCoverage {
3838
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {

compiler/rustc_mir_transform/src/cross_crate_inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_span::sym;
1010

1111
use crate::{inline, pass_manager as pm};
1212

13-
pub fn provide(providers: &mut Providers) {
13+
pub(super) fn provide(providers: &mut Providers) {
1414
providers.cross_crate_inlinable = cross_crate_inlinable;
1515
}
1616

compiler/rustc_mir_transform/src/ctfe_limit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::mir::{
88
use rustc_middle::ty::TyCtxt;
99
use tracing::instrument;
1010

11-
pub struct CtfeLimit;
11+
pub(super) struct CtfeLimit;
1212

1313
impl<'tcx> crate::MirPass<'tcx> for CtfeLimit {
1414
#[instrument(skip(self, _tcx, body))]

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use tracing::{debug, debug_span, instrument};
2626
const BLOCK_LIMIT: usize = 100;
2727
const PLACE_LIMIT: usize = 100;
2828

29-
pub struct DataflowConstProp;
29+
pub(super) struct DataflowConstProp;
3030

3131
impl<'tcx> crate::MirPass<'tcx> for DataflowConstProp {
3232
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
@@ -332,7 +332,7 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
332332
}
333333

334334
impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
335-
pub fn new(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, map: Map<'tcx>) -> Self {
335+
fn new(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, map: Map<'tcx>) -> Self {
336336
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
337337
Self {
338338
map,

compiler/rustc_mir_transform/src/dead_store_elimination.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::util::is_within_packed;
2828
///
2929
/// The `borrowed` set must be a `BitSet` of all the locals that are ever borrowed in this body. It
3030
/// can be generated via the [`borrowed_locals`] function.
31-
pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
31+
fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
3232
let borrowed_locals = borrowed_locals(body);
3333

3434
// If the user requests complete debuginfo, mark the locals that appear in it as live, so
@@ -127,7 +127,7 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
127127
}
128128
}
129129

130-
pub enum DeadStoreElimination {
130+
pub(super) enum DeadStoreElimination {
131131
Initial,
132132
Final,
133133
}

compiler/rustc_mir_transform/src/deduce_param_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ fn type_will_always_be_passed_directly(ty: Ty<'_>) -> bool {
150150
/// body of the function instead of just the signature. These can be useful for optimization
151151
/// purposes on a best-effort basis. We compute them here and store them into the crate metadata so
152152
/// dependent crates can use them.
153-
pub fn deduced_param_attrs<'tcx>(
153+
pub(super) fn deduced_param_attrs<'tcx>(
154154
tcx: TyCtxt<'tcx>,
155155
def_id: LocalDefId,
156156
) -> &'tcx [DeducedParamAttrs] {

compiler/rustc_mir_transform/src/deduplicate_blocks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tracing::debug;
1313

1414
use super::simplify::simplify_cfg;
1515

16-
pub struct DeduplicateBlocks;
16+
pub(super) struct DeduplicateBlocks;
1717

1818
impl<'tcx> crate::MirPass<'tcx> for DeduplicateBlocks {
1919
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {

0 commit comments

Comments
 (0)