Skip to content

Commit 96d8ae2

Browse files
committed
Simplify IntRange::from_const
1 parent bfb0f2d commit 96d8ae2

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ use rustc_index::vec::Idx;
5454

5555
use rustc_hir::def_id::DefId;
5656
use rustc_hir::{HirId, RangeEnd};
57-
use rustc_middle::mir::interpret::ConstValue;
5857
use rustc_middle::mir::Field;
5958
use rustc_middle::ty::layout::IntegerExt;
6059
use rustc_middle::ty::{self, Const, Ty, TyCtxt};
@@ -116,26 +115,20 @@ impl IntRange {
116115
param_env: ty::ParamEnv<'tcx>,
117116
value: &Const<'tcx>,
118117
) -> Option<IntRange> {
119-
if let Some((target_size, bias)) = Self::integral_size_and_signed_bias(tcx, value.ty) {
120-
let ty = value.ty;
121-
let val = (|| {
122-
if let ty::ConstKind::Value(ConstValue::Scalar(scalar)) = value.val {
123-
// For this specific pattern we can skip a lot of effort and go
124-
// straight to the result, after doing a bit of checking. (We
125-
// could remove this branch and just fall through, which
126-
// is more general but much slower.)
127-
if let Ok(bits) = scalar.to_bits_or_ptr(target_size, &tcx) {
128-
return Some(bits);
129-
}
130-
}
131-
// This is a more general form of the previous case.
132-
value.try_eval_bits(tcx, param_env, ty)
133-
})()?;
134-
let val = val ^ bias;
135-
Some(IntRange { range: val..=val })
118+
let (target_size, bias) = Self::integral_size_and_signed_bias(tcx, value.ty)?;
119+
let ty = value.ty;
120+
let val = if let Some(int) = value.val.try_to_scalar_int() {
121+
// For this specific pattern we can skip a lot of effort and go
122+
// straight to the result, after doing a bit of checking. (We
123+
// could remove this branch and just always use try_eval_bits, which
124+
// is more general but much slower.)
125+
int.assert_bits(target_size)
136126
} else {
137-
None
138-
}
127+
// This is a more general form of the previous case.
128+
value.try_eval_bits(tcx, param_env, ty)?
129+
};
130+
let val = val ^ bias;
131+
Some(IntRange { range: val..=val })
139132
}
140133

141134
#[inline]

0 commit comments

Comments
 (0)