Skip to content

Commit 11af20b

Browse files
committed
Fix panic with reported const_eval promoted error
1 parent a5fdefe commit 11af20b

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ pub fn trans_place<'a, 'tcx: 'a>(
11341134
match place {
11351135
Place::Base(base) => match base {
11361136
PlaceBase::Local(local) => fx.get_local_place(*local),
1137-
PlaceBase::Promoted(promoted) => crate::constant::trans_promoted(fx, promoted.0),
1137+
PlaceBase::Promoted(data) => crate::constant::trans_promoted(fx, data.0, data.1),
11381138
PlaceBase::Static(static_) => crate::constant::codegen_static_ref(fx, static_),
11391139
}
11401140
Place::Projection(projection) => {

src/constant.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,22 @@ pub fn codegen_static_ref<'a, 'tcx: 'a>(
5353
pub fn trans_promoted<'a, 'tcx: 'a>(
5454
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
5555
promoted: Promoted,
56+
dest_ty: Ty<'tcx>,
5657
) -> CPlace<'tcx> {
57-
let const_ = fx
58+
match fx
5859
.tcx
5960
.const_eval(ParamEnv::reveal_all().and(GlobalId {
6061
instance: fx.instance,
6162
promoted: Some(promoted),
6263
}))
63-
.unwrap();
64-
65-
trans_const_place(fx, const_)
64+
{
65+
Ok(const_) => {
66+
let cplace = trans_const_place(fx, const_);
67+
debug_assert_eq!(cplace.layout(), fx.layout_of(dest_ty));
68+
cplace
69+
}
70+
Err(_) => crate::trap::trap_unreachable_ret_place(fx, fx.layout_of(dest_ty)),
71+
}
6672
}
6773

6874
pub fn trans_constant<'a, 'tcx: 'a>(

src/trap.rs

+7
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ pub fn trap_unreachable_ret_value<'tcx>(fx: &mut FunctionCx<'_, 'tcx, impl crane
1616
let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
1717
CValue::ByRef(zero, dest_layout)
1818
}
19+
20+
pub fn trap_unreachable_ret_place<'tcx>(fx: &mut FunctionCx<'_, 'tcx, impl cranelift_module::Backend>, dest_layout: TyLayout<'tcx>) -> CPlace<'tcx> {
21+
let true_ = fx.bcx.ins().iconst(types::I32, 1);
22+
fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
23+
let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
24+
CPlace::Addr(zero, None, dest_layout)
25+
}

0 commit comments

Comments
 (0)