Skip to content

Commit

Permalink
Add a helper method with an explicit name instead of hand rolling a m…
Browse files Browse the repository at this point in the history
…atch 3x
  • Loading branch information
oli-obk committed Jul 5, 2022
1 parent 7839cb9 commit c3aec30
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_codegen_ssa/src/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {

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);
Expand All @@ -300,7 +300,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
}
}
(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)
Expand All @@ -311,7 +311,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
}
(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)) => {
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/ty/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down

0 comments on commit c3aec30

Please sign in to comment.