1
Fork 0

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
This commit is contained in:
bjorn3 2019-02-03 13:29:04 +01:00
parent 07c693b764
commit 0d16dcfce1
2 changed files with 15 additions and 8 deletions

View file

@ -474,9 +474,17 @@ fn trans_stmt<'a, 'tcx: 'a>(
lval.write_cvalue(fx, CValue::ByVal(res, layout)); lval.write_cvalue(fx, CValue::ByVal(res, layout));
} }
Rvalue::Cast(CastKind::ReifyFnPointer, operand, ty) => { Rvalue::Cast(CastKind::ReifyFnPointer, operand, ty) => {
let operand = trans_operand(fx, operand);
let layout = fx.layout_of(ty); let layout = fx.layout_of(ty);
lval.write_cvalue(fx, operand.unchecked_cast_to(layout)); match fx.monomorphize(&operand.ty(&fx.mir.local_decls, fx.tcx)).sty {
ty::FnDef(def_id, substs) => {
let func_ref = fx.get_function_ref(
Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs).unwrap(),
);
let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
lval.write_cvalue(fx, CValue::ByVal(func_addr, layout));
}
_ => bug!("Trying to ReifyFnPointer on non FnDef {:?}", ty),
}
} }
Rvalue::Cast(CastKind::UnsafeFnPointer, operand, ty) => { Rvalue::Cast(CastKind::UnsafeFnPointer, operand, ty) => {
let operand = trans_operand(fx, operand); let operand = trans_operand(fx, operand);

View file

@ -110,12 +110,11 @@ fn trans_const_value<'a, 'tcx: 'a>(
let bits = const_.val.try_to_bits(layout.size).unwrap(); let bits = const_.val.try_to_bits(layout.size).unwrap();
CValue::const_val(fx, ty, bits as i128 as i64) CValue::const_val(fx, ty, bits as i128 as i64)
} }
ty::FnDef(def_id, substs) => { ty::FnDef(_def_id, _substs) => {
let func_ref = fx.get_function_ref( CValue::ByRef(
Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs).unwrap(), fx.bcx.ins().iconst(fx.pointer_type, 0),
); layout
let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref); )
CValue::ByVal(func_addr, layout)
} }
_ => trans_const_place(fx, const_).to_cvalue(fx), _ => trans_const_place(fx, const_).to_cvalue(fx),
} }