Skip to content

Commit 0d16dcf

Browse files
committed
Don't try to store func_addr for FnDef in trans_const_value
This would crash, because the place provides 0 bytes of space for FnDef
1 parent 07c693b commit 0d16dcf

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/base.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,17 @@ fn trans_stmt<'a, 'tcx: 'a>(
474474
lval.write_cvalue(fx, CValue::ByVal(res, layout));
475475
}
476476
Rvalue::Cast(CastKind::ReifyFnPointer, operand, ty) => {
477-
let operand = trans_operand(fx, operand);
478477
let layout = fx.layout_of(ty);
479-
lval.write_cvalue(fx, operand.unchecked_cast_to(layout));
478+
match fx.monomorphize(&operand.ty(&fx.mir.local_decls, fx.tcx)).sty {
479+
ty::FnDef(def_id, substs) => {
480+
let func_ref = fx.get_function_ref(
481+
Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs).unwrap(),
482+
);
483+
let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
484+
lval.write_cvalue(fx, CValue::ByVal(func_addr, layout));
485+
}
486+
_ => bug!("Trying to ReifyFnPointer on non FnDef {:?}", ty),
487+
}
480488
}
481489
Rvalue::Cast(CastKind::UnsafeFnPointer, operand, ty) => {
482490
let operand = trans_operand(fx, operand);

src/constant.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,11 @@ fn trans_const_value<'a, 'tcx: 'a>(
110110
let bits = const_.val.try_to_bits(layout.size).unwrap();
111111
CValue::const_val(fx, ty, bits as i128 as i64)
112112
}
113-
ty::FnDef(def_id, substs) => {
114-
let func_ref = fx.get_function_ref(
115-
Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs).unwrap(),
116-
);
117-
let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
118-
CValue::ByVal(func_addr, layout)
113+
ty::FnDef(_def_id, _substs) => {
114+
CValue::ByRef(
115+
fx.bcx.ins().iconst(fx.pointer_type, 0),
116+
layout
117+
)
119118
}
120119
_ => trans_const_place(fx, const_).to_cvalue(fx),
121120
}

0 commit comments

Comments
 (0)