diff --git a/src/abi/mod.rs b/src/abi/mod.rs index 3d527bd72b6..ef56fb191bf 100644 --- a/src/abi/mod.rs +++ b/src/abi/mod.rs @@ -507,7 +507,7 @@ pub(crate) fn codegen_terminator_call<'tcx>( let ret_block = fx.get_block(dest); fx.bcx.ins().jump(ret_block, &[]); } else { - trap_unreachable(fx, "[corruption] Diverging function returned"); + fx.bcx.ins().trap(TrapCode::UnreachableCodeReached); } } diff --git a/src/base.rs b/src/base.rs index 59e9e23d882..0a0d17a69c5 100644 --- a/src/base.rs +++ b/src/base.rs @@ -90,7 +90,7 @@ pub(crate) fn codegen_fn<'tcx>( } else if arg_uninhabited { fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]); fx.bcx.switch_to_block(fx.block_map[START_BLOCK]); - crate::trap::trap_unreachable(&mut fx, "function has uninhabited argument"); + fx.bcx.ins().trap(TrapCode::UnreachableCodeReached); } else { tcx.sess.time("codegen clif ir", || { tcx.sess @@ -424,18 +424,16 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) { fx.bcx.ins().jump(destination_block, &[]); } None => { - crate::trap::trap_unreachable( - fx, - "[corruption] Returned from noreturn inline asm", - ); + fx.bcx.ins().trap(TrapCode::UnreachableCodeReached); } } } TerminatorKind::Resume | TerminatorKind::Abort => { - trap_unreachable(fx, "[corruption] Unwinding bb reached."); + // FIXME implement unwinding + fx.bcx.ins().trap(TrapCode::UnreachableCodeReached); } TerminatorKind::Unreachable => { - trap_unreachable(fx, "[corruption] Hit unreachable code."); + fx.bcx.ins().trap(TrapCode::UnreachableCodeReached); } TerminatorKind::Yield { .. } | TerminatorKind::FalseEdge { .. } @@ -925,5 +923,5 @@ pub(crate) fn codegen_panic_inner<'tcx>( args, ); - crate::trap::trap_unreachable(fx, "panic lang item returned"); + fx.bcx.ins().trap(TrapCode::UnreachableCodeReached); } diff --git a/src/discriminant.rs b/src/discriminant.rs index 3326f87f000..6b2893fdaeb 100644 --- a/src/discriminant.rs +++ b/src/discriminant.rs @@ -68,11 +68,10 @@ pub(crate) fn codegen_get_discriminant<'tcx>( let layout = value.layout(); if layout.abi == Abi::Uninhabited { - return trap_unreachable_ret_value( - fx, - dest_layout, - "[panic] Tried to get discriminant for uninhabited type.", - ); + let true_ = fx.bcx.ins().iconst(types::I32, 1); + fx.bcx.ins().trapnz(true_, TrapCode::UnreachableCodeReached); + // Return a dummy value + return CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout); } let (tag_scalar, tag_field, tag_encoding) = match &layout.variants { diff --git a/src/intrinsics/llvm.rs b/src/intrinsics/llvm.rs index 098862b0662..0e4f7ee907a 100644 --- a/src/intrinsics/llvm.rs +++ b/src/intrinsics/llvm.rs @@ -126,12 +126,9 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>( }; } - if let Some((_, dest)) = destination { - let ret_block = fx.get_block(dest); - fx.bcx.ins().jump(ret_block, &[]); - } else { - trap_unreachable(fx, "[corruption] Diverging intrinsic returned."); - } + let dest = destination.expect("all llvm intrinsics used by stdlib should return").1; + let ret_block = fx.get_block(dest); + fx.bcx.ins().jump(ret_block, &[]); } // llvm.x86.avx2.vperm2i128 diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index 8411ec0e035..eeda0dd6f70 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -229,7 +229,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( // Insert non returning intrinsics here match intrinsic { sym::abort => { - trap_abort(fx, "Called intrinsic::abort."); + fx.bcx.ins().trap(TrapCode::User(0)); } sym::transmute => { crate::base::codegen_panic(fx, "Transmuting to uninhabited type.", span); @@ -1119,6 +1119,6 @@ fn codegen_regular_intrinsic_call<'tcx>( let ret_block = fx.get_block(dest); fx.bcx.ins().jump(ret_block, &[]); } else { - trap_unreachable(fx, "[corruption] Diverging intrinsic returned."); + fx.bcx.ins().trap(TrapCode::UnreachableCodeReached); } } diff --git a/src/lib.rs b/src/lib.rs index a3c794fb156..878b9390e13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,7 +105,6 @@ mod prelude { pub(crate) use crate::common::*; pub(crate) use crate::debuginfo::{DebugContext, UnwindContext}; pub(crate) use crate::pointer::Pointer; - pub(crate) use crate::trap::*; pub(crate) use crate::value_and_place::{CPlace, CPlaceInner, CValue}; } diff --git a/src/trap.rs b/src/trap.rs index 99b5366e349..923269c4de9 100644 --- a/src/trap.rs +++ b/src/trap.rs @@ -25,12 +25,6 @@ fn codegen_print(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) { fx.bcx.ins().call(puts, &[msg_ptr]); } -/// Trap code: user1 -pub(crate) fn trap_abort(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef) { - codegen_print(fx, msg.as_ref()); - fx.bcx.ins().trap(TrapCode::User(1)); -} - /// Use this for example when a function call should never return. This will fill the current block, /// so you can **not** add instructions to it afterwards. /// @@ -39,21 +33,6 @@ pub(crate) fn trap_unreachable(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef< codegen_print(fx, msg.as_ref()); fx.bcx.ins().trap(TrapCode::UnreachableCodeReached); } - -/// Like `trap_unreachable` but returns a fake value of the specified type. -/// -/// Trap code: user65535 -pub(crate) fn trap_unreachable_ret_value<'tcx>( - fx: &mut FunctionCx<'_, '_, 'tcx>, - dest_layout: TyAndLayout<'tcx>, - msg: impl AsRef, -) -> CValue<'tcx> { - codegen_print(fx, msg.as_ref()); - let true_ = fx.bcx.ins().iconst(types::I32, 1); - fx.bcx.ins().trapnz(true_, TrapCode::UnreachableCodeReached); - CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout) -} - /// Use this when something is unimplemented, but `libcore` or `libstd` requires it to codegen. /// Unlike `trap_unreachable` this will not fill the current block, so you **must** add instructions /// to it afterwards.