Skip to content

Commit

Permalink
Rollup merge of rust-lang#62229 - christianpoveda:intptrcast-explicit…
Browse files Browse the repository at this point in the history
…-casts, r=RalfJung

Enable intptrcast for explicit casts

I checked locally that this does not break miri on master. r? @RalfJung
  • Loading branch information
Mark-Simulacrum authored Jul 3, 2019
2 parents 28ea0ae + 92c28bf commit e9a218c
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/librustc_mir/interpret/cast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc::ty::{self, Ty, TypeAndMut};
use rustc::ty::layout::{self, TyLayout, Size};
use rustc::ty::adjustment::{PointerCast};
use syntax::ast::{FloatTy, IntTy, UintTy};
use syntax::ast::FloatTy;
use syntax::symbol::sym;

use rustc_apfloat::ieee::{Single, Double};
Expand Down Expand Up @@ -151,7 +151,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
"Unexpected cast from type {:?}", src_layout.ty
);
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),
}
}
Expand Down Expand Up @@ -239,17 +239,25 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
fn cast_from_ptr(
&self,
ptr: Pointer<M::PointerTag>,
ty: Ty<'tcx>
src_layout: TyLayout<'tcx>,
dest_layout: TyLayout<'tcx>,
) -> InterpResult<'tcx, Scalar<M::PointerTag>> {
use rustc::ty::TyKind::*;
match ty.sty {

match dest_layout.ty.sty {
// Casting to a reference or fn pointer is not permitted by rustc,
// no need to support it here.
RawPtr(_) |
Int(IntTy::Isize) |
Uint(UintTy::Usize) => Ok(ptr.into()),
Int(_) | Uint(_) => err!(ReadPointerAsBytes),
_ => err!(Unimplemented(format!("ptr to {:?} cast", ty))),
RawPtr(_) => Ok(ptr.into()),
Int(_) | Uint(_) => {
let size = self.memory.pointer_size();

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)
}
}

Expand Down

0 comments on commit e9a218c

Please sign in to comment.