Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9575d53

Browse files
committed
Upstream fast path
1 parent f4bf855 commit 9575d53

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

compiler/rustc_middle/src/mir/consts.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,15 @@ impl<'tcx> Const<'tcx> {
297297
tcx: TyCtxt<'tcx>,
298298
param_env: ty::ParamEnv<'tcx>,
299299
) -> Option<ScalarInt> {
300-
self.try_eval_scalar(tcx, param_env)?.try_to_int().ok()
300+
match self {
301+
// Fast path for already evaluated constants.
302+
Const::Val(ConstValue::Scalar(Scalar::Int(scalar_int)), _) => Some(scalar_int),
303+
Const::Ty(c)
304+
if let ty::ConstKind::Value(ty::ValTree::Leaf(scalar_int)) = c.kind()
305+
&& c.ty().is_primitive()
306+
=> Some(scalar_int),
307+
_ => self.try_eval_scalar(tcx, param_env)?.try_to_int().ok(),
308+
}
301309
}
302310

303311
#[inline]

compiler/rustc_middle/src/thir.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -941,13 +941,6 @@ impl<'tcx> PatRangeBoundary<'tcx> {
941941
param_env: ty::ParamEnv<'tcx>,
942942
) -> u128 {
943943
match self {
944-
Self::Finite {
945-
value: mir::Const::Val(mir::ConstValue::Scalar(Scalar::Int(scalar_int)), _),
946-
} => scalar_int.assert_bits(scalar_int.size()),
947-
Self::Finite {
948-
value: mir::Const::Ty(c),
949-
} if let ty::ConstKind::Value(ty::ValTree::Leaf(scalar_int)) = c.kind()
950-
=> scalar_int.assert_bits(scalar_int.size()),
951944
Self::Finite { value } => value.eval_bits(tcx, param_env),
952945
// Self::NegInfinity => {
953946
// // Unwrap is ok because the type is known to be numeric.

0 commit comments

Comments
 (0)