From 5508d44e2be18441db6bd596d5b9f63202b6b61b Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 27 Jun 2018 15:23:40 +0200 Subject: [PATCH] Support bool constants and fix a u8 const error --- example.rs | 12 ++++++------ src/base.rs | 4 ++++ src/common.rs | 1 + 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/example.rs b/example.rs index dc78a6b1080..399130be4de 100644 --- a/example.rs +++ b/example.rs @@ -18,10 +18,10 @@ trait Mul { fn mul(self, rhs: RHS) -> Self::Output; } -impl Mul for u32 { - type Output = u32; +impl Mul for u8 { + type Output = Self; - fn mul(self, rhs: u32) -> u32 { + fn mul(self, rhs: Self) -> Self { self * rhs } } @@ -39,11 +39,11 @@ unsafe fn drop_in_place(to_drop: *mut T) { drop_in_place(to_drop); } -fn abc(a: u32) -> u32 { +fn abc(a: u8) -> u8 { a * 2 } -fn bcd(b: bool, a: u32) -> u32 { +fn bcd(b: bool, a: u8) -> u8 { if b { a * 2 } else { @@ -73,6 +73,6 @@ fn option_unwrap_or(o: BoolOption, d: bool) -> bool { } } -fn ret_42() -> u32 { +fn ret_42() -> u8 { 42 } diff --git a/src/base.rs b/src/base.rs index 696c0b9dfde..5f1d7670f9b 100644 --- a/src/base.rs +++ b/src/base.rs @@ -514,6 +514,10 @@ fn trans_operand<'a, 'tcx>(fx: &mut FunctionCx<'a, 'tcx>, operand: &Operand<'tcx Literal::Value { value } => { let layout = fx.layout_of(const_.ty); match const_.ty.sty { + TypeVariants::TyBool => { + let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap(); + CValue::const_val(fx, const_.ty, bits as u64 as i64) + } TypeVariants::TyUint(_) => { let bits = value.to_scalar().unwrap().to_bits(layout.size).unwrap(); CValue::const_val(fx, const_.ty, bits as u64 as i64) diff --git a/src/common.rs b/src/common.rs index 09a8e2da048..df6c88a2270 100644 --- a/src/common.rs +++ b/src/common.rs @@ -165,6 +165,7 @@ impl<'tcx> CValue<'tcx> { pub fn const_val<'a>(fx: &mut FunctionCx<'a, 'tcx>, ty: Ty<'tcx>, const_val: i64) -> CValue<'tcx> where 'tcx: 'a { let cton_ty = fx.cton_type(ty).unwrap(); + let cton_ty = fixup_cton_ty(cton_ty); let layout = fx.layout_of(ty); CValue::ByVal(fx.bcx.ins().iconst(cton_ty, const_val), layout) }