Skip to content

Commit 95bc720

Browse files
committed
Use pointer size as the source size
1 parent 7f1e160 commit 95bc720

File tree

1 file changed

+3
-16
lines changed

1 file changed

+3
-16
lines changed

src/librustc_mir/interpret/cast.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -244,37 +244,24 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
244244
) -> InterpResult<'tcx, Scalar<M::PointerTag>> {
245245
use rustc::ty::TyKind::*;
246246

247-
fn int_size<'tcx>(layout: TyLayout<'tcx>) -> Option<usize> {
248-
match layout.ty.sty {
249-
Int(i) => i.bit_width(),
250-
Uint(i) => i.bit_width(),
251-
_ => bug!("Not an integer"),
252-
}
253-
}
254-
255247
match dest_layout.ty.sty {
256248
// Casting to a reference or fn pointer is not permitted by rustc,
257249
// no need to support it here.
258250
RawPtr(_) => Ok(ptr.into()),
259251
Int(IntTy::Isize) | Uint(UintTy::Usize) => {
260-
let size = self.memory.pointer_size();
261-
262-
if let Ok(bits) = self.force_bits(Scalar::Ptr(ptr), size) {
252+
if let Ok(bits) = self.force_bits(Scalar::Ptr(ptr), self.memory.pointer_size()) {
263253
self.cast_from_int(bits, src_layout, dest_layout)
264254
} else {
265255
Ok(ptr.into())
266256
}
267257
}
268258
Int(_) | Uint(_) => {
269-
let size = Size::from_bits(int_size(dest_layout).unwrap() as u64);
270-
271-
if let Ok(bits) = self.force_bits(Scalar::Ptr(ptr), size) {
259+
if let Ok(bits) = self.force_bits(Scalar::Ptr(ptr), self.memory.pointer_size()) {
272260
self.cast_from_int(bits, src_layout, dest_layout)
273261
} else {
274262
err!(ReadPointerAsBytes)
275263
}
276-
},
277-
// Casting to any other type is not implemented
264+
}
278265
_ => return err!(Unimplemented(format!("ptr to {:?} cast", dest_layout.ty))),
279266
}
280267
}

0 commit comments

Comments
 (0)