Skip to content

Commit e2d9ded

Browse files
committed
optimized cmp of other scalars
1 parent fde5210 commit e2d9ded

File tree

3 files changed

+9
-25
lines changed

3 files changed

+9
-25
lines changed

compiler/rustc_mir_build/src/builder/matches/match_pair.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
116116
fn is_constant_pattern(&self, pat: &Pat<'tcx>) -> bool {
117117
if let PatKind::Constant { value } = pat.kind
118118
&& let Const::Ty(_, const_) = value
119-
&& let ty::ConstKind::Value(ty, valtree) = const_.kind()
119+
&& let ty::ConstKind::Value(_, valtree) = const_.kind()
120120
&& let ty::ValTree::Leaf(_) = valtree
121-
&& self.tcx.types.u8 == ty
122121
{
123122
true
124123
} else {

compiler/rustc_mir_build/src/builder/matches/test.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc_middle::{bug, span_bug};
1717
use rustc_span::def_id::DefId;
1818
use rustc_span::source_map::Spanned;
1919
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
20-
use rustc_trait_selection::infer::InferCtxtExt;
2120
use tracing::{debug, instrument};
2221

2322
use crate::builder::Builder;
@@ -363,7 +362,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
363362
);
364363
}
365364

366-
/// Compare two values using `<T as std::compare::PartialEq>::eq`.
365+
/// Compare two values using `<T as std::compare::pattern::MatchLoweredCmp>::do_match`.
367366
/// If the values are already references, just call it directly, otherwise
368367
/// take a reference to the values first and then call it.
369368
fn non_scalar_compare(
@@ -451,36 +450,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
451450
(_, expect) = coerce(expect_ty, expect, *elem_ty);
452451
}
453452

454-
// Figure out the type we are searching for traits against. This involves an extra wrapping
453+
// Figure out the type we are searching for trait impls against. This involves an extra wrapping
455454
// reference: we can only compare two `&T`, and then compare_ty will be `T`.
456455
// Make sure that we do *not* call any user-defined code here.
457-
// The only types that can end up here are string and byte literals,
456+
// The only types that can end up here are str and ScalarInt slices,
458457
// which have their comparison defined in `core`.
459458
// (Interestingly this means that exhaustiveness analysis relies, for soundness,
460-
// on the `PartialEq` impls for `str` and `[u8]` to b correct!)
459+
// on the `MatchLoweredCmp` impls for `str` and `[T]` to be correct!)
461460
let compare_ty = match *ty.kind() {
462-
ty::Ref(_, deref_ty, _)
463-
if deref_ty == self.tcx.types.str_ || deref_ty != self.tcx.types.u8 =>
464-
{
461+
ty::Ref(_, deref_ty, _) if deref_ty == self.tcx.types.str_ || deref_ty.is_slice() => {
465462
deref_ty
466463
}
467464
_ => span_bug!(source_info.span, "invalid type for non-scalar compare: {}", ty),
468465
};
469466

470-
let mut cmp_trait_def_id =
467+
let cmp_trait_def_id =
471468
self.tcx.require_lang_item(LangItem::MatchLoweredCmp, Some(source_info.span));
472-
473-
let has_pattern_eq = self
474-
.infcx
475-
.type_implements_trait(cmp_trait_def_id, [compare_ty], self.param_env)
476-
.must_apply_modulo_regions();
477-
if !has_pattern_eq {
478-
cmp_trait_def_id =
479-
self.tcx.require_lang_item(LangItem::PartialEq, Some(source_info.span));
480-
}
481-
482469
let method =
483470
trait_method(self.tcx, cmp_trait_def_id, sym::do_match, [compare_ty, compare_ty]);
471+
484472
let bool_ty = self.tcx.types.bool;
485473
let eq_result = self.temp(bool_ty, source_info.span);
486474
let eq_block = self.cfg.start_new_block();

library/core/src/cmp/pattern.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ where
88
fn do_match(&self, other: &Rhs) -> bool;
99
}
1010

11-
// TODO: tuples, arrays?
12-
1311
macro_rules ! impl_match_lowered_cmp_for_primitive {
1412
($($t:ty),*) => {
1513
$(
@@ -23,13 +21,12 @@ macro_rules ! impl_match_lowered_cmp_for_primitive {
2321
}
2422

2523
impl_match_lowered_cmp_for_primitive! {
26-
(), bool, char,
24+
bool, char,
2725
u8, u16, u32, u64, u128, usize,
2826
i8, i16, i32, i64, i128, isize,
2927
f32, f64
3028
}
3129

32-
// note: this wasn't possible before
3330
impl const MatchLoweredCmp for str {
3431
#[rustc_allow_const_fn_unstable(const_trait_impl)]
3532
fn do_match(&self, other: &Self) -> bool {

0 commit comments

Comments
 (0)