Skip to content

Commit 9ca823d

Browse files
committed
Streamline AbortUnwindingCalls.
Currently it constructs two vectors `calls_to_terminated` and `cleanups_to_remove` in the main loop, and then processes them after the main loop. But the processing can be done in the main loop, avoiding the need for the vectors.
1 parent 1d28d6f commit 9ca823d

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

compiler/rustc_mir_transform/src/abort_unwinding_calls.rs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ impl<'tcx> 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> 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> 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
}

0 commit comments

Comments
 (0)