Skip to content

Commit ba82f54

Browse files
committed
use RawConst in miri
1 parent b50c1b2 commit ba82f54

File tree

5 files changed

+32
-40
lines changed

5 files changed

+32
-40
lines changed

src/librustc_mir/const_eval.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ pub(crate) fn eval_promoted<'a, 'mir, 'tcx>(
9999
eval_body_using_ecx(&mut ecx, cid, Some(mir), param_env)
100100
}
101101

102-
// FIXME: This thing is a bad hack. We should get rid of it. Ideally constants are always
103-
// in an allocation.
102+
// FIXME: These two conversion functions are bad hacks. We should just always use allocations.
104103
pub fn op_to_const<'tcx>(
105104
ecx: &CompileTimeEvalContext<'_, '_, 'tcx>,
106105
op: OpTy<'tcx>,
@@ -146,6 +145,13 @@ pub fn op_to_const<'tcx>(
146145
};
147146
Ok(ty::Const::from_const_value(ecx.tcx.tcx, val, op.layout.ty))
148147
}
148+
pub fn const_to_op<'tcx>(
149+
ecx: &CompileTimeEvalContext<'_, '_, 'tcx>,
150+
cnst: &ty::Const<'tcx>,
151+
) -> EvalResult<'tcx, OpTy<'tcx>> {
152+
let op = ecx.const_value_to_op(cnst.val)?;
153+
Ok(OpTy { op, layout: ecx.layout_of(cnst.ty)? })
154+
}
149155

