Skip to content

Commit d1db5e4

Browse files
Rollup merge of rust-lang#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
2 parents b8713e5 + 92c28bf commit d1db5e4

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/librustc_mir/interpret/cast.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc::ty::{self, Ty, TypeAndMut};
22
use rustc::ty::layout::{self, TyLayout, Size};
33
use rustc::ty::adjustment::{PointerCast};
4-
use syntax::ast::{FloatTy, IntTy, UintTy};
4+
use syntax::ast::FloatTy;
55
use syntax::symbol::sym;
66

77
use rustc_apfloat::ieee::{Single, Double};
@@ -151,7 +151,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
151151
"Unexpected cast from type {:?}", src_layout.ty
152152
);
153153
match val.to_bits_or_ptr(src_layout.size, self) {
154-
Err(ptr) => self.cast_from_ptr(ptr, dest_layout.ty),
154+
Err(ptr) => self.cast_from_ptr(ptr, src_layout, dest_layout),
155155
Ok(data) => self.cast_from_int(data, src_layout, dest_layout),
156156
}
157157
}
@@ -239,17 +239,25 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
239239
fn cast_from_ptr(
240240
&self,
241241
ptr: Pointer<M::PointerTag>,
242-
ty: Ty<'tcx>
242+
src_layout: TyLayout<'tcx>,
243+
dest_layout: TyLayout<'tcx>,
243244
) -> InterpResult<'tcx, Scalar<M::PointerTag>> {
244245
use rustc::ty::TyKind::*;
245-
match ty.sty {
246+
247+
match dest_layout.ty.sty {
246248
// Casting to a reference or fn pointer is not permitted by rustc,
247249
// no need to support it here.
248-
RawPtr(_) |
249-
Int(IntTy::Isize) |
250-
Uint(UintTy::Usize) => Ok(ptr.into()),
251-
Int(_) | Uint(_) => err!(ReadPointerAsBytes),
252-
_ => err!(Unimplemented(format!("ptr to {:?} cast", ty))),
250+
RawPtr(_) => Ok(ptr.into()),
251+
Int(_) | Uint(_) => {
252+
let size = self.memory.pointer_size();
253+
254+
match self.force_bits(Scalar::Ptr(ptr), size) {
255+
Ok(bits) => self.cast_from_int(bits, src_layout, dest_layout),
256+
Err(_) if dest_layout.size == size => Ok(ptr.into()),
257+
Err(e) => Err(e),
258+
}
259+
}
260+
_ => bug!("invalid MIR: ptr to {:?} cast", dest_layout.ty)
253261
}
254262
}
255263

0 commit comments

Comments
 (0)