Skip to content

Commit 3e3b148

Browse files
authoredSep 9, 2024
Rollup merge of #129929 - nnethercote:rustc_mir_transform-cleanups-2, r=cjgillot
`rustc_mir_transform` cleanups, round 2 More cleanups in the style of #129738. r? ``@cjgillot``
2 parents 38e3a57 + 5445953 commit 3e3b148

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

Lines changed: 15 additions & 28 deletions
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

Lines changed: 3 additions & 3 deletions
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;

0 commit comments

Comments
 (0)