Skip to content

Commit c3aec30

Browse files
committed
Add a helper method with an explicit name instead of hand rolling a match 3x
1 parent 7839cb9 commit c3aec30

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
286286

287287
let newval = match (r_t_in, r_t_out) {
288288
(CastTy::Int(i), CastTy::Int(_)) => {
289-
bx.intcast(llval, ll_t_out, matches!(i, IntTy::I))
289+
bx.intcast(llval, ll_t_out, i.is_signed())
290290
}
291291
(CastTy::Float, CastTy::Float) => {
292292
let srcsz = bx.cx().float_width(ll_t_in);
@@ -300,7 +300,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
300300
}
301301
}
302302
(CastTy::Int(i), CastTy::Float) => {
303-
if matches!(i, IntTy::I) {
303+
if i.is_signed() {
304304
bx.sitofp(llval, ll_t_out)
305305
} else {
306306
bx.uitofp(llval, ll_t_out)
@@ -311,7 +311,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
311311
}
312312
(CastTy::Int(i), CastTy::Ptr(_)) => {
313313
let usize_llval =
314-
bx.intcast(llval, bx.cx().type_isize(), matches!(i, IntTy::I));
314+
bx.intcast(llval, bx.cx().type_isize(), i.is_signed());
315315
bx.inttoptr(usize_llval, ll_t_out)
316316
}
317317
(CastTy::Float, CastTy::Int(IntTy::I)) => {

compiler/rustc_middle/src/ty/cast.rs

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ pub enum IntTy {
1515
Char,
1616
}
1717

18+
impl IntTy {
19+
pub fn is_signed(self) -> bool {
20+
matches!(self, Self::I)
21+
}
22+
}
23+
1824
// Valid types for the result of a non-coercion cast
1925
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2026
pub enum CastTy<'tcx> {

0 commit comments

Comments
 (0)