Skip to content

Commit bdbf9b2

Browse files
committed
compare_const_vals: add a special case for certain ranges.
This commit removes the `a == b` early return, which isn't useful in practice, and replaces it with one that helps matches with many ranges, including char ranges.
1 parent 73c52b7 commit bdbf9b2

File tree

1 file changed

+19
-4
lines changed
  • compiler/rustc_mir_build/src/thir/pattern

1 file changed

+19
-4
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use rustc_hir::def::{CtorOf, DefKind, Res};
1515
use rustc_hir::pat_util::EnumerateAndAdjustIterator;
1616
use rustc_hir::RangeEnd;
1717
use rustc_index::vec::Idx;
18-
use rustc_middle::mir::interpret::{ErrorHandled, LitToConstError, LitToConstInput};
18+
use rustc_middle::mir::interpret::{
19+
ConstValue, ErrorHandled, LitToConstError, LitToConstInput, Scalar,
20+
};
1921
use rustc_middle::mir::{self, UserTypeProjection};
2022
use rustc_middle::mir::{BorrowKind, Field, Mutability};
2123
use rustc_middle::thir::{Ascription, BindingMode, FieldPat, LocalVarId, Pat, PatKind, PatRange};
@@ -755,11 +757,24 @@ pub(crate) fn compare_const_vals<'tcx>(
755757
) -> Option<Ordering> {
756758
assert_eq!(a.ty(), b.ty());
757759

758-
if a == b {
759-
return Some(Ordering::Equal);
760+
let ty = a.ty();
761+
762+
// This code is hot when compiling matches with many ranges. So we
763+
// special-case extraction of evaluated scalars for speed, for types where
764+
// raw data comparisons are appropriate. E.g. `unicode-normalization` has
765+
// many ranges such as '\u{037A}'..='\u{037F}', and chars can be compared
766+
// in this way.
767+
match ty.kind() {
768+
ty::Float(_) | ty::Int(_) => {} // require special handling, see below
769+
_ => match (a, b) {
770+
(
771+
mir::ConstantKind::Val(ConstValue::Scalar(Scalar::Int(a)), _a_ty),
772+
mir::ConstantKind::Val(ConstValue::Scalar(Scalar::Int(b)), _b_ty),
773+
) => return Some(a.cmp(&b)),
774+
_ => {}
775+
},
760776
}
761777

762-
let ty = a.ty();
763778
let a = a.eval_bits(tcx, param_env, ty);
764779
let b = b.eval_bits(tcx, param_env, ty);
765780

0 commit comments

Comments
 (0)