Skip to content

Commit aca10b3

Browse files
committed
Simplify IntRange::from_const
1 parent 0502d42 commit aca10b3

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
@@ -52,7 +52,6 @@ use rustc_data_structures::captures::Captures;
5252
use rustc_index::vec::Idx;
5353

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

140133
#[inline]

0 commit comments

Comments
 (0)