@@ -242,14 +242,31 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> {
242
242
ty : Ty < ' tcx >
243
243
) -> InterpResult < ' tcx , Scalar < M :: PointerTag > > {
244
244
use rustc:: ty:: TyKind :: * ;
245
- match ty. sty {
245
+
246
+ let size = match ty. sty {
246
247
// Casting to a reference or fn pointer is not permitted by rustc,
247
248
// 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) ) ) ,
249
+ RawPtr ( _) => return Ok ( ptr. into ( ) ) ,
250
+ Int ( IntTy :: Isize ) | Uint ( UintTy :: Usize ) => {
251
+ let size = self . memory . pointer_size ( ) ;
252
+ if let Ok ( bits) = self . force_bits ( Scalar :: Ptr ( ptr) , size) {
253
+ return Ok ( Scalar :: from_uint ( bits, size) ) ;
254
+ }
255
+ return Ok ( ptr. into ( ) ) ;
256
+ }
257
+ // If the target type is a sized integer, we need the its size to perform the pointer cast
258
+ Int ( i) => i. bit_width ( ) . unwrap ( ) ,
259
+ Uint ( i) => i. bit_width ( ) . unwrap ( ) ,
260
+ // Casting to any other type is not implemented
261
+ _ => return err ! ( Unimplemented ( format!( "ptr to {:?} cast" , ty) ) ) ,
262
+ } ;
263
+
264
+ let size = Size :: from_bits ( size as u64 ) ;
265
+
266
+ if let Ok ( bits) = self . force_bits ( Scalar :: Ptr ( ptr) , size) {
267
+ Ok ( Scalar :: from_uint ( bits, size) )
268
+ } else {
269
+ err ! ( ReadPointerAsBytes )
253
270
}
254
271
}
255
272
0 commit comments