Skip to content

Commit

Permalink
Use pointer size as the source size
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdrz committed Jun 29, 2019
1 parent 7f1e160 commit 95bc720
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions src/librustc_mir/interpret/cast.rs
Expand Up @@ -244,37 +244,24 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
) -> InterpResult<'tcx, Scalar<M::PointerTag>> {
use rustc::ty::TyKind::*;

fn int_size<'tcx>(layout: TyLayout<'tcx>) -> Option<usize> {
match layout.ty.sty {
Int(i) => i.bit_width(),
Uint(i) => i.bit_width(),
_ => bug!("Not an integer"),
}
}

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

if let Ok(bits) = self.force_bits(Scalar::Ptr(ptr), size) {
if let Ok(bits) = self.force_bits(Scalar::Ptr(ptr), self.memory.pointer_size()) {
self.cast_from_int(bits, src_layout, dest_layout)
} else {
Ok(ptr.into())
}
}
Int(_) | Uint(_) => {
let size = Size::from_bits(int_size(dest_layout).unwrap() as u64);

if let Ok(bits) = self.force_bits(Scalar::Ptr(ptr), size) {
if let Ok(bits) = self.force_bits(Scalar::Ptr(ptr), self.memory.pointer_size()) {
self.cast_from_int(bits, src_layout, dest_layout)
} else {
err!(ReadPointerAsBytes)
}
},
// Casting to any other type is not implemented
}
_ => return err!(Unimplemented(format!("ptr to {:?} cast", dest_layout.ty))),
}
}
Expand Down

0 comments on commit 95bc720

Please sign in to comment.