Skip to content

Commit a5fdefe

Browse files
committed
Fix trans_get_discriminant for uninhabited types
1 parent 8a9cadc commit a5fdefe

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ pub fn trans_get_discriminant<'a, 'tcx: 'a>(
739739
let layout = place.layout();
740740

741741
if layout.abi == layout::Abi::Uninhabited {
742-
trap_unreachable(&mut fx.bcx);
742+
return trap_unreachable_ret_value(fx, dest_layout);
743743
}
744744
match layout.variants {
745745
layout::Variants::Single { index } => {

src/trap.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use cranelift::prelude::*;
2-
3-
use cranelift::codegen::ir::TrapCode;
1+
use crate::prelude::*;
42

53
/// Trap code: user0
64
pub fn trap_panic(bcx: &mut FunctionBuilder) {
@@ -11,3 +9,10 @@ pub fn trap_panic(bcx: &mut FunctionBuilder) {
119
pub fn trap_unreachable(bcx: &mut FunctionBuilder) {
1210
bcx.ins().trap(TrapCode::User(!0));
1311
}
12+
13+
pub fn trap_unreachable_ret_value<'tcx>(fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>, dest_layout: TyLayout<'tcx>) -> CValue<'tcx> {
14+
let true_ = fx.bcx.ins().iconst(types::I32, 1);
15+
fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
16+
let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
17+
CValue::ByRef(zero, dest_layout)
18+
}

0 commit comments

Comments
 (0)