1 use crate::build::matches::MatchPair;
2 use crate::build::Builder;
4 use rustc_middle::mir::*;
6 use smallvec::SmallVec;
7 use std::convert::TryInto;
9 impl<'a, 'tcx> Builder<'a, 'tcx> {
10 crate fn field_match_pairs<'pat>(
13 subpatterns: &'pat [FieldPat<'tcx>],
14 ) -> Vec<MatchPair<'pat, 'tcx>> {
18 let place = self.tcx.mk_place_field(place, fieldpat.field, fieldpat.pattern.ty);
19 MatchPair::new(place, &fieldpat.pattern)
24 crate fn prefix_slice_suffix<'pat>(
26 match_pairs: &mut SmallVec<[MatchPair<'pat, 'tcx>; 1]>,
28 prefix: &'pat [Pat<'tcx>],
29 opt_slice: Option<&'pat Pat<'tcx>>,
30 suffix: &'pat [Pat<'tcx>],
33 let (min_length, exact_size) = match place.ty(&self.local_decls, tcx).ty.kind() {
34 ty::Array(_, length) => (length.eval_usize(tcx, self.param_env), true),
35 _ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
38 match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
40 ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false };
41 let place = tcx.mk_place_elem(*place, elem);
42 MatchPair::new(place, subpattern)
45 if let Some(subslice_pat) = opt_slice {
46 let suffix_len = suffix.len() as u64;
47 let subslice = tcx.mk_place_elem(
49 ProjectionElem::Subslice {
50 from: prefix.len() as u64,
51 to: if exact_size { min_length - suffix_len } else { suffix_len },
52 from_end: !exact_size,
55 match_pairs.push(MatchPair::new(subslice, subslice_pat));
58 match_pairs.extend(suffix.iter().rev().enumerate().map(|(idx, subpattern)| {
59 let end_offset = (idx + 1) as u64;
60 let elem = ProjectionElem::ConstantIndex {
61 offset: if exact_size { min_length - end_offset } else { end_offset },
63 from_end: !exact_size,
65 let place = tcx.mk_place_elem(*place, elem);
66 MatchPair::new(place, subpattern)
70 /// Creates a false edge to `imaginary_target` and a real edge to
71 /// real_target. If `imaginary_target` is none, or is the same as the real
72 /// target, a Goto is generated instead to simplify the generated MIR.
75 from_block: BasicBlock,
76 real_target: BasicBlock,
77 imaginary_target: Option<BasicBlock>,
78 source_info: SourceInfo,
80 match imaginary_target {
81 Some(target) if target != real_target => {
85 TerminatorKind::FalseEdge { real_target, imaginary_target: target },
88 _ => self.cfg.goto(from_block, source_info, real_target),
93 impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
94 crate fn new(place: Place<'tcx>, pattern: &'pat Pat<'tcx>) -> MatchPair<'pat, 'tcx> {
95 MatchPair { place, pattern }