From c47ee6b52154524619bb965ab6d47fdf2ee976b7 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Wed, 13 Sep 2017 13:46:54 +0200 Subject: [PATCH] Rustup part 2/2 --- src/librustc_mir/interpret/cast.rs | 51 +++++++++++++--------- src/librustc_mir/interpret/eval_context.rs | 27 ++++++------ src/librustc_mir/interpret/step.rs | 5 ++- src/librustc_mir/interpret/validation.rs | 2 +- 4 files changed, 48 insertions(+), 37 deletions(-) diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index f9a771c7af7..5ae7c9da31c 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -38,36 +38,45 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> { self.cast_from_int(val as u128, ty, val < 0) } + fn int_to_int(&self, v: i128, ty: IntTy) -> u128 { + match ty { + IntTy::I8 => v as i8 as u128, + IntTy::I16 => v as i16 as u128, + IntTy::I32 => v as i32 as u128, + IntTy::I64 => v as i64 as u128, + IntTy::I128 => v as u128, + IntTy::Is => { + let ty = self.tcx.sess.target.isize_ty; + self.int_to_int(v, ty) + } + } + } + fn int_to_uint(&self, v: u128, ty: UintTy) -> u128 { + match ty { + UintTy::U8 => v as u8 as u128, + UintTy::U16 => v as u16 as u128, + UintTy::U32 => v as u32 as u128, + UintTy::U64 => v as u64 as u128, + UintTy::U128 => v, + UintTy::Us => { + let ty = self.tcx.sess.target.usize_ty; + self.int_to_uint(v, ty) + } + } + } + fn cast_from_int( &self, v: u128, ty: ty::Ty<'tcx>, negative: bool, ) -> EvalResult<'tcx, PrimVal> { + trace!("cast_from_int: {}, {}, {}", v, ty, negative); use rustc::ty::TypeVariants::*; match ty.sty { // Casts to bool are not permitted by rustc, no need to handle them here. - TyInt(IntTy::I8) => Ok(PrimVal::Bytes(v as i128 as i8 as u128)), - TyInt(IntTy::I16) => Ok(PrimVal::Bytes(v as i128 as i16 as u128)), - TyInt(IntTy::I32) => Ok(PrimVal::Bytes(v as i128 as i32 as u128)), - TyInt(IntTy::I64) => Ok(PrimVal::Bytes(v as i128 as i64 as u128)), - TyInt(IntTy::I128) => Ok(PrimVal::Bytes(v as u128)), - - TyUint(UintTy::U8) => Ok(PrimVal::Bytes(v as u8 as u128)), - TyUint(UintTy::U16) => Ok(PrimVal::Bytes(v as u16 as u128)), - TyUint(UintTy::U32) => Ok(PrimVal::Bytes(v as u32 as u128)), - TyUint(UintTy::U64) => Ok(PrimVal::Bytes(v as u64 as u128)), - TyUint(UintTy::U128) => Ok(PrimVal::Bytes(v)), - - TyInt(IntTy::Is) => { - let ty = self.tcx.types.isize; - self.cast_from_int(v, ty, negative) - } - - TyUint(UintTy::Us) => { - let ty = self.tcx.types.usize; - self.cast_from_int(v, ty, negative) - } + TyInt(ty) => Ok(PrimVal::Bytes(self.int_to_int(v as i128, ty))), + TyUint(ty) => Ok(PrimVal::Bytes(self.int_to_uint(v, ty))), TyFloat(FloatTy::F64) if negative => Ok(PrimVal::from_f64(v as i128 as f64)), TyFloat(FloatTy::F64) => Ok(PrimVal::from_f64(v as f64)), diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index 231bfa92ccd..0a23e056931 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -240,10 +240,20 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> { Str(ref s) => return self.str_to_value(s), ByteStr(ref bs) => { - let ptr = self.memory.allocate_cached(bs)?; + let ptr = self.memory.allocate_cached(bs.data)?; PrimVal::Ptr(ptr) } + Unevaluated(def_id, substs) => { + let instance = self.resolve_associated_const(def_id, substs); + let cid = GlobalId { + instance, + promoted: None, + }; + return Ok(Value::ByRef(*self.globals.get(&cid).expect("static/const not cached"))); + } + + Aggregate(..) | Variant(_) => unimplemented!(), // function items are zero sized and thus have no readable value Function(..) => PrimVal::Undef, @@ -1284,16 +1294,7 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> { use rustc::mir::Literal; let mir::Constant { ref literal, .. } = **constant; let value = match *literal { - Literal::Value { ref value } => self.const_to_value(value)?, - - Literal::Item { def_id, substs } => { - let instance = self.resolve_associated_const(def_id, substs); - let cid = GlobalId { - instance, - promoted: None, - }; - Value::ByRef(*self.globals.get(&cid).expect("static/const not cached")) - } + Literal::Value { ref value } => self.const_to_value(&value.val)?, Literal::Promoted { index } => { let cid = GlobalId { @@ -2501,7 +2502,7 @@ struct AssociatedTypeNormalizer<'a, 'tcx: 'a> { impl<'a, 'tcx> AssociatedTypeNormalizer<'a, 'tcx> { fn fold>(&mut self, value: &T) -> T { - if !value.has_projection_types() { + if !value.has_projections() { value.clone() } else { value.fold_with(self) @@ -2515,7 +2516,7 @@ impl<'a, 'tcx> ::rustc::ty::fold::TypeFolder<'tcx, 'tcx> for AssociatedTypeNorma } fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { - if !ty.has_projection_types() { + if !ty.has_projections() { ty } else { self.tcx.normalize_associated_type(&ty) diff --git a/src/librustc_mir/interpret/step.rs b/src/librustc_mir/interpret/step.rs index 3dc74368fe8..05f1bd10e87 100644 --- a/src/librustc_mir/interpret/step.rs +++ b/src/librustc_mir/interpret/step.rs @@ -10,6 +10,7 @@ use rustc::traits::Reveal; use rustc::ty; use rustc::ty::layout::Layout; use rustc::ty::subst::Substs; +use rustc::middle::const_val::ConstVal; use super::{EvalResult, EvalContext, StackPopCleanup, PtrAndAlign, GlobalId, Lvalue, MemoryKind, Machine, PrimVal}; @@ -300,8 +301,7 @@ impl<'a, 'b, 'tcx, M: Machine<'tcx>> Visitor<'tcx> for ConstantExtractor<'a, 'b, self.super_constant(constant, location); match constant.literal { // already computed by rustc - mir::Literal::Value { .. } => {} - mir::Literal::Item { def_id, substs } => { + mir::Literal::Value { value: &ty::Const { val: ConstVal::Unevaluated(def_id, substs), .. } } => { self.try(|this| { this.ecx.global_item( def_id, @@ -311,6 +311,7 @@ impl<'a, 'b, 'tcx, M: Machine<'tcx>> Visitor<'tcx> for ConstantExtractor<'a, 'b, ) }); } + mir::Literal::Value { .. } => {} mir::Literal::Promoted { index } => { let cid = GlobalId { instance: self.instance, diff --git a/src/librustc_mir/interpret/validation.rs b/src/librustc_mir/interpret/validation.rs index 1f9de6785fd..2477001bec4 100644 --- a/src/librustc_mir/interpret/validation.rs +++ b/src/librustc_mir/interpret/validation.rs @@ -246,7 +246,7 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> { { let param_env = ty::ParamEnv::empty(Reveal::All); - if !value.has_projection_types() { + if !value.has_projections() { return value.clone(); }