Rollup merge of #62229 - christianpoveda:intptrcast-explicit-casts, r=RalfJung

Enable intptrcast for explicit casts

I checked locally that this does not break miri on master. r? @RalfJung
This commit is contained in:
Mark Rousskov
2019-07-03 09:59:22 -04:00
committed by GitHub

View File

@@ -1,7 +1,7 @@
use rustc::ty::{self, Ty, TypeAndMut}; use rustc::ty::{self, Ty, TypeAndMut};
use rustc::ty::layout::{self, TyLayout, Size}; use rustc::ty::layout::{self, TyLayout, Size};
use rustc::ty::adjustment::{PointerCast}; use rustc::ty::adjustment::{PointerCast};
use syntax::ast::{FloatTy, IntTy, UintTy}; use syntax::ast::FloatTy;
use syntax::symbol::sym; use syntax::symbol::sym;
use rustc_apfloat::ieee::{Single, Double}; use rustc_apfloat::ieee::{Single, Double};
@@ -151,7 +151,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
"Unexpected cast from type {:?}", src_layout.ty "Unexpected cast from type {:?}", src_layout.ty
); );
match val.to_bits_or_ptr(src_layout.size, self) { match val.to_bits_or_ptr(src_layout.size, self) {
Err(ptr) => self.cast_from_ptr(ptr, dest_layout.ty), Err(ptr) => self.cast_from_ptr(ptr, src_layout, dest_layout),
Ok(data) => self.cast_from_int(data, src_layout, dest_layout), Ok(data) => self.cast_from_int(data, src_layout, dest_layout),
} }
} }
@@ -239,17 +239,25 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
fn cast_from_ptr( fn cast_from_ptr(
&self, &self,
ptr: Pointer<M::PointerTag>, ptr: Pointer<M::PointerTag>,
ty: Ty<'tcx> src_layout: TyLayout<'tcx>,
dest_layout: TyLayout<'tcx>,
) -> InterpResult<'tcx, Scalar<M::PointerTag>> { ) -> InterpResult<'tcx, Scalar<M::PointerTag>> {
use rustc::ty::TyKind::*; use rustc::ty::TyKind::*;
match ty.sty {
match dest_layout.ty.sty {
// Casting to a reference or fn pointer is not permitted by rustc, // Casting to a reference or fn pointer is not permitted by rustc,
// no need to support it here. // no need to support it here.
RawPtr(_) | RawPtr(_) => Ok(ptr.into()),
Int(IntTy::Isize) | Int(_) | Uint(_) => {
Uint(UintTy::Usize) => Ok(ptr.into()), let size = self.memory.pointer_size();
Int(_) | Uint(_) => err!(ReadPointerAsBytes),
_ => err!(Unimplemented(format!("ptr to {:?} cast", ty))), match self.force_bits(Scalar::Ptr(ptr), size) {
Ok(bits) => self.cast_from_int(bits, src_layout, dest_layout),
Err(_) if dest_layout.size == size => Ok(ptr.into()),
Err(e) => Err(e),
}
}
_ => bug!("invalid MIR: ptr to {:?} cast", dest_layout.ty)
} }
} }