From bf2c35f05dac421091cde5aae6c0a68fbfe7c3d1 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Thu, 19 Jul 2018 19:37:34 +0200 Subject: [PATCH] Implement char to uint cast --- example.rs | 4 ++++ src/base.rs | 1 + src/common.rs | 3 ++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/example.rs b/example.rs index 884a15710261..f0078c5a239c 100644 --- a/example.rs +++ b/example.rs @@ -135,6 +135,10 @@ fn int_cast(a: u16, b: i16) -> (u8, u16, u32, usize, i8, i16, i32, isize) { ) } +fn char_cast(c: char) -> u8 { + c as u8 +} + struct DebugTuple(()); fn debug_tuple() -> DebugTuple { diff --git a/src/base.rs b/src/base.rs index aebac3713ae7..773ba27dc71f 100644 --- a/src/base.rs +++ b/src/base.rs @@ -298,6 +298,7 @@ fn trans_stmt<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, stmt: &Statement<'tcx (TypeVariants::TyRawPtr(..), TypeVariants::TyRawPtr(..)) => { lval.write_cvalue(fx, operand.unchecked_cast_to(dest_layout)); } + (TypeVariants::TyChar, TypeVariants::TyUint(_)) | (TypeVariants::TyUint(_), TypeVariants::TyInt(_)) | (TypeVariants::TyUint(_), TypeVariants::TyUint(_)) => { let from = operand.load_value(fx); diff --git a/src/common.rs b/src/common.rs index c09d69c8fdff..7e471bf40a22 100644 --- a/src/common.rs +++ b/src/common.rs @@ -48,6 +48,7 @@ pub fn cton_type_from_ty<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx> IntTy::Isize => types::I64, } } + TypeVariants::TyChar => types::I32, TypeVariants::TyFnPtr(_) => types::I64, TypeVariants::TyRawPtr(TypeAndMut { ty, mutbl: _ }) | TypeVariants::TyRef(_, ty, _) => { if ty.is_sized(tcx.at(DUMMY_SP), ParamEnv::reveal_all()) { @@ -100,7 +101,7 @@ impl<'tcx> CValue<'tcx> { pub fn load_value<'a>(self, fx: &mut FunctionCx<'a, 'tcx>) -> Value where 'tcx: 'a{ match self { CValue::ByRef(addr, layout) => { - let cton_ty = fx.cton_type(layout.ty).expect(&format!("{:?}", layout.ty)); + let cton_ty = fx.cton_type(layout.ty).expect(&format!("load_value of type {:?}", layout.ty)); fx.bcx.ins().load(cton_ty, MemFlags::new(), addr, 0) } CValue::ByVal(value, _layout) => value,