Skip to content

Commit

Permalink
Expose miri <-> ctfe differences
Browse files Browse the repository at this point in the history
miri needs to start storing everything in the TyCtxt so we can have relocations and aggregates
  • Loading branch information
oli-obk committed Sep 29, 2017
1 parent ea35192 commit d4c442d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
7 changes: 4 additions & 3 deletions src/librustc/mir/interpret/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use rustc_const_math::ConstInt;
use std::fmt;
use std::error::Error;

pub fn eval_body_as_primval<'a, 'tcx>(
pub fn eval_body<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
instance: Instance<'tcx>,
) -> EvalResult<'tcx, (PrimVal, Ty<'tcx>)> {
) -> EvalResult<'tcx, (Value, Ty<'tcx>)> {
let limits = super::ResourceLimits::default();
let mut ecx = EvalContext::<CompileTimeFunctionEvaluator>::new(tcx, limits, (), ());
let cid = GlobalId {
Expand Down Expand Up @@ -73,7 +73,8 @@ pub fn eval_body_as_primval<'a, 'tcx>(
value,
ty: mir.return_ty,
};
Ok((ecx.value_to_primval(valty)?, mir.return_ty))
// FIXME: store cached value in TyCtxt
Ok(value, mir.return_ty))
}

pub fn eval_body_as_integer<'a, 'tcx>(
Expand Down
6 changes: 5 additions & 1 deletion src/librustc/mir/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1895,7 +1895,11 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
}

pub(super) fn substs(&self) -> &'tcx Substs<'tcx> {
self.frame().instance.substs
if let Some(frame) = self.stack.last() {
frame.instance.substs
} else {
Substs::empty()
}
}

fn unsize_into_ptr(
Expand Down
34 changes: 17 additions & 17 deletions src/librustc_const_eval/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,68 +793,68 @@ fn const_eval<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
};

let instance = ty::Instance::new(def_id, substs);
let miri_result = ::rustc::interpret::eval_body_as_primval(tcx, instance);
let miri_result = ::rustc::mir::interpret::eval_body_as_primval(tcx, instance);
let old_result = ConstContext::new(tcx, key.param_env.and(substs), tables).eval(&body.value);
match (miri_result, old_result) {
(Err(err), Ok(ok)) => {
warn!("miri fails to eval {:?} to {:?} with error {:?}", key, ok, err);
Ok(ok)
panic!("miri fails to eval {:?} to {:?} with error {:?}", key, ok, err);
//Ok(ok)
},
(Ok(ok), Err(err)) => {
info!("miri can eval {:?} to {:?}, while old ctfe fails with {:?}", key, ok, err);
Err(err)
panic!("miri can eval {:?} to {:?}, while old ctfe fails with {:?}", key, ok, err);
//Err(err)
},
(Err(_), Err(err)) => Err(err),
(Ok((miri_val, miri_ty)), Ok(ctfe)) => {
use rustc::ty::TypeVariants::*;
use rustc::interpret::PrimVal;
use rustc::mir::interpret::PrimVal;
match (miri_val, &miri_ty.sty, ctfe.val) {
(PrimVal::Undef, _, _) => {
warn!("miri produced an undef, while old ctfe produced {:?}", ctfe);
panic!("miri produced an undef, while old ctfe produced {:?}", ctfe);
},
(PrimVal::Ptr(_), _, _) => {
warn!("miri produced a pointer, which isn't implemented yet");
panic!("miri produced a pointer, which isn't implemented yet");
},
(PrimVal::Bytes(b), &TyInt(int_ty), ConstVal::Integral(ci)) => {
let c = ConstInt::new_signed_truncating(b as i128,
int_ty,
tcx.sess.target.isize_ty);
if c != ci {
warn!("miri evaluated to {}, but ctfe yielded {}", b as i128, ci);
panic!("miri evaluated to {}, but ctfe yielded {}", b as i128, ci);
}
}
(PrimVal::Bytes(b), &TyUint(int_ty), ConstVal::Integral(ci)) => {
let c = ConstInt::new_unsigned_truncating(b, int_ty, tcx.sess.target.usize_ty);
if c != ci {
warn!("miri evaluated to {}, but ctfe yielded {}", b, ci);
panic!("miri evaluated to {}, but ctfe yielded {}", b, ci);
}
}
(PrimVal::Bytes(bits), &TyFloat(ty), ConstVal::Float(cf)) => {
let f = ConstFloat { bits, ty };
if f != cf {
warn!("miri evaluated to {}, but ctfe yielded {}", f, cf);
panic!("miri evaluated to {}, but ctfe yielded {}", f, cf);
}
}
(PrimVal::Bytes(bits), &TyBool, ConstVal::Bool(b)) => {
if bits == 0 && b {
warn!("miri evaluated to {}, but ctfe yielded {}", bits == 0, b);
panic!("miri evaluated to {}, but ctfe yielded {}", bits == 0, b);
} else if bits == 1 && !b {
warn!("miri evaluated to {}, but ctfe yielded {}", bits == 1, b);
panic!("miri evaluated to {}, but ctfe yielded {}", bits == 1, b);
} else {
warn!("miri evaluated to {}, but expected a bool {}", bits, b);
panic!("miri evaluated to {}, but expected a bool {}", bits, b);
}
}
(PrimVal::Bytes(bits), &TyChar, ConstVal::Char(c)) => {
if let Some(cm) = ::std::char::from_u32(bits as u32) {
if cm != c {
warn!("miri evaluated to {:?}, but expected {:?}", cm, c);
panic!("miri evaluated to {:?}, but expected {:?}", cm, c);
}
} else {
warn!("miri evaluated to {}, but expected a char {:?}", bits, c);
panic!("miri evaluated to {}, but expected a char {:?}", bits, c);
}
}
_ => {
info!("can't check whether miri's {:?} ({}) makes sense when ctfe yields {:?}",
panic!("can't check whether miri's {:?} ({}) makes sense when ctfe yields {:?}",
miri_val,
miri_ty,
ctfe)
Expand Down

0 comments on commit d4c442d

Please sign in to comment.