From 6c86274943032b11c60b8ea3e512d4f6cd23db57 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Thu, 9 Aug 2018 11:07:10 +0200 Subject: [PATCH] Implement raw ptr -> usize cast --- examples/example.rs | 4 ++++ src/base.rs | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/examples/example.rs b/examples/example.rs index 05b370f91e0..bbffb13fabb 100644 --- a/examples/example.rs +++ b/examples/example.rs @@ -154,3 +154,7 @@ fn repeat_array() -> [u8; 3] { unsafe fn use_ctlz_nonzero(a: u16) -> u16 { intrinsics::ctlz_nonzero(a) } + +fn ptr_as_usize(ptr: *const u8) -> usize { + ptr as usize +} diff --git a/src/base.rs b/src/base.rs index be8281c59f4..15d950ef01a 100644 --- a/src/base.rs +++ b/src/base.rs @@ -379,6 +379,9 @@ fn trans_stmt<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, cur_ebb: Ebb, stmt: & | (TypeVariants::TyRawPtr(..), TypeVariants::TyRawPtr(..)) => { lval.write_cvalue(fx, operand.unchecked_cast_to(dest_layout)); } + (TypeVariants::TyRawPtr(..), TypeVariants::TyUint(_)) if to_ty.sty == fx.tcx.types.usize.sty => { + lval.write_cvalue(fx, operand.unchecked_cast_to(dest_layout)); + } (TypeVariants::TyChar, TypeVariants::TyUint(_)) | (TypeVariants::TyUint(_), TypeVariants::TyInt(_)) | (TypeVariants::TyUint(_), TypeVariants::TyUint(_)) => { @@ -392,7 +395,7 @@ fn trans_stmt<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, cur_ebb: Ebb, stmt: & let res = crate::common::cton_intcast(fx, from, from_ty, to_ty, true); lval.write_cvalue(fx, CValue::ByVal(res, dest_layout)); } - _ => unimpl!("rval misc {:?} {:?}", operand, to_ty), + _ => unimpl!("rval misc {:?} {:?}", from_ty, to_ty), } } Rvalue::Cast(CastKind::ClosureFnPointer, operand, ty) => {