Skip to content

Commit 4ba83cb

Browse files
authored
Merge pull request rust-lang#1220 from bjorn3/dont_print_on_trap
Replace a lot of print+trap with plain trap
2 parents 90f8aef + d8e7501 commit 4ba83cb

File tree

7 files changed

+16
-44
lines changed

7 files changed

+16
-44
lines changed

src/abi/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
507507
let ret_block = fx.get_block(dest);
508508
fx.bcx.ins().jump(ret_block, &[]);
509509
} else {
510-
trap_unreachable(fx, "[corruption] Diverging function returned");
510+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
511511
}
512512
}
513513

src/base.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub(crate) fn codegen_fn<'tcx>(
9090
} else if arg_uninhabited {
9191
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
9292
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
93-
crate::trap::trap_unreachable(&mut fx, "function has uninhabited argument");
93+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
9494
} else {
9595
tcx.sess.time("codegen clif ir", || {
9696
tcx.sess
@@ -424,18 +424,16 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) {
424424
fx.bcx.ins().jump(destination_block, &[]);
425425
}
426426
None => {
427-
crate::trap::trap_unreachable(
428-
fx,
429-
"[corruption] Returned from noreturn inline asm",
430-
);
427+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
431428
}
432429
}
433430
}
434431
TerminatorKind::Resume | TerminatorKind::Abort => {
435-
trap_unreachable(fx, "[corruption] Unwinding bb reached.");
432+
// FIXME implement unwinding
433+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
436434
}
437435
TerminatorKind::Unreachable => {
438-
trap_unreachable(fx, "[corruption] Hit unreachable code.");
436+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
439437
}
440438
TerminatorKind::Yield { .. }
441439
| TerminatorKind::FalseEdge { .. }
@@ -925,5 +923,5 @@ pub(crate) fn codegen_panic_inner<'tcx>(
925923
args,
926924
);
927925

928-
crate::trap::trap_unreachable(fx, "panic lang item returned");
926+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
929927
}

src/discriminant.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,10 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
6868
let layout = value.layout();
6969

7070
if layout.abi == Abi::Uninhabited {
71-
return trap_unreachable_ret_value(
72-
fx,
73-
dest_layout,
74-
"[panic] Tried to get discriminant for uninhabited type.",
75-
);
71+
let true_ = fx.bcx.ins().iconst(types::I32, 1);
72+
fx.bcx.ins().trapnz(true_, TrapCode::UnreachableCodeReached);
73+
// Return a dummy value
74+
return CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout);
7675
}
7776

7877
let (tag_scalar, tag_field, tag_encoding) = match &layout.variants {

src/intrinsics/llvm.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,9 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>(
126126
};
127127
}
128128

129-
if let Some((_, dest)) = destination {
130-
let ret_block = fx.get_block(dest);
131-
fx.bcx.ins().jump(ret_block, &[]);
132-
} else {
133-
trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");
134-
}
129+
let dest = destination.expect("all llvm intrinsics used by stdlib should return").1;
130+
let ret_block = fx.get_block(dest);
131+
fx.bcx.ins().jump(ret_block, &[]);
135132
}
136133

137134
// llvm.x86.avx2.vperm2i128

src/intrinsics/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
229229
// Insert non returning intrinsics here
230230
match intrinsic {
231231
sym::abort => {
232-
trap_abort(fx, "Called intrinsic::abort.");
232+
fx.bcx.ins().trap(TrapCode::User(0));
233233
}
234234
sym::transmute => {
235235
crate::base::codegen_panic(fx, "Transmuting to uninhabited type.", span);
@@ -1119,6 +1119,6 @@ fn codegen_regular_intrinsic_call<'tcx>(
11191119
let ret_block = fx.get_block(dest);
11201120
fx.bcx.ins().jump(ret_block, &[]);
11211121
} else {
1122-
trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");
1122+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
11231123
}
11241124
}

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ mod prelude {
105105
pub(crate) use crate::common::*;
106106
pub(crate) use crate::debuginfo::{DebugContext, UnwindContext};
107107
pub(crate) use crate::pointer::Pointer;
108-
pub(crate) use crate::trap::*;
109108
pub(crate) use crate::value_and_place::{CPlace, CPlaceInner, CValue};
110109
}
111110

src/trap.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ fn codegen_print(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) {
2525
fx.bcx.ins().call(puts, &[msg_ptr]);
2626
}
2727

28-
/// Trap code: user1
29-
pub(crate) fn trap_abort(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef<str>) {
30-
codegen_print(fx, msg.as_ref());
31-
fx.bcx.ins().trap(TrapCode::User(1));
32-
}
33-
3428
/// Use this for example when a function call should never return. This will fill the current block,
3529
/// so you can **not** add instructions to it afterwards.
3630
///
@@ -39,21 +33,6 @@ pub(crate) fn trap_unreachable(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef<
3933
codegen_print(fx, msg.as_ref());
4034
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
4135
}
42-
43-
/// Like `trap_unreachable` but returns a fake value of the specified type.
44-
///
45-
/// Trap code: user65535
46-
pub(crate) fn trap_unreachable_ret_value<'tcx>(
47-
fx: &mut FunctionCx<'_, '_, 'tcx>,
48-
dest_layout: TyAndLayout<'tcx>,
49-
msg: impl AsRef<str>,
50-
) -> CValue<'tcx> {
51-
codegen_print(fx, msg.as_ref());
52-
let true_ = fx.bcx.ins().iconst(types::I32, 1);
53-
fx.bcx.ins().trapnz(true_, TrapCode::UnreachableCodeReached);
54-
CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
55-
}
56-
5736
/// Use this when something is unimplemented, but `libcore` or `libstd` requires it to codegen.
5837
/// Unlike `trap_unreachable` this will not fill the current block, so you **must** add instructions
5938
/// to it afterwards.

0 commit comments

Comments
 (0)