|
1 | 1 | use rustc::ty::{self, Ty, TypeAndMut}; |
2 | 2 | use rustc::ty::layout::{self, TyLayout, Size}; |
3 | 3 | use rustc::ty::adjustment::{PointerCast}; |
4 | | -use syntax::ast::{FloatTy, IntTy, UintTy}; |
| 4 | +use syntax::ast::FloatTy; |
5 | 5 | use syntax::symbol::sym; |
6 | 6 |
|
7 | 7 | use rustc_apfloat::ieee::{Single, Double}; |
@@ -151,7 +151,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> { |
151 | 151 | "Unexpected cast from type {:?}", src_layout.ty |
152 | 152 | ); |
153 | 153 | 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), |
155 | 155 | Ok(data) => self.cast_from_int(data, src_layout, dest_layout), |
156 | 156 | } |
157 | 157 | } |
@@ -239,17 +239,25 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpretCx<'mir, 'tcx, M> { |
239 | 239 | fn cast_from_ptr( |
240 | 240 | &self, |
241 | 241 | ptr: Pointer<M::PointerTag>, |
242 | | - ty: Ty<'tcx> |
| 242 | + src_layout: TyLayout<'tcx>, |
| 243 | + dest_layout: TyLayout<'tcx>, |
243 | 244 | ) -> InterpResult<'tcx, Scalar<M::PointerTag>> { |
244 | 245 | use rustc::ty::TyKind::*; |
245 | | - match ty.sty { |
| 246 | + |
| 247 | + match dest_layout.ty.sty { |
246 | 248 | // Casting to a reference or fn pointer is not permitted by rustc, |
247 | 249 | // 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) |
253 | 261 | } |
254 | 262 | } |
255 | 263 |
|
|
0 commit comments