Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable intptrcast for explicit casts #62229

Merged
merged 5 commits into from
Jul 3, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/librustc_mir/interpret/cast.rs
Original file line number Diff line number Diff line change
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,30 @@ 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(IntTy::Isize) | Uint(UintTy::Usize) => {
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(_) => {
if let Ok(bits) = self.force_bits(Scalar::Ptr(ptr), self.memory.pointer_size()) {
self.cast_from_int(bits, src_layout, dest_layout)
RalfJung marked this conversation as resolved.
Show resolved Hide resolved
} else {
err!(ReadPointerAsBytes)
}
}
_ => return err!(Unimplemented(format!("ptr to {:?} cast", dest_layout.ty))),
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down