Skip to content

Commit 580067c

Browse files
committed
InstSimplify rustc_nounwind calls
1 parent 9842a5c commit 580067c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

compiler/rustc_mir_transform/src/instsimplify.rs

+25
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
33
use crate::simplify::simplify_duplicate_switch_targets;
44
use rustc_middle::mir::*;
5+
use rustc_middle::ty::layout;
56
use rustc_middle::ty::layout::ValidityRequirement;
67
use rustc_middle::ty::{self, GenericArgsRef, ParamEnv, Ty, TyCtxt};
78
use rustc_span::symbol::Symbol;
89
use rustc_target::abi::FieldIdx;
10+
use rustc_target::spec::abi::Abi;
911

1012
pub struct InstSimplify;
1113

@@ -38,6 +40,7 @@ impl<'tcx> MirPass<'tcx> for InstSimplify {
3840
block.terminator.as_mut().unwrap(),
3941
&mut block.statements,
4042
);
43+
ctx.simplify_nounwind_call(block.terminator.as_mut().unwrap());
4144
simplify_duplicate_switch_targets(block.terminator.as_mut().unwrap());
4245
}
4346
}
@@ -252,6 +255,28 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
252255
terminator.kind = TerminatorKind::Goto { target: destination_block };
253256
}
254257

258+
fn simplify_nounwind_call(&self, terminator: &mut Terminator<'tcx>) {
259+
let TerminatorKind::Call { func, unwind, .. } = &mut terminator.kind else {
260+
return;
261+
};
262+
263+
let Some((def_id, _)) = func.const_fn_def() else {
264+
return;
265+
};
266+
267+
let body_ty = self.tcx.type_of(def_id).skip_binder();
268+
let body_abi = match body_ty.kind() {
269+
ty::FnDef(..) => body_ty.fn_sig(self.tcx).abi(),
270+
ty::Closure(..) => Abi::RustCall,
271+
ty::Coroutine(..) => Abi::Rust,
272+
_ => bug!("unexpected body ty: {:?}", body_ty),
273+
};
274+
275+
if !layout::fn_can_unwind(self.tcx, Some(def_id), body_abi) {
276+
*unwind = UnwindAction::Unreachable;
277+
}
278+
}
279+
255280
fn simplify_intrinsic_assert(
256281
&self,
257282
terminator: &mut Terminator<'tcx>,

0 commit comments

Comments
 (0)