Skip to content

Commit

Permalink
make the eval() functions on our const types return the resulting value
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Sep 13, 2023
1 parent c41e779 commit 8e6f68b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 31 deletions.
7 changes: 2 additions & 5 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,11 +723,8 @@ fn codegen_stmt<'tcx>(
}
Rvalue::Repeat(ref operand, times) => {
let operand = codegen_operand(fx, operand);
let times = fx
.monomorphize(times)
.eval(fx.tcx, ParamEnv::reveal_all())
.try_to_bits(fx.tcx.data_layout.pointer_size)
.unwrap();
let times =
fx.monomorphize(times).eval_target_usize(fx.tcx, ParamEnv::reveal_all());
if operand.layout().size.bytes() == 0 {
// Do nothing for ZST's
} else if fx.clif_type(operand.layout().ty) == Some(types::I8) {
Expand Down
30 changes: 4 additions & 26 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,9 @@ pub(crate) fn eval_mir_constant<'tcx>(
fx: &FunctionCx<'_, '_, 'tcx>,
constant: &Constant<'tcx>,
) -> Option<(ConstValue<'tcx>, Ty<'tcx>)> {
let constant_kind = fx.monomorphize(constant.literal);
let uv = match constant_kind {
ConstantKind::Ty(const_) => match const_.kind() {
ty::ConstKind::Unevaluated(uv) => uv.expand(),
ty::ConstKind::Value(val) => {
return Some((fx.tcx.valtree_to_const_val((const_.ty(), val)), const_.ty()));
}
err => span_bug!(
constant.span,
"encountered bad ConstKind after monomorphizing: {:?}",
err
),
},
ConstantKind::Unevaluated(mir::UnevaluatedConst { def, .. }, _)
if fx.tcx.is_static(def) =>
{
span_bug!(constant.span, "MIR constant refers to static");
}
ConstantKind::Unevaluated(uv, _) => uv,
ConstantKind::Val(val, _) => return Some((val, constant_kind.ty())),
};

let val = fx
.tcx
.const_eval_resolve(ty::ParamEnv::reveal_all(), uv, None)
let cv = fx.monomorphize(constant.literal);
let val = cv
.eval(fx.tcx, ty::ParamEnv::reveal_all(), Some(constant.span))
.map_err(|err| match err {
ErrorHandled::Reported(_) => {
fx.tcx.sess.span_err(constant.span, "erroneous constant encountered");
Expand All @@ -111,7 +89,7 @@ pub(crate) fn eval_mir_constant<'tcx>(
}
})
.ok();
val.map(|val| (val, constant_kind.ty()))
val.map(|val| (val, cv.ty()))
}

pub(crate) fn codegen_constant_operand<'tcx>(
Expand Down

0 comments on commit 8e6f68b

Please sign in to comment.