Skip to content

Commit 1c2c49e

Browse files
committed
no MIR errors but rip tests
1 parent 3fc5ee1 commit 1c2c49e

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use rustc_middle::mir::*;
22
use rustc_middle::thir::{self, *};
33
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
4-
use rustc_span::Span;
54

65
use crate::builder::Builder;
76
use crate::builder::expr::as_place::{PlaceBase, PlaceBuilder};
@@ -56,22 +55,21 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5655
((prefix.len() + suffix.len()).try_into().unwrap(), false)
5756
};
5857

59-
if self.subslice_work_optimizing(prefix) && opt_slice.is_none() && suffix.is_empty() {
58+
if self.should_optimize_subslice(prefix) {
6059
let elem_ty = prefix[0].ty;
6160
let prefix_valtree = self.simplify_const_pattern_slice_into_valtree(prefix);
6261

63-
let project = PlaceElem::Subslice {
62+
let src_path_subslice = place.clone_project(PlaceElem::Subslice {
6463
from: 0 as u64,
65-
to: prefix.len() as u64, // TODO: look at this
64+
to: prefix.len() as u64,
6665
from_end: !exact_size,
67-
};
66+
});
6867

6968
let match_pair = self.valtree_to_match_pair(
70-
src_path.ty,
71-
src_path.span,
69+
src_path,
7270
prefix.len() as u64,
7371
prefix_valtree,
74-
place.clone_project(project),
72+
src_path_subslice,
7573
elem_ty,
7674
);
7775

@@ -91,7 +89,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9189
let suffix_len = suffix.len() as u64;
9290
let subslice = place.clone_project(PlaceElem::Subslice {
9391
from: prefix.len() as u64,
94-
to: if exact_size { min_length - suffix_len } else { suffix_len }, // TODO: look at this
92+
to: if exact_size { min_length - suffix_len } else { suffix_len },
9593
from_end: !exact_size,
9694
});
9795
match_pairs.push(MatchPairTree::for_pattern(subslice, subslice_pat, self));
@@ -109,7 +107,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
109107
}));
110108
}
111109

112-
fn subslice_work_optimizing(&self, subslice: &[Box<Pat<'tcx>>]) -> bool {
110+
fn should_optimize_subslice(&self, subslice: &[Box<Pat<'tcx>>]) -> bool {
111+
// Only wasted effort if we're just comparing a single elememt anyway.
113112
subslice.len() > 1 && subslice.iter().all(|p| self.is_constant_pattern(p))
114113
}
115114

@@ -148,8 +147,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
148147

149148
fn valtree_to_match_pair<'pat>(
150149
&mut self,
151-
src_pat_ty: Ty<'tcx>,
152-
span: Span,
150+
src_path: &'pat Pat<'tcx>,
153151
subslice_len: u64,
154152
valtree: ty::ValTree<'tcx>,
155153
place: PlaceBuilder<'tcx>,
@@ -162,16 +160,20 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
162160
Ty::new_array(tcx, elem_ty, subslice_len),
163161
);
164162

165-
let pat_ty = match src_pat_ty.kind() {
163+
let pat_ty = match src_path.ty.kind() {
166164
ty::Slice(_) => Ty::new_slice(tcx, elem_ty),
167165
ty::Array(_, _) => Ty::new_array(tcx, elem_ty, subslice_len),
168166
_ => unreachable!(),
169167
};
170168

171-
let ty_cost = ty::Const::new(tcx, ty::ConstKind::Value(const_ty, valtree));
172-
let value = Const::Ty(const_ty, r#ty_cost);
169+
let ty_const = ty::Const::new(tcx, ty::ConstKind::Value(const_ty, valtree));
170+
let value = Const::Ty(const_ty, ty_const);
173171
let test_case = TestCase::Constant { value };
174-
let pattern = tcx.arena.alloc(Pat { ty: pat_ty, span, kind: PatKind::Constant { value } });
172+
let pattern = tcx.arena.alloc(Pat {
173+
ty: pat_ty,
174+
span: src_path.span,
175+
kind: PatKind::Constant { value },
176+
});
175177

176178
MatchPairTree {
177179
place: Some(place.to_place(self)),

library/core/src/cmp/pattern.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use crate::cmp::BytewiseEq;
2+
use crate::intrinsics::compare_bytes;
3+
use crate::mem;
4+
15
#[lang = "MatchLoweredCmp"]
26
#[const_trait]
37
trait MatchLoweredCmp<Rhs = Self>
@@ -39,7 +43,7 @@ where
3943
T: ~const MatchLoweredCmp,
4044
{
4145
#[rustc_allow_const_fn_unstable(const_trait_impl)]
42-
fn do_match(&self, other: &Self) -> bool {
46+
default fn do_match(&self, other: &Self) -> bool {
4347
if self.len() != other.len() {
4448
return false;
4549
}
@@ -57,3 +61,22 @@ where
5761
true
5862
}
5963
}
64+
65+
impl<T> const MatchLoweredCmp for [T]
66+
where
67+
T: ~const MatchLoweredCmp + BytewiseEq<T>,
68+
{
69+
#[rustc_allow_const_fn_unstable(core_intrinsics, const_trait_impl)]
70+
fn do_match(&self, other: &Self) -> bool {
71+
if self.len() != other.len() {
72+
return false;
73+
}
74+
75+
// SAFETY: `self` and `other` are references and are thus guaranteed to be valid.
76+
// The two slices have been checked to have the same size above.
77+
unsafe {
78+
let size = mem::size_of_val(self);
79+
compare_bytes(self.as_ptr() as *const u8, other.as_ptr() as *const u8, size) == 0
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)