Skip to content

Commit 5ed4377

Browse files
committed
Fix ConstantKind::Ty codegen
1 parent b3d3ba9 commit 5ed4377

File tree

2 files changed

+34
-42
lines changed

2 files changed

+34
-42
lines changed

src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ pub(crate) fn codegen_operand<'tcx>(
917917
let cplace = codegen_place(fx, *place);
918918
cplace.to_cvalue(fx)
919919
}
920-
Operand::Constant(const_) => crate::constant::codegen_constant(fx, const_),
920+
Operand::Constant(const_) => crate::constant::codegen_constant_operand(fx, const_),
921921
}
922922
}
923923

src/constant.rs

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_middle::mir::interpret::{
77
};
88
use rustc_span::DUMMY_SP;
99

10-
use cranelift_codegen::ir::GlobalValueData;
1110
use cranelift_module::*;
1211

1312
use crate::prelude::*;
@@ -81,53 +80,46 @@ pub(crate) fn codegen_tls_ref<'tcx>(
8180
CValue::by_val(tls_ptr, layout)
8281
}
8382

84-
fn codegen_static_ref<'tcx>(
85-
fx: &mut FunctionCx<'_, '_, 'tcx>,
86-
def_id: DefId,
87-
layout: TyAndLayout<'tcx>,
88-
) -> CPlace<'tcx> {
89-
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
90-
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
91-
if fx.clif_comments.enabled() {
92-
fx.add_comment(local_data_id, format!("{:?}", def_id));
93-
}
94-
let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
95-
assert!(!layout.is_unsized(), "unsized statics aren't supported");
96-
assert!(
97-
matches!(
98-
fx.bcx.func.global_values[local_data_id],
99-
GlobalValueData::Symbol { tls: false, .. }
100-
),
101-
"tls static referenced without Rvalue::ThreadLocalRef"
102-
);
103-
CPlace::for_ptr(crate::pointer::Pointer::new(global_ptr), layout)
104-
}
105-
106-
pub(crate) fn codegen_constant<'tcx>(
83+
pub(crate) fn eval_mir_constant<'tcx>(
10784
fx: &mut FunctionCx<'_, '_, 'tcx>,
10885
constant: &Constant<'tcx>,
109-
) -> CValue<'tcx> {
110-
let (const_val, ty) = match fx.monomorphize(constant.literal) {
111-
ConstantKind::Ty(const_) => unreachable!("{:?}", const_),
112-
ConstantKind::Unevaluated(mir::UnevaluatedConst { def, substs, promoted }, ty)
86+
) -> (ConstValue<'tcx>, Ty<'tcx>) {
87+
let constant_kind = fx.monomorphize(constant.literal);
88+
let uv = match constant_kind {
89+
ConstantKind::Ty(const_) => match const_.kind() {
90+
ty::ConstKind::Unevaluated(uv) => uv.expand(),
91+
ty::ConstKind::Value(val) => {
92+
return (fx.tcx.valtree_to_const_val((const_.ty(), val)), const_.ty());
93+
}
94+
err => span_bug!(
95+
constant.span,
96+
"encountered bad ConstKind after monomorphizing: {:?}",
97+
err
98+
),
99+
},
100+
ConstantKind::Unevaluated(mir::UnevaluatedConst { def, .. }, _)
113101
if fx.tcx.is_static(def.did) =>
114102
{
115-
assert!(substs.is_empty());
116-
assert!(promoted.is_none());
117-
118-
return codegen_static_ref(fx, def.did, fx.layout_of(ty)).to_cvalue(fx);
119-
}
120-
ConstantKind::Unevaluated(unevaluated, ty) => {
121-
match fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None) {
122-
Ok(const_val) => (const_val, ty),
123-
Err(_) => {
124-
span_bug!(constant.span, "erroneous constant not captured by required_consts");
125-
}
126-
}
103+
span_bug!(constant.span, "MIR constant refers to static");
127104
}
128-
ConstantKind::Val(val, ty) => (val, ty),
105+
ConstantKind::Unevaluated(uv, _) => uv,
106+
ConstantKind::Val(val, _) => return (val, constant_kind.ty()),
129107
};
130108

109+
(
110+
fx.tcx.const_eval_resolve(ty::ParamEnv::reveal_all(), uv, None).unwrap_or_else(|_err| {
111+
span_bug!(constant.span, "erroneous constant not captured by required_consts");
112+
}),
113+
constant_kind.ty(),
114+
)
115+
}
116+
117+
pub(crate) fn codegen_constant_operand<'tcx>(
118+
fx: &mut FunctionCx<'_, '_, 'tcx>,
119+
constant: &Constant<'tcx>,
120+
) -> CValue<'tcx> {
121+
let (const_val, ty) = eval_mir_constant(fx, constant);
122+
131123
codegen_const_value(fx, const_val, ty)
132124
}
133125

0 commit comments

Comments
 (0)