Skip to content

Commit 0080e04

Browse files
committed
try to make things nice without regressing perf
1 parent 1ed8ff2 commit 0080e04

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

compiler/rustc_middle/src/mir/consts.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ impl<'tcx> Const<'tcx> {
244244
Const::Ty(c) => match c.kind() {
245245
ty::ConstKind::Value(valtree) if c.ty().is_primitive() => {
246246
// A valtree of a type where leaves directly represent the scalar const value.
247+
// Just checking whether it is a leaf is insufficient as e.g. references are leafs
248+
// but the leaf value is the value they point to, not the reference itself!
247249
Some(valtree.unwrap_leaf().into())
248250
}
249251
_ => None,
@@ -255,7 +257,17 @@ impl<'tcx> Const<'tcx> {
255257

256258
#[inline]
257259
pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
258-
self.try_to_scalar()?.try_to_int().ok()
260+
// This is equivalent to `self.try_to_scalar()?.try_to_int().ok()`, but measurably faster.
261+
match self {
262+
Const::Val(ConstValue::Scalar(Scalar::Int(x)), _) => Some(x),
263+
Const::Ty(c) => match c.kind() {
264+
ty::ConstKind::Value(valtree) if c.ty().is_primitive() => {
265+
Some(valtree.unwrap_leaf())
266+
}
267+
_ => None,
268+
},
269+
_ => None,
270+
}
259271
}
260272

261273
#[inline]

compiler/rustc_middle/src/thir.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1013,12 +1013,12 @@ impl<'tcx> PatRangeBoundary<'tcx> {
10131013
// many ranges such as '\u{037A}'..='\u{037F}', and chars can be compared
10141014
// in this way.
10151015
(Finite(a), Finite(b)) if matches!(ty.kind(), ty::Uint(_) | ty::Char) => {
1016-
let to_scalar_int = |x| match x {
1016+
/*let to_scalar_int = |x| match x {
10171017
mir::Const::Val(mir::ConstValue::Scalar(Scalar::Int(x)), _) => Some(x),
10181018
mir::Const::Ty(x) => Some(x.to_valtree().unwrap_leaf()),
10191019
_ => None,
1020-
};
1021-
if let (Some(a), Some(b)) = (to_scalar_int(a), to_scalar_int(b)) {
1020+
};*/
1021+
if let (Some(a), Some(b)) = (a.try_to_scalar_int(), b.try_to_scalar_int()) {
10221022
let sz = ty.primitive_size(tcx);
10231023
let a = a.assert_uint(sz);
10241024
let b = b.assert_uint(sz);

0 commit comments

Comments
 (0)