From c3aec3056e3c88bfc63db5cd4dc24624be7c576a Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 5 Jul 2022 09:26:45 +0000 Subject: [PATCH] Add a helper method with an explicit name instead of hand rolling a match 3x --- compiler/rustc_codegen_ssa/src/mir/rvalue.rs | 6 +++--- compiler/rustc_middle/src/ty/cast.rs | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs index e3a055b619a..953787ab6c5 100644 --- a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs +++ b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs @@ -286,7 +286,7 @@ pub fn codegen_rvalue_operand( let newval = match (r_t_in, r_t_out) { (CastTy::Int(i), CastTy::Int(_)) => { - bx.intcast(llval, ll_t_out, matches!(i, IntTy::I)) + bx.intcast(llval, ll_t_out, i.is_signed()) } (CastTy::Float, CastTy::Float) => { let srcsz = bx.cx().float_width(ll_t_in); @@ -300,7 +300,7 @@ pub fn codegen_rvalue_operand( } } (CastTy::Int(i), CastTy::Float) => { - if matches!(i, IntTy::I) { + if i.is_signed() { bx.sitofp(llval, ll_t_out) } else { bx.uitofp(llval, ll_t_out) @@ -311,7 +311,7 @@ pub fn codegen_rvalue_operand( } (CastTy::Int(i), CastTy::Ptr(_)) => { let usize_llval = - bx.intcast(llval, bx.cx().type_isize(), matches!(i, IntTy::I)); + bx.intcast(llval, bx.cx().type_isize(), i.is_signed()); bx.inttoptr(usize_llval, ll_t_out) } (CastTy::Float, CastTy::Int(IntTy::I)) => { diff --git a/compiler/rustc_middle/src/ty/cast.rs b/compiler/rustc_middle/src/ty/cast.rs index 20a6af5f6c1..c4b743dd467 100644 --- a/compiler/rustc_middle/src/ty/cast.rs +++ b/compiler/rustc_middle/src/ty/cast.rs @@ -15,6 +15,12 @@ pub enum IntTy { Char, } +impl IntTy { + pub fn is_signed(self) -> bool { + matches!(self, Self::I) + } +} + // Valid types for the result of a non-coercion cast #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum CastTy<'tcx> { -- GitLab