150156
fn eval_body_and_ecx<'a, 'mir, 'tcx>(
151157
tcx: TyCtxt<'a, 'tcx, 'tcx>,
@@ -496,7 +502,7 @@ pub fn const_field<'a, 'tcx>(
496502
let ecx = mk_eval_cx(tcx, instance, param_env).unwrap();
497503
let result = (|| {
498504
// get the operand again
499-
let op = ecx.const_to_op(value)?;
505+
let op = const_to_op(&ecx, value)?;
500506
// downcast
501507
let down = match variant {
502508
None => op,
@@ -523,7 +529,7 @@ pub fn const_variant_index<'a, 'tcx>(
523529
) -> EvalResult<'tcx, VariantIdx> {
524530
trace!("const_variant_index: {:?}, {:?}", instance, val);
525531
let ecx = mk_eval_cx(tcx, instance, param_env).unwrap();
526-
let op = ecx.const_to_op(val)?;
532+
let op = const_to_op(&ecx, val)?;
527533
Ok(ecx.read_discriminant(op)?.1)
528534
}
529535

src/librustc_mir/interpret/eval_context.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -588,18 +588,22 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tc
588588
Ok(())
589589
}
590590

591-
pub fn const_eval(&self, gid: GlobalId<'tcx>) -> EvalResult<'tcx, &'tcx ty::Const<'tcx>> {
591+
pub fn const_eval_raw(
592+
&self,
593+
gid: GlobalId<'tcx>,
594+
) -> EvalResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
592595
let param_env = if self.tcx.is_static(gid.instance.def_id()).is_some() {
593596
ty::ParamEnv::reveal_all()
594597
} else {
595598
self.param_env
596599
};
597-
self.tcx.const_eval(param_env.and(gid)).map_err(|err| {
600+
let val = self.tcx.const_eval_raw(param_env.and(gid)).map_err(|err| {
598601
match err {
599-
ErrorHandled::Reported => EvalErrorKind::ReferencedConstant.into(),
600-
ErrorHandled::TooGeneric => EvalErrorKind::TooGeneric.into(),
602+
ErrorHandled::Reported => EvalErrorKind::ReferencedConstant,
603+
ErrorHandled::TooGeneric => EvalErrorKind::TooGeneric,
601604
}
602-
})
605+
})?;
606+
self.raw_const_to_mplace(val)
603607
}
604608

605609
pub fn dump_place(&self, place: Place<M::PointerTag>) {

src/librustc_mir/interpret/operand.rs

+7-21
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
use std::convert::TryInto;
1515

16-
use rustc::{mir, ty};
16+
use rustc::mir;
1717
use rustc::ty::layout::{self, Size, LayoutOf, TyLayout, HasDataLayout, IntegerExt, VariantIdx};
1818

1919
use rustc::mir::interpret::{
@@ -535,20 +535,21 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
535535
.collect()
536536
}
537537

538-
// Also used e.g. when miri runs into a constant.
539-
// FIXME: Can we avoid converting with ConstValue and Const? We should be using RawConst.
540-
fn const_value_to_op(
538+
// Used when miri runs into a constant, and by CTFE.
539+
// FIXME: CTFE should use allocations, then we can make this private (embed it into
540+
// `eval_operand`, ideally).
541+
pub(crate) fn const_value_to_op(
541542
&self,
542543
val: ConstValue<'tcx>,
543544
) -> EvalResult<'tcx, Operand<M::PointerTag>> {
544545
trace!("const_value_to_op: {:?}", val);
545546
match val {
546547
ConstValue::Unevaluated(def_id, substs) => {
547548
let instance = self.resolve(def_id, substs)?;
548-
self.global_to_op(GlobalId {
549+
Ok(*OpTy::from(self.const_eval_raw(GlobalId {
549550
instance,
550551
promoted: None,
551-
})
552+
})?))
552553
}
553554
ConstValue::ByRef(id, alloc, offset) => {
554555
// We rely on mutability being set correctly in that allocation to prevent writes
@@ -566,21 +567,6 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
566567
Ok(Operand::Immediate(Immediate::Scalar(x.into())).with_default_tag()),
567568
}
568569
}
569-
pub fn const_to_op(
570-
&self,
571-
cnst: &ty::Const<'tcx>,
572-
) -> EvalResult<'tcx, OpTy<'tcx, M::PointerTag>> {
573-
let op = self.const_value_to_op(cnst.val)?;
574-
Ok(OpTy { op, layout: self.layout_of(cnst.ty)? })
575-
}
576-
577-
pub(super) fn global_to_op(
578-
&self,
579-
gid: GlobalId<'tcx>
580-
) -> EvalResult<'tcx, Operand<M::PointerTag>> {
581-
let cv = self.const_eval(gid)?;
582-
self.const_value_to_op(cv.val)
583-
}
584570

585571
/// Read discriminant, return the runtime value as well as the variant index.
586572
pub fn read_discriminant(

src/librustc_mir/interpret/place.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -553,16 +553,10 @@ where
553553
Ok(match *mir_place {
554554
Promoted(ref promoted) => {
555555
let instance = self.frame().instance;
556-
let op = self.global_to_op(GlobalId {
556+
self.const_eval_raw(GlobalId {
557557
instance,
558558
promoted: Some(promoted.0),
559-
})?;
560-
let mplace = op.to_mem_place(); // these are always in memory
561-
let ty = self.monomorphize(promoted.1, self.substs());
562-
MPlaceTy {
563-
mplace,
564-
layout: self.layout_of(ty)?,
565-
}
559+
})?
566560
}
567561

568562
Static(ref static_) => {

src/librustc_mir/transform/const_prop.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ use rustc::ty::layout::{
2929
};
3030

3131
use interpret::{self, EvalContext, ScalarMaybeUndef, Immediate, OpTy, MemoryKind};
32-
use const_eval::{CompileTimeInterpreter, error_to_const_error, eval_promoted, mk_borrowck_eval_cx};
32+
use const_eval::{
33+
CompileTimeInterpreter, const_to_op, error_to_const_error, eval_promoted, mk_borrowck_eval_cx
34+
};
3335
use transform::{MirPass, MirSource};
3436

3537
pub struct ConstProp;
@@ -262,7 +264,7 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
262264
source_info: SourceInfo,
263265
) -> Option<Const<'tcx>> {
264266
self.ecx.tcx.span = source_info.span;
265-
match self.ecx.const_to_op(c.literal) {
267+
match const_to_op(&self.ecx, c.literal) {
266268
Ok(op) => {
267269
Some((op, c.span))
268270
},

0 commit comments

Comments
 (0)