1 use crate::build::expr::as_place::PlaceBuilder;
2 use crate::build::matches::MatchPair;
3 use crate::build::Builder;
4 use rustc_middle::mir::*;
5 use rustc_middle::thir::*;
7 use smallvec::SmallVec;
8 use std::convert::TryInto;
10 impl<'a, 'tcx> Builder<'a, 'tcx> {
11 pub(crate) fn field_match_pairs<'pat>(
13 place: PlaceBuilder<'tcx>,
14 subpatterns: &'pat [FieldPat<'tcx>],
15 ) -> Vec<MatchPair<'pat, 'tcx>> {
19 let place = place.clone().field(fieldpat.field, fieldpat.pattern.ty);
20 MatchPair::new(place, &fieldpat.pattern)
25 pub(crate) fn prefix_slice_suffix<'pat>(
27 match_pairs: &mut SmallVec<[MatchPair<'pat, 'tcx>; 1]>,
28 place: &PlaceBuilder<'tcx>,
29 prefix: &'pat [Pat<'tcx>],
30 opt_slice: Option<&'pat Pat<'tcx>>,
31 suffix: &'pat [Pat<'tcx>],
34 let (min_length, exact_size) =
35 if let Ok(place_resolved) = place.clone().try_upvars_resolved(self) {
36 match place_resolved.into_place(self).ty(&self.local_decls, tcx).ty.kind() {
37 ty::Array(_, length) => (length.eval_usize(tcx, self.param_env), true),
38 _ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
41 ((prefix.len() + suffix.len()).try_into().unwrap(), false)
44 match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
46 ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false };
47 let place = place.clone().project(elem);
48 MatchPair::new(place, subpattern)
51 if let Some(subslice_pat) = opt_slice {
52 let suffix_len = suffix.len() as u64;
53 let subslice = place.clone().project(ProjectionElem::Subslice {
54 from: prefix.len() as u64,
55 to: if exact_size { min_length - suffix_len } else { suffix_len },
56 from_end: !exact_size,
58 match_pairs.push(MatchPair::new(subslice, subslice_pat));
61 match_pairs.extend(suffix.iter().rev().enumerate().map(|(idx, subpattern)| {
62 let end_offset = (idx + 1) as u64;
63 let elem = ProjectionElem::ConstantIndex {
64 offset: if exact_size { min_length - end_offset } else { end_offset },
66 from_end: !exact_size,
68 let place = place.clone().project(elem);
69 MatchPair::new(place, subpattern)
73 /// Creates a false edge to `imaginary_target` and a real edge to
74 /// real_target. If `imaginary_target` is none, or is the same as the real
75 /// target, a Goto is generated instead to simplify the generated MIR.
76 pub(crate) fn false_edges(
78 from_block: BasicBlock,
79 real_target: BasicBlock,
80 imaginary_target: Option<BasicBlock>,
81 source_info: SourceInfo,
83 match imaginary_target {
84 Some(target) if target != real_target => {
88 TerminatorKind::FalseEdge { real_target, imaginary_target: target },
91 _ => self.cfg.goto(from_block, source_info, real_target),
96 impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
97 pub(in crate::build) fn new(
98 place: PlaceBuilder<'tcx>,
99 pattern: &'pat Pat<'tcx>,
100 ) -> MatchPair<'pat, 'tcx> {
101 MatchPair { place, pattern }